#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 /*<<< orphan*/ free(char *);
int mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
int recurse_mkdir(char *path, int mode) {
  int ret = 0;
  char *cpy = strdup(path);
  char *parent = dirname(cpy);
  struct stat st;
  if (stat(parent, &st) < 0)
    ret = recurse_mkdir(parent, mode);
  free(cpy);
  if (ret < 0)
    return ret;
  return mkdir(path, mode);
}


x86 assembly (clang, -O0):
<recurse_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11b6 <recurse_mkdir+0x56>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <recurse_mkdir@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x18(%rbp)
jge    11d4 <recurse_mkdir+0x74>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <recurse_mkdir+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recurse_mkdir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1080 <strdup@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <dirname@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     119a <recurse_mkdir+0x3a>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    11b3 <recurse_mkdir+0x53>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <recurse_mkdir@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
test   %ebx,%ebx
js     11c0 <recurse_mkdir+0x60>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @recurse_mkdir(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%10 = load i8*, i8** %4
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = call i8* @dirname(i8* %12)
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = call i64 @stat(i8* %14, %struct.stat* %9)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i8*, i8** %8
%19 = load i32, i32* %5
%20 = call i32 @recurse_mkdir(i8* %18, i32 %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %17, %2
%22 = load i8*, i8** %7
%23 = call i32 @free(i8* %22)
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i32, i32* %6
store i32 %27, i32* %3
br label %32
28: ; preds = %21
%29 = load i8*, i8** %4
%30 = load i32, i32* %5
%31 = call i32 @mkdir(i8* %29, i32 %30)
store i32 %31, i32* %3
br label %32
32: ; preds = %28, %26
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @recurse_mkdir(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @dirname(i8* %4)
%6 = bitcast %struct.stat* %3 to i8*
%7 = call i64 @stat(i8* %5, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @free(i8* %4)
br label %15
11: ; preds = %2
%12 = call i32 @recurse_mkdir(i8* %5, i32 %1)
%13 = call i32 @free(i8* %4)
%14 = icmp slt i32 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %9, %11
%16 = call i32 @mkdir(i8* %0, i32 %1)
br label %17
17: ; preds = %11, %15
%18 = phi i32 [ %16, %15 ], [ %12, %11 ]
ret i32 %18
}