#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 st_mode;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ ENOENT;
int S_IFDIR;
int /*<<< orphan*/ die(char *, char const *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int /*<<< orphan*/ strerror(scalar_t__);
char *tu__dirname(char const *);
void tu_mkpath(const char *path, mode_t mode) {
  struct stat st;
  char *dir;
  int rc;
  if (rc = stat(path, &st), rc && errno != ENOENT)
    goto fail;
  if (rc == 0 && st.st_mode & S_IFDIR)
    return;
  dir = tu__dirname(path);
  if (dir) {
    tu_mkpath(dir, mode);
    free(dir);
  }
  if (mkdir(path, mode))
    goto fail;
  return;
fail:
  die("tu_mkpath %s failed: %s", path, strerror(errno));
}


x86 assembly (clang, -O0):
<tu_mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
je     11b8 <tu_mkpath+0x48>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11c8 <tu_mkpath+0x58>
jmp    11cd <tu_mkpath+0x5d>
jmp    123e <tu_mkpath+0xce>
cmpl   $0x0,-0x1c(%rbp)
jne    11f1 <tu_mkpath+0x81>
mov    -0x10(%rbp),%eax
mov    0x2de7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f1 <tu_mkpath+0x81>
jmp    1267 <tu_mkpath+0xf7>
mov    -0x8(%rbp),%rdi
call   1070 <tu__dirname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     121e <tu_mkpath+0xae>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <tu_mkpath@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1239 <tu_mkpath+0xc9>
jmp    123e <tu_mkpath+0xce>
jmp    1267 <tu_mkpath+0xf7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xd9e(%rip),%rdi
call   1080 <die@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tu_mkpath>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
mov    0x2e54(%rip),%r15
test   %eax,%eax
je     119f <tu_mkpath+0x2f>
mov    (%r15),%rdi
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rdi
jne    11e7 <tu_mkpath+0x77>
test   %eax,%eax
jne    11b1 <tu_mkpath+0x41>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
jne    1207 <tu_mkpath+0x97>
mov    %rbx,%rdi
call   1070 <tu__dirname@plt>
test   %rax,%rax
je     11d4 <tu_mkpath+0x64>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14d,%esi
call   1060 <tu_mkpath@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1207 <tu_mkpath+0x97>
mov    (%r15),%rdi
call   1090 <strerror@plt>
lea    0xe0d(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <die@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@S_IFDIR = global i32 0
@.str = [24 x i8] c"tu_mkpath %s failed: %s\00"
define void @tu_mkpath(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %5)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp ne i64 %13, %14
br label %16
16: ; preds = %12, %2
%17 = phi i1 [ false, %2 ], [ %15, %12 ]
br i1 %17, label %18, label %19
18: ; preds = %16
br label %46
19: ; preds = %16
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @S_IFDIR
%26 = and i32 %24, %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %51
29: ; preds = %22, %19
%30 = load i8*, i8** %3
%31 = call i8* @tu__dirname(i8* %30)
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = load i32, i32* %4
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %34, %29
%40 = load i8*, i8** %3
%41 = load i32, i32* %4
%42 = call i64 @mkdir(i8* %40, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
br label %46
45: ; preds = %39
br label %51
46: ; preds = %44, %18
%47 = load i8*, i8** %3
%48 = load i64, i64* @errno
%49 = call i32 @strerror(i64 %48)
%50 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %47, i32 %49)
br label %51
51: ; preds = %46, %45, %28
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@S_IFDIR = global i32 0
@.str = [24 x i8] c"tu_mkpath %s failed: %s\00"
define void @tu_mkpath(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp ne i32 %5, 0
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp ne i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %29, label %11
11: ; preds = %2
%12 = icmp eq i32 %5, 0
br i1 %12, label %13, label %19
13: ; preds = %11
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @S_IFDIR
%17 = and i32 %16, %15
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %13, %11
%20 = tail call i8* @tu__dirname(i8* %0)
%21 = icmp eq i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %19
tail call void @tu_mkpath(i8* %20, i32 %1)
%23 = tail call i32 @free(i8* %20)
br label %24
24: ; preds = %22, %19
%25 = tail call i64 @mkdir(i8* %0, i32 %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = load i64, i64* @errno
br label %29
29: ; preds = %27, %2
%30 = phi i64 [ %28, %27 ], [ %7, %2 ]
%31 = tail call i32 @strerror(i64 %30)
%32 = tail call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %31)
br label %33
33: ; preds = %24, %13, %29
ret void
}