#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 */
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int mkstemp(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int check_directory(char *dir) {
  int result = 0;
  char fname[PATH_MAX];
  int fd;
  DIR *dirdata;
  FILE *fp;
  if (dir && *dir) {
    if (!(dirdata = opendir(dir)))
      result = 2;
    else {
      closedir(dirdata);
      strcpy(fname, dir);
      if (fname[strlen(fname) - 1] != '/')
        strcat(fname, "/");
      strcat(fname, "test.XXXXXX");
      if ((fd = mkstemp(fname)) == -1)
        result = 3;
      else {
        close(fd);
        unlink(fname);
        if (!(fp = fopen(fname, "w")))
          result = 4;
        else {
          fclose(fp);
          unlink(fname);
        }
      }
    }
  } else
    result = 1;
  return result;
}


x86 assembly (clang, -O0):
<check_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12f2 <check_directory+0x152>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12f2 <check_directory+0x152>
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    121d <check_directory+0x7d>
movl   $0x2,-0xc(%rbp)
jmp    12ed <check_directory+0x14d>
mov    -0x30(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1265 <check_directory+0xc5>
mov    -0x40(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x40(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <mkstemp@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1296 <check_directory+0xf6>
movl   $0x3,-0xc(%rbp)
jmp    12e8 <check_directory+0x148>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x40(%rbp),%rdi
lea    0xd5c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12d1 <check_directory+0x131>
movl   $0x4,-0xc(%rbp)
jmp    12e3 <check_directory+0x143>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
jmp    12e8 <check_directory+0x148>
jmp    12ed <check_directory+0x14d>
jmp    12f9 <check_directory+0x159>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_directory>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    $0x1,%eax
test   %rdi,%rdi
je     1286 <check_directory+0xe6>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1286 <check_directory+0xe6>
mov    %rbx,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1273 <check_directory+0xd3>
mov    %rax,%rdi
call   1090 <closedir@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r14,1)
je     1220 <check_directory+0x80>
lea    0xde8(%rip),%rsi
mov    %r14,%rdi
call   10c0 <strcat@plt>
lea    0xddb(%rip),%rsi
mov    %r14,%rdi
call   10c0 <strcat@plt>
mov    %r14,%rdi
call   10a0 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     127a <check_directory+0xda>
mov    %eax,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
lea    0xdbc(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1281 <check_directory+0xe1>
mov    %rax,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
jmp    1286 <check_directory+0xe6>
mov    $0x2,%eax
jmp    1286 <check_directory+0xe6>
mov    $0x3,%eax
jmp    1286 <check_directory+0xe6>
mov    $0x4,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [12 x i8] c"test.XXXXXX\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @check_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %2
store i32 0, i32* %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %58
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %58
20: ; preds = %15
%21 = load i8*, i8** %2
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %20
store i32 2, i32* %3
br label %57
25: ; preds = %20
%26 = load i32*, i32** %7
%27 = call i32 @closedir(i32* %26)
%28 = load i8*, i8** %2
%29 = call i32 @strcpy(i8* %12, i8* %28)
%30 = call i32 @strlen(i8* %12)
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %12, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 47
br i1 %36, label %37, label %39
37: ; preds = %25
%38 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %25
%40 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @mkstemp(i8* %12)
store i32 %41, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 3, i32* %3
br label %56
44: ; preds = %39
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = call i32 @unlink(i8* %12)
%48 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %48, i32** %8
%49 = icmp ne i32* %48, null
br i1 %49, label %51, label %50
50: ; preds = %44
store i32 4, i32* %3
br label %55
51: ; preds = %44
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
%54 = call i32 @unlink(i8* %12)
br label %55
55: ; preds = %51, %50
br label %56
56: ; preds = %55, %43
br label %57
57: ; preds = %56, %24
br label %59
58: ; preds = %15, %1
store i32 1, i32* %3
br label %59
59: ; preds = %58, %57
%60 = load i32, i32* %3
%61 = load i8*, i8** %4
ret i32 %60
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [12 x i8] c"test.XXXXXX\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @check_directory(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = icmp eq i8* %0, null
br i1 %5, label %35, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %6
%10 = tail call i32* @opendir(i8* %0)
%11 = icmp eq i32* %10, null
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = tail call i32 @closedir(i32* %10)
%14 = call i32 @strcpy(i8* %4, i8* %0)
%15 = call i32 @strlen(i8* %4)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %4, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 47
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %12
%24 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @mkstemp(i8* %4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = call i32 @close(i32 %25)
%29 = call i32 @unlink(i8* %4)
%30 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %35, label %32
32: ; preds = %27
%33 = call i32 @fclose(i32* %30)
%34 = call i32 @unlink(i8* %4)
br label %35
35: ; preds = %1, %6, %27, %23, %9, %32
%36 = phi i32 [ 0, %32 ], [ 2, %9 ], [ 3, %23 ], [ 4, %27 ], [ 1, %6 ], [ 1, %1 ]
ret i32 %36
}