#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 st_mode;
};

/* Variables and functions */
scalar_t__ EEXIST;
int S_IFDIR;
scalar_t__ errno;
int /*<<< orphan*/ log_stderr(char *, char *);
int /*<<< orphan*/ log_stdout(char *, char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int mksubdir(char *path) {
  char buf[(4096)];
  struct stat statbuf;
  if (stat(path, &statbuf) == 0) {
    log_stdout("2210 Directory %s already exists.\n", path);
    if (!(statbuf.st_mode & S_IFDIR)) {
      log_stderr("2200 Log base %s is not a directory\n", path);
      return 0;
    }
    return 1;
  }
  log_stdout("2000 Creating directory %s\n", path);
  if (mkdir(path, 0777) != 0) {
    if (errno == EEXIST) {
      if (stat(path, &statbuf) == 0) {
        log_stdout("2210 Directory %s already exists.\n", path);
        if (!(statbuf.st_mode & S_IFDIR)) {
          log_stderr("2200 Log base %s is not a directory\n", path);
          return 0;
        }
        return 1;
      }
    }
    snprintf(buf, ((4096) - 1), "2100 Cannot create log path component %s",
             path);
    buf[((4096) - 1)] = '\0';
    perror(buf);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<mksubdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1018(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11d9 <mksubdir+0x79>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1070 <log_stdout@plt>
mov    -0x1018(%rbp),%eax
mov    0x2e22(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11cd <mksubdir+0x6d>
mov    -0x10(%rbp),%rsi
lea    0xe67(%rip),%rdi
call   1030 <log_stderr@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <mksubdir+0x169>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <mksubdir+0x169>
mov    -0x10(%rbp),%rsi
lea    0xe64(%rip),%rdi
call   1070 <log_stdout@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12c2 <mksubdir+0x162>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
jne    128a <mksubdir+0x12a>
mov    -0x10(%rbp),%rdi
lea    -0x1018(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1285 <mksubdir+0x125>
mov    -0x10(%rbp),%rsi
lea    0xdc0(%rip),%rdi
call   1070 <log_stdout@plt>
mov    -0x1018(%rbp),%eax
mov    0x2d76(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1279 <mksubdir+0x119>
mov    -0x10(%rbp),%rsi
lea    0xdbb(%rip),%rdi
call   1030 <log_stderr@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <mksubdir+0x169>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <mksubdir+0x169>
jmp    128a <mksubdir+0x12a>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xfff,%esi
lea    0xdc3(%rip),%rdx
call   1050 <snprintf@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x1010(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <mksubdir+0x169>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mksubdir>:
push   %rbp
push   %rbx
sub    $0x1018,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11e9 <mksubdir+0x89>
lea    0xec6(%rip),%rdi
mov    %rbx,%rsi
call   1070 <log_stdout@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     121d <mksubdir+0xbd>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    1229 <mksubdir+0xc9>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    1229 <mksubdir+0xc9>
lea    0xe30(%rip),%rdi
mov    %rbx,%rsi
call   1070 <log_stdout@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
jne    121d <mksubdir+0xbd>
jmp    120c <mksubdir+0xac>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
call   1070 <log_stdout@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    $0x1,%ebp
test   %eax,0x8(%rsp)
jne    121d <mksubdir+0xbd>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
call   1030 <log_stderr@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe34(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
movb   $0x0,0x100f(%rsp)
mov    %rbp,%rdi
call   1080 <perror@plt>
jmp    121b <mksubdir+0xbb>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [35 x i8] c"2210 Directory %s already exists.\0A\00"
@S_IFDIR = global i32 0
@.str.1 = [37 x i8] c"2200 Log base %s is not a directory\0A\00"
@.str.2 = [28 x i8] c"2000 Creating directory %s\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [41 x i8] c"2100 Cannot create log path component %s\00"
define i32 @mksubdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [4096 x i8]
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @stat(i8* %6, %struct.stat* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @log_stdout(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IFDIR
%15 = and i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = call i32 @log_stderr(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 0, i32* %2
br label %56
20: ; preds = %9
store i32 1, i32* %2
br label %56
21: ; preds = %1
%22 = load i8*, i8** %3
%23 = call i32 @log_stdout(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %3
%25 = call i64 @mkdir(i8* %24, i32 511)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %55
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = load i64, i64* @EEXIST
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %48
31: ; preds = %27
%32 = load i8*, i8** %3
%33 = call i64 @stat(i8* %32, %struct.stat* %5)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %31
%36 = load i8*, i8** %3
%37 = call i32 @log_stdout(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @S_IFDIR
%41 = and i32 %39, %40
%42 = icmp ne i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %35
%44 = load i8*, i8** %3
%45 = call i32 @log_stderr(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %44)
store i32 0, i32* %2
br label %56
46: ; preds = %35
store i32 1, i32* %2
br label %56
47: ; preds = %31
br label %48
48: ; preds = %47, %27
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%50 = load i8*, i8** %3
%51 = call i32 @snprintf(i8* %49, i32 4095, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 4095
store i8 0, i8* %52
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%54 = call i32 @perror(i8* %53)
store i32 0, i32* %2
br label %56
55: ; preds = %21
store i32 1, i32* %2
br label %56
56: ; preds = %55, %48, %46, %43, %20, %17
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [35 x i8] c"2210 Directory %s already exists.\0A\00"
@S_IFDIR = global i32 0
@.str.1 = [37 x i8] c"2200 Log base %s is not a directory\0A\00"
@.str.2 = [28 x i8] c"2000 Creating directory %s\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [41 x i8] c"2100 Cannot create log path component %s\00"
define i32 @mksubdir(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = call i32 @log_stdout(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFDIR
%13 = and i32 %12, %11
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %8
%16 = call i32 @log_stderr(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %41
17: ; preds = %1
%18 = call i32 @log_stdout(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = call i64 @mkdir(i8* %0, i32 511)
%20 = icmp eq i64 %19, 0
br i1 %20, label %41, label %21
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EEXIST
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = call i64 @stat(i8* %0, %struct.stat* %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = call i32 @log_stdout(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @S_IFDIR
%33 = and i32 %32, %31
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %28
%36 = call i32 @log_stderr(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %41
37: ; preds = %25, %21
%38 = call i32 @snprintf(i8* %4, i32 4095, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 4095
store i8 0, i8* %39
%40 = call i32 @perror(i8* %4)
br label %41
41: ; preds = %17, %28, %8, %37, %35, %15
%42 = phi i32 [ 0, %15 ], [ 0, %35 ], [ 0, %37 ], [ 1, %8 ], [ 1, %28 ], [ 1, %17 ]
ret i32 %42
}