#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 */
scalar_t__ EEXIST;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ log_error(char *, char *);
int mkdir(char *, int);
int stat(char *, struct stat *);
int create_directory(char *dirname) {
  struct stat buf;
  int err = mkdir(dirname, 0750);
  if (err == -1) {
    if (errno != EEXIST) {
      log_error("Could not create the directory %s. Exiting", dirname);
      return err;
    }
    err = stat(dirname, &buf);
    if (err) {
      log_error("Stat failed for file %s. Exiting", dirname);
      return err;
    }
    if (!S_ISDIR(buf.st_mode)) {
      log_error("File %s exist but is not a directory. Exiting", dirname);
      err = -1;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<create_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1e8,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fe <create_directory+0xbe>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
je     119c <create_directory+0x5c>
mov    -0x10(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1040 <log_error@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <create_directory+0xc4>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11d1 <create_directory+0x91>
mov    -0x10(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <log_error@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <create_directory+0xc4>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11f9 <create_directory+0xb9>
mov    -0x10(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11fe <create_directory+0xbe>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_directory>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x1e8,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    119b <create_directory+0x5b>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    118a <create_directory+0x4a>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %eax,%eax
je     11a2 <create_directory+0x62>
lea    0xea5(%rip),%rdi
mov    %eax,%ebp
jmp    1191 <create_directory+0x51>
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <log_error@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    119b <create_directory+0x5b>
lea    0xe93(%rip),%rdi
jmp    1191 <create_directory+0x51>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@.str = [43 x i8] c"Could not create the directory %s. Exiting\00"
@.str.1 = [33 x i8] c"Stat failed for file %s. Exiting\00"
@.str.2 = [46 x i8] c"File %s exist but is not a directory. Exiting\00"
define i32 @create_directory(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @mkdir(i8* %6, i32 488)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %36
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EEXIST
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i32 @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* %5
store i32 %17, i32* %2
br label %38
18: ; preds = %10
%19 = load i8*, i8** %3
%20 = call i32 @stat(i8* %19, %struct.stat* %4)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i32 @log_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %5
store i32 %26, i32* %2
br label %38
27: ; preds = %18
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @S_ISDIR(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = call i32 @log_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %33)
store i32 -1, i32* %5
br label %35
35: ; preds = %32, %27
br label %36
36: ; preds = %35, %1
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %38
38: ; preds = %36, %23, %14
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@.str = [43 x i8] c"Could not create the directory %s. Exiting\00"
@.str.1 = [33 x i8] c"Stat failed for file %s. Exiting\00"
@.str.2 = [46 x i8] c"File %s exist but is not a directory. Exiting\00"
define i32 @create_directory(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @mkdir(i8* %0, i32 488)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %22
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EEXIST
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %18
10: ; preds = %6
%11 = call i32 @stat(i8* %0, %struct.stat* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @S_ISDIR(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13, %10, %6
%19 = phi i8* [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %10 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), %13 ]
%20 = phi i32 [ -1, %6 ], [ %11, %10 ], [ -1, %13 ]
%21 = tail call i32 @log_error(i8* %19, i8* %0)
br label %22
22: ; preds = %18, %1, %13
%23 = phi i32 [ 0, %13 ], [ %4, %1 ], [ %20, %18 ]
ret i32 %23
}