#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_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ log_event(int, char *, char *);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
int create_dir(char *dirname, int nolog) {
  struct stat fstatus;
  char buffer[128], *delim;
  int i;
  while ((i = strlen(dirname)) > 1 && dirname[i - 1] == '/')
    dirname[i - 1] = '\0';
  if (stat(dirname, &fstatus) || !S_ISDIR(fstatus.st_mode)) {
    strncpy(buffer, dirname, 128);
    delim = strrchr(buffer, '/');
    if (delim != buffer)
      delim[0] = '\0';
    else
      delim[1] = '\0';
    if (create_dir(buffer, nolog) != 0)
      return 1;
    stat(buffer, &fstatus);
    if (mkdir(dirname, fstatus.st_mode) != 0) {
      if (!nolog)
        log_event(1, "failed to create directory", dirname);
      return 1;
    } else if (!nolog)
      log_event(2, "directory created", dirname);
    if (chown(dirname, fstatus.st_uid, fstatus.st_gid) != 0)
      if (!nolog)
        log_event(4, "failed to set owner on directory (non fatal)", dirname);
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    %ecx,-0xac(%rbp)
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0xad(%rbp)
jle    11e4 <create_dir+0x54>
mov    -0x10(%rbp),%rax
mov    -0xac(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0xad(%rbp)
mov    -0xad(%rbp),%al
test   $0x1,%al
jne    11f7 <create_dir+0x67>
jmp    1210 <create_dir+0x80>
mov    -0x10(%rbp),%rax
mov    -0xac(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a2 <create_dir+0x12>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1238 <create_dir+0xa8>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1371 <create_dir+0x1e1>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x80,%edx
call   1030 <strncpy@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
lea    -0xa0(%rbp),%rcx
cmp    %rcx,%rax
je     128b <create_dir+0xfb>
mov    -0xa8(%rbp),%rax
movb   $0x0,(%rax)
jmp    1296 <create_dir+0x106>
mov    -0xa8(%rbp),%rax
movb   $0x0,0x1(%rax)
lea    -0xa0(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <create_dir@plt>
cmp    $0x0,%eax
je     12ba <create_dir+0x12a>
movl   $0x1,-0x4(%rbp)
jmp    1378 <create_dir+0x1e8>
lea    -0xa0(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     130b <create_dir+0x17b>
cmpl   $0x0,-0x14(%rbp)
jne    12ff <create_dir+0x16f>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xd06(%rip),%rsi
call   10a0 <log_event@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <create_dir+0x1e8>
cmpl   $0x0,-0x14(%rbp)
jne    132a <create_dir+0x19a>
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
lea    0xcf6(%rip),%rsi
call   10a0 <log_event@plt>
jmp    132f <create_dir+0x19f>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   10b0 <chown@plt>
cmp    $0x0,%rax
je     136c <create_dir+0x1dc>
cmpl   $0x0,-0x14(%rbp)
jne    1367 <create_dir+0x1d7>
mov    -0x10(%rbp),%rdx
mov    $0x4,%edi
lea    0xccb(%rip),%rsi
call   10a0 <log_event@plt>
jmp    136c <create_dir+0x1dc>
jmp    1371 <create_dir+0x1e1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmp    $0x2,%eax
jl     11ca <create_dir+0x3a>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%eax
cmpb   $0x2f,(%rbx,%rax,1)
jne    11ca <create_dir+0x3a>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jg     11b0 <create_dir+0x20>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     1259 <create_dir+0xc9>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
cmp    %rbp,%rax
lea    0x11(%rsp),%rcx
cmovne %rax,%rcx
movb   $0x0,(%rcx)
mov    %rbp,%rdi
mov    %r14d,%esi
call   1090 <create_dir@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    12b5 <create_dir+0x125>
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1080 <stat@plt>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
call   1040 <mkdir@plt>
test   %rax,%rax
je     126d <create_dir+0xdd>
test   %r14d,%r14d
jne    12b5 <create_dir+0x125>
lea    0xdb3(%rip),%rsi
mov    $0x1,%ebp
mov    $0x1,%edi
jmp    12ad <create_dir+0x11d>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    12b5 <create_dir+0x125>
jmp    11da <create_dir+0x4a>
test   %r14d,%r14d
jne    1286 <create_dir+0xf6>
lea    0xda2(%rip),%rsi
mov    $0x2,%edi
mov    %rbx,%rdx
call   10a0 <log_event@plt>
mov    (%rsp),%edx
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   10b0 <chown@plt>
xor    %ebp,%ebp
test   %r14d,%r14d
jne    12b5 <create_dir+0x125>
test   %rax,%rax
je     12b5 <create_dir+0x125>
lea    0xd85(%rip),%rsi
mov    $0x4,%edi
mov    %rbx,%rdx
call   10a0 <log_event@plt>
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@.str = [27 x i8] c"failed to create directory\00"
@.str.1 = [18 x i8] c"directory created\00"
@.str.2 = [45 x i8] c"failed to set owner on directory (non fatal)\00"
define i32 @create_dir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca [128 x i8]
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
br label %10
10: ; preds = %25, %2
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %9
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = load i32, i32* %9
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 47
br label %23
23: ; preds = %14, %10
%24 = phi i1 [ false, %10 ], [ %22, %14 ]
br i1 %24, label %25, label %31
25: ; preds = %23
%26 = load i8*, i8** %4
%27 = load i32, i32* %9
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 0, i8* %30
br label %10
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = call i64 @stat(i8* %32, %struct.stat* %6)
%34 = icmp ne i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %31
%36 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @S_ISDIR(i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %99, label %40
40: ; preds = %35, %31
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%42 = load i8*, i8** %4
%43 = call i32 @strncpy(i8* %41, i8* %42, i32 128)
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%45 = call i8* @strrchr(i8* %44, i8 signext 47)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%48 = icmp ne i8* %46, %47
br i1 %48, label %49, label %52
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 0, i8* %51
br label %55
52: ; preds = %40
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 1
store i8 0, i8* %54
br label %55
55: ; preds = %52, %49
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%57 = load i32, i32* %5
%58 = call i32 @create_dir(i8* %56, i32 %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
store i32 1, i32* %3
br label %100
61: ; preds = %55
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%63 = call i64 @stat(i8* %62, %struct.stat* %6)
%64 = load i8*, i8** %4
%65 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%66 = load i32, i32* %65
%67 = call i64 @mkdir(i8* %64, i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %76
69: ; preds = %61
%70 = load i32, i32* %5
%71 = icmp ne i32 %70, 0
br i1 %71, label %75, label %72
72: ; preds = %69
%73 = load i8*, i8** %4
%74 = call i32 @log_event(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %72, %69
store i32 1, i32* %3
br label %100
76: ; preds = %61
%77 = load i32, i32* %5
%78 = icmp ne i32 %77, 0
br i1 %78, label %82, label %79
79: ; preds = %76
%80 = load i8*, i8** %4
%81 = call i32 @log_event(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %76
br label %83
83: ; preds = %82
%84 = load i8*, i8** %4
%85 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%86 = load i32, i32* %85
%87 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%88 = load i32, i32* %87
%89 = call i64 @chown(i8* %84, i32 %86, i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %98
91: ; preds = %83
%92 = load i32, i32* %5
%93 = icmp ne i32 %92, 0
br i1 %93, label %97, label %94
94: ; preds = %91
%95 = load i8*, i8** %4
%96 = call i32 @log_event(i32 4, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %95)
br label %97
97: ; preds = %94, %91
br label %98
98: ; preds = %97, %83
br label %99
99: ; preds = %98, %35
store i32 0, i32* %3
br label %100
100: ; preds = %99, %75, %60
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@.str = [27 x i8] c"failed to create directory\00"
@.str.1 = [18 x i8] c"directory created\00"
@.str.2 = [45 x i8] c"failed to set owner on directory (non fatal)\00"
define i32 @create_dir(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca [128 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 1
br i1 %8, label %9, label %19
9: ; preds = %2, %16
%10 = phi i32 [ %17, %16 ], [ %7, %2 ]
%11 = add nsw i32 %10, -1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
br i1 %15, label %16, label %19
16: ; preds = %9
store i8 0, i8* %13
%17 = tail call i32 @strlen(i8* %0)
%18 = icmp sgt i32 %17, 1
br i1 %18, label %9, label %19
19: ; preds = %9, %16, %2
%20 = call i64 @stat(i8* %0, %struct.stat* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 @S_ISDIR(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %59
27: ; preds = %22, %19
%28 = call i32 @strncpy(i8* %6, i8* %0, i32 128)
%29 = call i8* @strrchr(i8* %6, i8 signext 47)
%30 = icmp eq i8* %29, %6
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 1
%32 = select i1 %30, i8* %31, i8* %29
store i8 0, i8* %32
%33 = call i32 @create_dir(i8* %6, i32 %1)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %59
35: ; preds = %27
%36 = call i64 @stat(i8* %6, %struct.stat* %3)
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%38 = load i32, i32* %37
%39 = call i64 @mkdir(i8* %0, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %35
%42 = icmp eq i32 %1, 0
br i1 %42, label %43, label %59
43: ; preds = %41
%44 = call i32 @log_event(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %59
45: ; preds = %35
%46 = icmp ne i32 %1, 0
br i1 %46, label %49, label %47
47: ; preds = %45
%48 = call i32 @log_event(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %49
49: ; preds = %45, %47
%50 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @chown(i8* %0, i32 %51, i32 %53)
%55 = icmp eq i64 %54, 0
%56 = or i1 %46, %55
br i1 %56, label %59, label %57
57: ; preds = %49
%58 = call i32 @log_event(i32 4, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %59
59: ; preds = %22, %57, %49, %41, %43, %27
%60 = phi i32 [ 1, %27 ], [ 1, %43 ], [ 1, %41 ], [ 0, %49 ], [ 0, %57 ], [ 0, %22 ]
ret i32 %60
}