#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 */
char *dirname(char *);
int mkdir(char const *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int mkdirs(const char *dir) {
  struct stat stats;
  if (stat(dir, &stats)) {
    char parent[1024];
    strncpy(parent, dir, 1024);
    if (!mkdirs(dirname(parent))) {
      return mkdir(dir, 0755);
    } else {
      perror(parent);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdirs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11e6 <mkdirs+0x86>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    -0x420(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,%rdi
call   1080 <mkdirs@plt>
cmp    $0x0,%eax
jne    11ce <mkdirs+0x6e>
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11ed <mkdirs+0x8d>
lea    -0x420(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <mkdirs+0x8d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirs>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11b4 <mkdirs+0x54>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1050 <dirname@plt>
mov    %rax,%rdi
call   1080 <mkdirs@plt>
test   %eax,%eax
jne    11c1 <mkdirs+0x61>
mov    %rbx,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
jmp    11b6 <mkdirs+0x56>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret
lea    0x10(%rsp),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11b6 <mkdirs+0x56>

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @mkdirs(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @stat(i8* %6, %struct.stat* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %1
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = load i8*, i8** %3
%12 = call i32 @strncpy(i8* %10, i8* %11, i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = call i8* @dirname(i8* %13)
%15 = call i32 @mkdirs(i8* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = call i32 @mkdir(i8* %18, i32 493)
store i32 %19, i32* %2
br label %24
20: ; preds = %9
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 @perror(i8* %21)
store i32 -1, i32* %2
br label %24
23: ; preds = %1
store i32 0, i32* %2
br label %24
24: ; preds = %23, %20, %17
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @mkdirs(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [1024 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %1
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = call i32 @strncpy(i8* %8, i8* %0, i32 1024)
%10 = call i8* @dirname(i8* %8)
%11 = call i32 @mkdirs(i8* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @mkdir(i8* %0, i32 493)
br label %17
15: ; preds = %7
%16 = call i32 @perror(i8* %8)
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ -1, %15 ], [ %14, %13 ]
br label %19
19: ; preds = %1, %17
%20 = phi i32 [ %18, %17 ], [ 0, %1 ]
ret i32 %20
}