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

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
char *strrchr(char *, char);
void mkdirhier(char *file) {
  struct stat st;
  char *s = file;
  const char *e = strrchr(file, '/');
  while (s < e) {
    s = strchr(s, '/');
    *s = 0;
    if (stat(file, &st))
      mkdir(file, 0777);
    else if (!S_ISDIR(st.st_mode)) {
      fprintf(stderr, "Cannot create %s\n", file);
      exit(1);
    }
    *s++ = '/';
  }
}


x86 assembly (clang, -O0):
<mkdirhier>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1237 <mkdirhier+0xc7>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11e7 <mkdirhier+0x77>
mov    -0x8(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
jmp    1220 <mkdirhier+0xb0>
mov    -0x10(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    121b <mkdirhier+0xab>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdf4(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1220 <mkdirhier+0xb0>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2f,(%rax)
jmp    1196 <mkdirhier+0x26>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirhier>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %r12,%rax
jbe    120a <mkdirhier+0x9a>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r12,%rdi
jmp    11bc <mkdirhier+0x4c>
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %rbx,%rdi
add    $0x1,%rdi
movb   $0x2f,(%rbx)
cmp    %r14,%rdi
jae    120a <mkdirhier+0x9a>
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    11a0 <mkdirhier+0x30>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
jne    11ad <mkdirhier+0x3d>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
mov    %r12,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Cannot create %s\0A\00"
define void @mkdirhier(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %4
%7 = load i8*, i8** %2
%8 = call i8* @strrchr(i8* %7, i8 signext 47)
store i8* %8, i8** %5
br label %9
9: ; preds = %34, %1
%10 = load i8*, i8** %4
%11 = load i8*, i8** %5
%12 = icmp ult i8* %10, %11
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = call i8* @strchr(i8* %14, i8 signext 47)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
store i8 0, i8* %16
%17 = load i8*, i8** %2
%18 = call i64 @stat(i8* %17, %struct.stat* %3)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i8*, i8** %2
%22 = call i32 @mkdir(i8* %21, i32 511)
br label %34
23: ; preds = %13
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISDIR(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %2
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
br label %34
34: ; preds = %33, %20
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %4
store i8 47, i8* %35
br label %9
37: ; preds = %9
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Cannot create %s\0A\00"
define void @mkdirhier(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%5 = icmp ugt i8* %4, %0
br i1 %5, label %6, label %26
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %8
8: ; preds = %6, %23
%9 = phi i8* [ %0, %6 ], [ %24, %23 ]
%10 = call i8* @strchr(i8* dereferenceable(1) %9, i8 signext 47)
store i8 0, i8* %10
%11 = call i64 @stat(i8* %0, %struct.stat* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = call i32 @mkdir(i8* %0, i32 511)
br label %23
15: ; preds = %8
%16 = load i32, i32* %7
%17 = call i32 @S_ISDIR(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15, %13
%24 = getelementptr inbounds i8, i8* %10, i64 1
store i8 47, i8* %10
%25 = icmp ult i8* %24, %4
br i1 %25, label %8, label %26
26: ; preds = %23, %1
ret void
}