#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 O_CREAT;
int O_WRONLY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int close(int /*<<< orphan*/);
scalar_t__ errno;
int mkdir(char *, int);
int /*<<< orphan*/ open(char *, int, int);
int stat(char *, struct stat *);
char *strrchr(char *, char);
int create_target_hierarchy(char *target, int is_dir) {
  int status;
  struct stat buf;
  if (!is_dir) {
    int result = stat(target, &buf);
    if (result) {
      char *pos = strrchr(target, '/');
      if (pos != target) {
        *pos = '\0';
        status = create_target_hierarchy(target, 1);
        *pos = '/';
        if (status)
          return status;
      }
      return close(open(target, O_CREAT | O_WRONLY, 0777));
    } else if (!S_ISDIR(buf.st_mode)) {
      return 0;
    } else {
      return -1;
    }
  } else {
    int result = stat(target, &buf);
    if (result) {
      char *pos = strrchr(target, '/');
      if (pos != target) {
        *pos = '\0';
        status = create_target_hierarchy(target, 1);
        *pos = '/';
        if (status)
          return status;
      }
      result = mkdir(target, 0777);
      if (result && errno == EEXIST)
        result = 0;
      return result;
    } else if (S_ISDIR(buf.st_mode)) {
      return 0;
    } else {
      return -1;
    }
  }
  assert(0);
}


x86 assembly (clang, -O0):
<create_target_hierarchy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1255 <create_target_hierarchy+0xe5>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     122b <create_target_hierarchy+0xbb>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11fc <create_target_hierarchy+0x8c>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1040 <create_target_hierarchy@plt>
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x2f,(%rax)
cmpl   $0x0,-0x18(%rbp)
je     11f7 <create_target_hierarchy+0x87>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
jmp    11fc <create_target_hierarchy+0x8c>
mov    -0x10(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,%edi
call   1070 <close@plt>
mov    %eax,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1249 <create_target_hierarchy+0xd9>
movl   $0x0,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     130f <create_target_hierarchy+0x19f>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     12c8 <create_target_hierarchy+0x158>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1040 <create_target_hierarchy@plt>
mov    %eax,-0x18(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x2f,(%rax)
cmpl   $0x0,-0x18(%rbp)
je     12c3 <create_target_hierarchy+0x153>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
jmp    12c8 <create_target_hierarchy+0x158>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1304 <create_target_hierarchy+0x194>
mov    0x2ce6(%rip),%rax
mov    (%rax),%rax
mov    0x2ce4(%rip),%rcx
cmp    (%rcx),%rax
jne    1304 <create_target_hierarchy+0x194>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     132d <create_target_hierarchy+0x1bd>
movl   $0x0,-0x4(%rbp)
jmp    1334 <create_target_hierarchy+0x1c4>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_target_hierarchy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %ebp,%ebp
je     11f4 <create_target_hierarchy+0x84>
test   %eax,%eax
je     1253 <create_target_hierarchy+0xe3>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %rbx,%rax
je     11c4 <create_target_hierarchy+0x54>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <create_target_hierarchy@plt>
movb   $0x2f,(%r14)
test   %eax,%eax
jne    1270 <create_target_hierarchy+0x100>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
je     1279 <create_target_hierarchy+0x109>
mov    0x2df0(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2dee(%rip),%rdx
xor    %esi,%esi
cmp    (%rdx),%rcx
cmove  %esi,%eax
jmp    1270 <create_target_hierarchy+0x100>
test   %eax,%eax
je     1262 <create_target_hierarchy+0xf2>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %rbx,%rax
je     1225 <create_target_hierarchy+0xb5>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <create_target_hierarchy@plt>
movb   $0x2f,(%r14)
test   %eax,%eax
jne    1270 <create_target_hierarchy+0x100>
mov    0x2dcc(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x1,%rax
jmp    126e <create_target_hierarchy+0xfe>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
neg    %rax
sbb    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    1270 <create_target_hierarchy+0x100>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @create_target_hierarchy(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %49, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @stat(i8* %15, %struct.stat* %7)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %42
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i8* @strrchr(i8* %20, i8 signext 47)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = load i8*, i8** %4
%24 = icmp ne i8* %22, %23
br i1 %24, label %25, label %35
25: ; preds = %19
%26 = load i8*, i8** %9
store i8 0, i8* %26
%27 = load i8*, i8** %4
%28 = call i32 @create_target_hierarchy(i8* %27, i32 1)
store i32 %28, i32* %6
%29 = load i8*, i8** %9
store i8 47, i8* %29
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %89
34: ; preds = %25
br label %35
35: ; preds = %34, %19
%36 = load i8*, i8** %4
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_WRONLY
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %36, i32 %39, i32 511)
%41 = call i32 @close(i32 %40)
store i32 %41, i32* %3
br label %89
42: ; preds = %14
%43 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @S_ISDIR(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %42
store i32 0, i32* %3
br label %89
48: ; preds = %42
store i32 -1, i32* %3
br label %89
49: ; preds = %2
%50 = load i8*, i8** %4
%51 = call i32 @stat(i8* %50, %struct.stat* %7)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %82
54: ; preds = %49
%55 = load i8*, i8** %4
%56 = call i8* @strrchr(i8* %55, i8 signext 47)
store i8* %56, i8** %11
%57 = load i8*, i8** %11
%58 = load i8*, i8** %4
%59 = icmp ne i8* %57, %58
br i1 %59, label %60, label %70
60: ; preds = %54
%61 = load i8*, i8** %11
store i8 0, i8* %61
%62 = load i8*, i8** %4
%63 = call i32 @create_target_hierarchy(i8* %62, i32 1)
store i32 %63, i32* %6
%64 = load i8*, i8** %11
store i8 47, i8* %64
%65 = load i32, i32* %6
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %60
%68 = load i32, i32* %6
store i32 %68, i32* %3
br label %89
69: ; preds = %60
br label %70
70: ; preds = %69, %54
%71 = load i8*, i8** %4
%72 = call i32 @mkdir(i8* %71, i32 511)
store i32 %72, i32* %10
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %70
%76 = load i64, i64* @errno
%77 = load i64, i64* @EEXIST
%78 = icmp eq i64 %76, %77
br i1 %78, label %79, label %80
79: ; preds = %75
store i32 0, i32* %10
br label %80
80: ; preds = %79, %75, %70
%81 = load i32, i32* %10
store i32 %81, i32* %3
br label %89
82: ; preds = %49
%83 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%84 = load i32, i32* %83
%85 = call i64 @S_ISDIR(i32 %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %82
store i32 0, i32* %3
br label %89
88: ; preds = %82
store i32 -1, i32* %3
br label %89
89: ; preds = %88, %87, %80, %67, %48, %47, %35, %32
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @create_target_hierarchy(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %1, 0
%6 = call i32 @stat(i8* %0, %struct.stat* %3)
%7 = icmp eq i32 %6, 0
br i1 %5, label %8, label %27
8: ; preds = %2
br i1 %7, label %21, label %9
9: ; preds = %8
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%11 = icmp eq i8* %10, %0
br i1 %11, label %15, label %12
12: ; preds = %9
store i8 0, i8* %10
%13 = tail call i32 @create_target_hierarchy(i8* %0, i32 1)
store i8 47, i8* %10
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %48
15: ; preds = %12, %9
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %0, i32 %18, i32 511)
%20 = tail call i32 @close(i32 %19)
br label %48
21: ; preds = %8
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i64 @S_ISDIR(i32 %23)
%25 = icmp ne i64 %24, 0
%26 = sext i1 %25 to i32
br label %48
27: ; preds = %2
br i1 %7, label %42, label %28
28: ; preds = %27
%29 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%30 = icmp eq i8* %29, %0
br i1 %30, label %34, label %31
31: ; preds = %28
store i8 0, i8* %29
%32 = tail call i32 @create_target_hierarchy(i8* %0, i32 1)
store i8 47, i8* %29
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %48
34: ; preds = %31, %28
%35 = tail call i32 @mkdir(i8* %0, i32 511)
%36 = icmp eq i32 %35, 0
br i1 %36, label %48, label %37
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EEXIST
%40 = icmp eq i64 %38, %39
%41 = select i1 %40, i32 0, i32 %35
br label %48
42: ; preds = %27
%43 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%44 = load i32, i32* %43
%45 = tail call i64 @S_ISDIR(i32 %44)
%46 = icmp eq i64 %45, 0
%47 = sext i1 %46 to i32
br label %48
48: ; preds = %37, %34, %31, %42, %12, %15, %21
%49 = phi i32 [ %20, %15 ], [ %13, %12 ], [ %26, %21 ], [ %32, %31 ], [ 0, %34 ], [ %41, %37 ], [ %47, %42 ]
ret i32 %49
}