#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 */

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ errno;
int mkdir(char *, int);
char *strchr(char *, char);
int mkdir_for_file(char *fn) {
  for (char *p = fn; (p = strchr(p + 1, '/')) != NULL;) {
    if (p[1] == '\0')
      return 0;
    *p = '\0';
    int rc = mkdir(fn, 0700);
    *p = '/';
    if (0 != rc && errno != EEXIST)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_for_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11c6 <mkdir_for_file+0xa6>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1171 <mkdir_for_file+0x51>
movl   $0x0,-0x4(%rbp)
jmp    11cd <mkdir_for_file+0xad>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
je     11c1 <mkdir_for_file+0xa1>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
je     11c1 <mkdir_for_file+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <mkdir_for_file+0xad>
jmp    1134 <mkdir_for_file+0x14>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_for_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11a7 <mkdir_for_file+0x87>
mov    %rax,%rbp
mov    0x2e8d(%rip),%r15
mov    0x2e8e(%rip),%r12
jmp    1179 <mkdir_for_file+0x59>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11a7 <mkdir_for_file+0x87>
cmpb   $0x0,0x1(%rbp)
je     11a7 <mkdir_for_file+0x87>
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
movb   $0x2f,0x0(%rbp)
test   %eax,%eax
je     1160 <mkdir_for_file+0x40>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <mkdir_for_file+0x40>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_for_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
store i8* %6, i8** %4
br label %7
7: ; preds = %31, %1
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = call i8* @strchr(i8* %9, i8 signext 47)
store i8* %10, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %32
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 0, i32* %2
br label %33
19: ; preds = %12
%20 = load i8*, i8** %4
store i8 0, i8* %20
%21 = load i8*, i8** %3
%22 = call i32 @mkdir(i8* %21, i32 448)
store i32 %22, i32* %5
%23 = load i8*, i8** %4
store i8 47, i8* %23
%24 = load i32, i32* %5
%25 = icmp ne i32 0, %24
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i64, i64* @errno
%28 = load i64, i64* @EEXIST
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -1, i32* %2
br label %33
31: ; preds = %26, %19
br label %7
32: ; preds = %7
store i32 0, i32* %2
br label %33
33: ; preds = %32, %30, %18
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_for_file(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 1
%3 = tail call i8* @strchr(i8* %2, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %20, label %5
5: ; preds = %1, %17
%6 = phi i8* [ %18, %17 ], [ %3, %1 ]
%7 = getelementptr inbounds i8, i8* %6, i64 1
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %5
store i8 0, i8* %6
%11 = tail call i32 @mkdir(i8* %0, i32 448)
store i8 47, i8* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EEXIST
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13, %10
%18 = tail call i8* @strchr(i8* %7, i8 signext 47)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %5
20: ; preds = %17, %5, %13, %1
%21 = phi i32 [ 0, %1 ], [ -1, %13 ], [ 0, %5 ], [ 0, %17 ]
ret i32 %21
}