#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 stat(char *, struct stat *);
int test_create_tempdir(void) {
  char tempdir[PATH_MAX];
  struct stat st_buf;
  int ret;
  ret = get_tempdir(tempdir, PATH_MAX, 0770);
  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;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_create_tempdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2ea1(%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    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    $0x1f8,%edx
call   1050 <get_tempdir@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11ac <test_create_tempdir+0x6c>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    123f <test_create_tempdir+0xff>
mov    -0x30(%rbp),%rdi
call   1040 <register_tempdir_for_cleanup@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11da <test_create_tempdir+0x9a>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    123f <test_create_tempdir+0xff>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    1208 <test_create_tempdir+0xc8>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    123f <test_create_tempdir+0xff>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1231 <test_create_tempdir+0xf1>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    123f <test_create_tempdir+0xff>
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>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2ea3(%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    $0x1f8,%edx
call   1050 <get_tempdir@plt>
test   %eax,%eax
jne    1180 <test_create_tempdir+0x40>
mov    %rbx,%rdi
call   1040 <register_tempdir_for_cleanup@plt>
test   %eax,%eax
je     1190 <test_create_tempdir+0x50>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
lea    -0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     1180 <test_create_tempdir+0x40>
mov    -0x10(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     1180 <test_create_tempdir+0x40>
jmp    1189 <test_create_tempdir+0x49>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @test_create_tempdir() {
%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 504)
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 %36
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 %36
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 %36
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 %36
35: ; preds = %28
store i32 0, i32* %1
store i32 1, i32* %6
br label %36
36: ; preds = %35, %33, %26, %21, %15
%37 = load i8*, i8** %2
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @test_create_tempdir() {
%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 504)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
br label %27
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 %27
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 %27
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
%25 = load i32, i32* @EXIT_FAILURE
%26 = select i1 %24, i32 %25, i32 0
br label %27
27: ; preds = %20, %18, %13, %8
%28 = phi i32 [ %9, %8 ], [ %14, %13 ], [ %19, %18 ], [ %26, %20 ]
ret i32 %28
}