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

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
int my_mkdir(char *idir, int mode) {
  char buffer[1024];
  char *ptr, *dir = buffer;
  struct stat st;
  strncpy(dir, idir, 1024 - 1);
  for (; dir != NULL && *dir;) {
    ptr = (char *)strchr(dir, '/');
    if (ptr != NULL) {
      *ptr = '\0';
    }
    if (stat(dir, &st) != 0) {
      if (mkdir(dir, mode) != 0)
        return -1;
    }
    chdir(dir);
    if (ptr != NULL)
      dir = ptr + 1;
    else
      dir = ptr;
  }
  return 0;
}


x86 assembly (clang, -O0):
<my_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
cmpq   $0x0,-0x430(%rbp)
mov    %al,-0x439(%rbp)
je     11b1 <my_mkdir+0x61>
mov    -0x430(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x439(%rbp)
mov    -0x439(%rbp),%al
test   $0x1,%al
jne    11c4 <my_mkdir+0x74>
jmp    127f <my_mkdir+0x12f>
mov    -0x430(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     11f4 <my_mkdir+0xa4>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x430(%rbp),%rdi
lea    -0x438(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     123b <my_mkdir+0xeb>
mov    -0x430(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1236 <my_mkdir+0xe6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <my_mkdir+0x136>
jmp    123b <my_mkdir+0xeb>
mov    -0x430(%rbp),%rdi
call   1050 <chdir@plt>
cmpq   $0x0,-0x428(%rbp)
je     126c <my_mkdir+0x11c>
mov    -0x428(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x430(%rbp)
jmp    127a <my_mkdir+0x12a>
mov    -0x428(%rbp),%rax
mov    %rax,-0x430(%rbp)
jmp    1185 <my_mkdir+0x35>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_mkdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %esi,%r14d
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%r15
xor    %r12d,%r12d
jmp    11a1 <my_mkdir+0x51>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <chdir@plt>
lea    0x1(%rbp),%rbx
test   %rbp,%rbp
je     11e5 <my_mkdir+0x95>
cmpb   $0x0,(%rbx)
je     11e5 <my_mkdir+0x95>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11bf <my_mkdir+0x6f>
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     1190 <my_mkdir+0x40>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1190 <my_mkdir+0x40>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @my_mkdir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = load i8*, i8** %4
%13 = call i32 @strncpy(i8* %11, i8* %12, i32 1023)
br label %14
14: ; preds = %53, %2
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i8*, i8** %8
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br label %22
22: ; preds = %17, %14
%23 = phi i1 [ false, %14 ], [ %21, %17 ]
br i1 %23, label %24, label %54
24: ; preds = %22
%25 = load i8*, i8** %8
%26 = call i64 @strchr(i8* %25, i8 signext 47)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i8*, i8** %7
store i8 0, i8* %31
br label %32
32: ; preds = %30, %24
%33 = load i8*, i8** %8
%34 = call i64 @stat(i8* %33, %struct.stat* %9)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i8*, i8** %8
%38 = load i32, i32* %5
%39 = call i64 @mkdir(i8* %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 -1, i32* %3
br label %55
42: ; preds = %36
br label %43
43: ; preds = %42, %32
%44 = load i8*, i8** %8
%45 = call i32 @chdir(i8* %44)
%46 = load i8*, i8** %7
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8* %50, i8** %8
br label %53
51: ; preds = %43
%52 = load i8*, i8** %7
store i8* %52, i8** %8
br label %53
53: ; preds = %51, %48
br label %14
54: ; preds = %22
store i32 0, i32* %3
br label %55
55: ; preds = %54, %41
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @my_mkdir(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i32 @strncpy(i8* %5, i8* %0, i32 1023)
br label %8
8: ; preds = %2, %23
%9 = phi i8* [ %5, %2 ], [ %25, %23 ]
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %8
%13 = call i64 @strchr(i8* %9, i8 signext 47)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %17, label %16
16: ; preds = %12
store i8 0, i8* %14
br label %17
17: ; preds = %16, %12
%18 = call i64 @stat(i8* %9, %struct.stat* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = call i64 @mkdir(i8* %9, i32 %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20, %17
%24 = call i32 @chdir(i8* %9)
%25 = getelementptr inbounds i8, i8* %14, i64 1
br i1 %15, label %26, label %8
26: ; preds = %8, %23, %20
%27 = phi i32 [ -1, %20 ], [ 0, %23 ], [ 0, %8 ]
ret i32 %27
}