#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 char *strdup;
typedef int chdir;

/* Variables and functions */
int /*<<< orphan*/ crash();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
char *strtok_r(char *, char *, char **);
void mkdirP(char *dir) {
  int tmpi;
  char *tdir, *elem, *saveptr;
  do {
    (tmpi) = (chdir)("/");
    if ((tmpi) == (-1)) {
      perror("chdir"
             "(\"/\")");
      crash();
    }
  } while (0);
  do {
    (tdir) = (strdup)(dir);
    if ((tdir) == (NULL)) {
      perror("strdup"
             "(dir)");
      crash();
    }
  } while (0);
  saveptr = NULL;
  while ((elem = strtok_r(saveptr ? NULL : tdir, "/", &saveptr))) {
    if (elem[0]) {
      mkdir(elem, 0777);
      do {
        (tmpi) = (chdir)(elem);
        if ((tmpi) == (-1)) {
          perror("chdir"
                 "(elem)");
          crash();
        }
      } while (0);
    }
  }
  free(tdir);
  do {
    (tmpi) = (chdir)("/");
    if ((tmpi) == (-1)) {
      perror("chdir"
             "(\"/\")");
      crash();
    }
  } while (0);
}


x86 assembly (clang, -O0):
<mkdirP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rax
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1183 <mkdirP+0x33>
lea    0xe8b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <crash@plt>
jmp    1188 <mkdirP+0x38>
jmp    118d <mkdirP+0x3d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <mkdirP+0x63>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <crash@plt>
jmp    11b8 <mkdirP+0x68>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d6 <mkdirP+0x86>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    11de <mkdirP+0x8e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe17(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     124e <mkdirP+0xfe>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1249 <mkdirP+0xf9>
mov    -0x20(%rbp),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    123f <mkdirP+0xef>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <crash@plt>
jmp    1244 <mkdirP+0xf4>
jmp    1249 <mkdirP+0xf9>
jmp    11c0 <mkdirP+0x70>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xda2(%rip),%rax
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    127e <mkdirP+0x12e>
lea    0xd90(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <crash@plt>
jmp    1283 <mkdirP+0x133>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirP>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r13
lea    0xe99(%rip),%rax
cmp    $0xffffffff,%eax
je     122c <mkdirP+0xdc>
test   %r13,%r13
je     1248 <mkdirP+0xf8>
movq   $0x0,0x8(%rsp)
lea    0xe77(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     120a <mkdirP+0xba>
mov    %rax,%rbx
lea    0xe5b(%rip),%r14
lea    0x8(%rsp),%r15
lea    0xe68(%rip),%r12
jmp    11d3 <mkdirP+0x83>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120a <mkdirP+0xba>
cmpb   $0x0,(%rbx)
je     11ea <mkdirP+0x9a>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
cmp    $0xffffffff,%ebx
je     11f9 <mkdirP+0xa9>
cmpq   $0x0,0x8(%rsp)
mov    %r13,%rdi
je     11c0 <mkdirP+0x70>
xor    %edi,%edi
jmp    11c0 <mkdirP+0x70>
mov    %r12,%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <crash@plt>
jmp    11ea <mkdirP+0x9a>
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xde7(%rip),%rax
cmp    $0xffffffff,%eax
je     1260 <mkdirP+0x110>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <crash@plt>
test   %r13,%r13
jne    1179 <mkdirP+0x29>
lea    0xdbe(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <crash@plt>
jmp    1179 <mkdirP+0x29>
lea    0xd9b(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <crash@plt>
jmp    121e <mkdirP+0xce>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c"chdir(\22/\22)\00"
@.str.2 = [12 x i8] c"strdup(dir)\00"
@.str.3 = [12 x i8] c"chdir(elem)\00"
define void @mkdirP(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
br label %7
7: ; preds = %1
store i32 ptrtoint ([2 x i8]* @.str to i32), i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @crash()
br label %13
13: ; preds = %10, %7
br label %14
14: ; preds = %13
br label %15
15: ; preds = %14
%16 = load i8*, i8** %2
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 (...) @crash()
br label %22
22: ; preds = %19, %15
br label %23
23: ; preds = %22
store i8* null, i8** %6
br label %24
24: ; preds = %52, %23
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %24
br label %30
28: ; preds = %24
%29 = load i8*, i8** %4
br label %30
30: ; preds = %28, %27
%31 = phi i8* [ null, %27 ], [ %29, %28 ]
%32 = call i8* @strtok_r(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6)
store i8* %32, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %53
34: ; preds = %30
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %34
%40 = load i8*, i8** %5
%41 = call i32 @mkdir(i8* %40, i32 511)
br label %42
42: ; preds = %39
%43 = load i8*, i8** %5
%44 = ptrtoint i8* %43 to i32
store i32 %44, i32* %3
%45 = load i32, i32* %3
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 (...) @crash()
br label %50
50: ; preds = %47, %42
br label %51
51: ; preds = %50
br label %52
52: ; preds = %51, %34
br label %24
53: ; preds = %30
%54 = load i8*, i8** %4
%55 = call i32 @free(i8* %54)
br label %56
56: ; preds = %53
store i32 ptrtoint ([2 x i8]* @.str to i32), i32* %3
%57 = load i32, i32* %3
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%61 = call i32 (...) @crash()
br label %62
62: ; preds = %59, %56
br label %63
63: ; preds = %62
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c"chdir(\22/\22)\00"
@.str.2 = [12 x i8] c"strdup(dir)\00"
@.str.3 = [12 x i8] c"chdir(elem)\00"
define void @mkdirP(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
br i1 icmp eq (i32 ptrtoint ([2 x i8]* @.str to i32), i32 -1), label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 (...) @crash()
br label %7
7: ; preds = %1, %4
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 (...) @crash()
br label %12
12: ; preds = %9, %7
store i8* null, i8** %2
%13 = call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%14 = icmp eq i8* %13, null
br i1 %14, label %33, label %15
15: ; preds = %12, %27
%16 = phi i8* [ %31, %27 ], [ %13, %12 ]
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = call i32 @mkdir(i8* %16, i32 511)
%21 = ptrtoint i8* %16 to i64
%22 = trunc i64 %21 to i32
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 (...) @crash()
br label %27
27: ; preds = %19, %24, %15
%28 = load i8*, i8** %2
%29 = icmp eq i8* %28, null
%30 = select i1 %29, i8* %0, i8* null
%31 = call i8* @strtok_r(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %15
33: ; preds = %27, %12
%34 = call i32 @free(i8* %0)
br i1 icmp eq (i32 ptrtoint ([2 x i8]* @.str to i32), i32 -1), label %35, label %38
35: ; preds = %33
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 (...) @crash()
br label %38
38: ; preds = %35, %33
ret void
}