#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(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char const *, int);
int /*<<< orphan*/ perror(char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strdup(char const *);
void prepare_dir(const char *dir) {
  struct stat st;
  if (0 == stat(dir, &st))
    return;
  char *parent = dirname(strdup(dir));
  prepare_dir(parent);
  free(parent);
  if (0 != mkdir(dir, 0700))
    perror(dir);
}


x86 assembly (clang, -O0):
<prepare_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    119c <prepare_dir+0x2c>
jmp    11e7 <prepare_dir+0x77>
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %eax,%edi
call   1050 <dirname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <prepare_dir@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11e7 <prepare_dir+0x77>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11b8 <prepare_dir+0x48>
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %eax,%edi
call   1050 <dirname@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <prepare_dir@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jne    11c0 <prepare_dir+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1070 <perror@plt>
jmp    11b8 <prepare_dir+0x48>

LLVM IR (-O0):
%struct.stat = type { i32 }
define void @prepare_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @stat(i8* %5, %struct.stat* %3)
%7 = icmp eq i64 0, %6
br i1 %7, label %8, label %9
8: ; preds = %1
br label %22
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @strdup(i8* %10)
%12 = call i8* @dirname(i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i32 @free(i8* %14)
%16 = load i8*, i8** %2
%17 = call i64 @mkdir(i8* %16, i32 448)
%18 = icmp ne i64 0, %17
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = load i8*, i8** %2
%21 = call i32 @perror(i8* %20)
br label %22
22: ; preds = %8, %19, %9
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define void @prepare_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %1
%7 = call i32 @strdup(i8* %0)
%8 = call i8* @dirname(i32 %7)
%9 = call i32 @free(i8* %8)
%10 = call i64 @mkdir(i8* %0, i32 448)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %6
%13 = call i32 @perror(i8* %0)
br label %14
14: ; preds = %6, %12, %1
ret void
}