#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int EXIT_FAILURE;
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int get_tempdir(char *, int, int);
int register_tempdir_for_cleanup(char *);
int /*<<< orphan*/ remove_tempdir(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ unregister_tempdir_for_cleanup(char *);
int test_create_tempdir_and_delete(void) {
  char tempdir[PATH_MAX];
  struct stat st_buf;
  int ret;
  ret = get_tempdir(tempdir, PATH_MAX, 0775);
  if (ret)
    return EXIT_FAILURE;
  ret = register_tempdir_for_cleanup(tempdir);
  if (ret)
    return EXIT_FAILURE;
  if (stat(tempdir, &st_buf) == -1) {
    return EXIT_FAILURE;
  }
  if (!S_ISDIR(st_buf.st_mode)) {
    return EXIT_FAILURE;
  }
  remove_tempdir(tempdir);
  unregister_tempdir_for_cleanup(tempdir);
  return 0;
}


x86 assembly (clang, -O0):
<test_create_tempdir_and_delete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    $0x1fd,%edx
call   1070 <get_tempdir@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11cc <test_create_tempdir_and_delete+0x6c>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1271 <test_create_tempdir_and_delete+0x111>
mov    -0x30(%rbp),%rdi
call   1060 <register_tempdir_for_cleanup@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11fa <test_create_tempdir_and_delete+0x9a>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1271 <test_create_tempdir_and_delete+0x111>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1228 <test_create_tempdir_and_delete+0xc8>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1271 <test_create_tempdir_and_delete+0x111>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1251 <test_create_tempdir_and_delete+0xf1>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1271 <test_create_tempdir_and_delete+0x111>
mov    -0x30(%rbp),%rdi
call   1030 <remove_tempdir@plt>
mov    -0x30(%rbp),%rdi
call   1040 <unregister_tempdir_for_cleanup@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_create_tempdir_and_delete>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    $0x1fd,%edx
call   1070 <get_tempdir@plt>
test   %eax,%eax
jne    11a0 <test_create_tempdir_and_delete+0x40>
mov    %rbx,%rdi
call   1060 <register_tempdir_for_cleanup@plt>
test   %eax,%eax
je     11b0 <test_create_tempdir_and_delete+0x50>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
lea    -0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     11a0 <test_create_tempdir_and_delete+0x40>
mov    -0x10(%rbp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     11a0 <test_create_tempdir_and_delete+0x40>
mov    %rbx,%rdi
call   1030 <remove_tempdir@plt>
mov    %rbx,%rdi
call   1040 <unregister_tempdir_for_cleanup@plt>
xor    %eax,%eax
jmp    11a9 <test_create_tempdir_and_delete+0x49>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @test_create_tempdir_and_delete() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %2
%10 = alloca i8, i64 %8
store i64 %8, i64* %3
%11 = load i32, i32* @PATH_MAX
%12 = call i32 @get_tempdir(i8* %10, i32 %11, i32 509)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
store i32 1, i32* %6
br label %38
17: ; preds = %0
%18 = call i32 @register_tempdir_for_cleanup(i8* %10)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* @EXIT_FAILURE
store i32 %22, i32* %1
store i32 1, i32* %6
br label %38
23: ; preds = %17
%24 = call i32 @stat(i8* %10, %struct.stat* %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %1
store i32 1, i32* %6
br label %38
28: ; preds = %23
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @S_ISDIR(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %1
store i32 1, i32* %6
br label %38
35: ; preds = %28
%36 = call i32 @remove_tempdir(i8* %10)
%37 = call i32 @unregister_tempdir_for_cleanup(i8* %10)
store i32 0, i32* %1
store i32 1, i32* %6
br label %38
38: ; preds = %35, %33, %26, %21, %15
%39 = load i8*, i8** %2
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @test_create_tempdir_and_delete() {
%1 = alloca %struct.stat
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = bitcast %struct.stat* %1 to i8*
%6 = call i32 @get_tempdir(i8* %4, i32 %2, i32 509)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
br label %30
10: ; preds = %0
%11 = call i32 @register_tempdir_for_cleanup(i8* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = load i32, i32* @EXIT_FAILURE
br label %30
15: ; preds = %10
%16 = call i32 @stat(i8* %4, %struct.stat* %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
br label %30
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i32, i32* @EXIT_FAILURE
br label %30
27: ; preds = %20
%28 = call i32 @remove_tempdir(i8* %4)
%29 = call i32 @unregister_tempdir_for_cleanup(i8* %4)
br label %30
30: ; preds = %27, %25, %18, %13, %8
%31 = phi i32 [ %9, %8 ], [ %14, %13 ], [ %19, %18 ], [ 0, %27 ], [ %26, %25 ]
ret i32 %31
}