#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*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
char *dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int mkdir(char *, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
char *strdup(char const *);
int mkpath(const char *s, mode_t mode) {
  char *q, *r = NULL, *path = NULL, *up = NULL;
  int rv = -1;
  if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
    return (0);
  if ((path = strdup(s)) == NULL)
    exit(1);
  if ((q = strdup(s)) == NULL)
    exit(1);
  if ((r = dirname(q)) == NULL)
    goto out;
  if ((up = strdup(r)) == NULL)
    exit(1);
  if ((mkpath(up, mode) == -1) && (errno != EEXIST))
    goto out;
  if ((mkdir(path, mode) == -1) && (errno != EEXIST))
    rv = -1;
  else
    rv = 0;
out:
  if (up != NULL)
    free(up);
  free(q);
  free(path);
  return (rv);
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11d2 <mkpath+0x62>
mov    -0x10(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11de <mkpath+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    1304 <mkpath+0x194>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11ff <mkpath+0x8f>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1220 <mkpath+0xb0>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    123c <mkpath+0xcc>
jmp    12d8 <mkpath+0x168>
mov    -0x28(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    125d <mkpath+0xed>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <mkpath@plt>
cmp    $0xffffffff,%eax
jne    1291 <mkpath+0x121>
mov    0x2d5f(%rip),%rax
mov    (%rax),%rax
mov    0x2d5d(%rip),%rcx
cmp    (%rcx),%rax
je     1291 <mkpath+0x121>
jmp    12d8 <mkpath+0x168>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    12cc <mkpath+0x15c>
mov    0x2d2b(%rip),%rax
mov    (%rax),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
je     12cc <mkpath+0x15c>
movl   $0xffffffff,-0x3c(%rbp)
jmp    12d3 <mkpath+0x163>
movl   $0x0,-0x3c(%rbp)
jmp    12d8 <mkpath+0x168>
cmpq   $0x0,-0x38(%rbp)
je     12ec <mkpath+0x17c>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xe7b(%rip),%rsi
call   1060 <strcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     126d <mkpath+0xfd>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     126d <mkpath+0xfd>
mov    %rbx,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     1278 <mkpath+0x108>
mov    %rax,%r14
mov    %rbx,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     1278 <mkpath+0x108>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <dirname@plt>
test   %rax,%rax
je     1258 <mkpath+0xe8>
mov    %rax,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     1278 <mkpath+0x108>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15d,%esi
call   1070 <mkpath@plt>
cmp    $0xffffffff,%eax
jne    1221 <mkpath+0xb1>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    124e <mkpath+0xde>
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    124c <mkpath+0xdc>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2d9e(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    124e <mkpath+0xde>
xor    %ebp,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
jmp    125d <mkpath+0xed>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
store i32 -1, i32* %10
%11 = load i8*, i8** %4
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14, %2
store i32 0, i32* %3
br label %76
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = call i8* @strdup(i8* %26)
store i8* %27, i8** %6
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %25
%32 = load i8*, i8** %6
%33 = call i8* @dirname(i8* %32)
store i8* %33, i8** %7
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %36
35: ; preds = %31
br label %64
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = call i8* @strdup(i8* %37)
store i8* %38, i8** %9
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = load i8*, i8** %9
%44 = load i32, i32* %5
%45 = call i32 @mkpath(i8* %43, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i64, i64* @errno
%49 = load i64, i64* @EEXIST
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
br label %64
52: ; preds = %47, %42
%53 = load i8*, i8** %8
%54 = load i32, i32* %5
%55 = call i32 @mkdir(i8* %53, i32 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i64, i64* @errno
%59 = load i64, i64* @EEXIST
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %57
store i32 -1, i32* %10
br label %63
62: ; preds = %57, %52
store i32 0, i32* %10
br label %63
63: ; preds = %62, %61
br label %64
64: ; preds = %63, %51, %35
%65 = load i8*, i8** %9
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i8*, i8** %9
%69 = call i32 @free(i8* %68)
br label %70
70: ; preds = %67, %64
%71 = load i8*, i8** %6
%72 = call i32 @free(i8* %71)
%73 = load i8*, i8** %8
%74 = call i32 @free(i8* %73)
%75 = load i32, i32* %10
store i32 %75, i32* %3
br label %76
76: ; preds = %70, %18
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkpath(i8* %0, i32 %1) {
%3 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %48, label %5
5: ; preds = %2
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %48, label %8
8: ; preds = %5
%9 = tail call i8* @strdup(i8* %0)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %8
%14 = tail call i8* @strdup(i8* %0)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = tail call i8* @dirname(i8* %14)
%20 = icmp eq i8* %19, null
br i1 %20, label %44, label %21
21: ; preds = %18
%22 = tail call i8* @strdup(i8* %19)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %21
%27 = tail call i32 @mkpath(i8* %22, i32 %1)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EEXIST
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %29, %26
%34 = tail call i32 @mkdir(i8* %9, i32 %1)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EEXIST
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36, %33
br label %41
41: ; preds = %29, %40, %36
%42 = phi i32 [ -1, %36 ], [ 0, %40 ], [ -1, %29 ]
%43 = tail call i32 @free(i8* %22)
br label %44
44: ; preds = %18, %41
%45 = phi i32 [ %42, %41 ], [ -1, %18 ]
%46 = tail call i32 @free(i8* %14)
%47 = tail call i32 @free(i8* %9)
br label %48
48: ; preds = %2, %5, %44
%49 = phi i32 [ %45, %44 ], [ 0, %5 ], [ 0, %2 ]
ret i32 %49
}