#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;
scalar_t__ mkdir(char *, int);
char *strchr(char *, char);
int mkdir_for_file(char *filename) {
  char *p = filename;
  if (!filename || !filename[0])
    return -1;
  while ((p = strchr(p + 1, '/')) != NULL) {
    *p = '\0';
    if ((mkdir(filename, 0700) != 0) && (errno != EEXIST)) {
      *p = '/';
      return -1;
    }
    *p++ = '/';
    if (!*p)
      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)
cmpq   $0x0,-0x10(%rbp)
je     114c <mkdir_for_file+0x2c>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1158 <mkdir_for_file+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <mkdir_for_file+0xe0>
jmp    115d <mkdir_for_file+0x3d>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11f9 <mkdir_for_file+0xd9>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11c9 <mkdir_for_file+0xa9>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
je     11c9 <mkdir_for_file+0xa9>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <mkdir_for_file+0xe0>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2f,(%rax)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11f4 <mkdir_for_file+0xd4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <mkdir_for_file+0xe0>
jmp    115d <mkdir_for_file+0x3d>
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    $0xffffffff,%r14d
test   %rdi,%rdi
je     119e <mkdir_for_file+0x7e>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     119e <mkdir_for_file+0x7e>
mov    0x2e96(%rip),%r15
mov    0x2e97(%rip),%r12
mov    %rbx,%rdi
jmp    1161 <mkdir_for_file+0x41>
xchg   %ax,%ax
mov    %rbp,%rdi
add    $0x1,%rdi
movb   $0x2f,0x0(%rbp)
cmpb   $0x0,0x1(%rbp)
je     119e <mkdir_for_file+0x7e>
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     119b <mkdir_for_file+0x7b>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     1150 <mkdir_for_file+0x30>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <mkdir_for_file+0x30>
movb   $0x2f,0x0(%rbp)
jmp    119e <mkdir_for_file+0x7e>
xor    %r14d,%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*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
store i8* %5, i8** %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %8, %1
store i32 -1, i32* %2
br label %40
14: ; preds = %8
br label %15
15: ; preds = %38, %14
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = call i8* @strchr(i8* %17, i8 signext 47)
store i8* %18, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %39
20: ; preds = %15
%21 = load i8*, i8** %4
store i8 0, i8* %21
%22 = load i8*, i8** %3
%23 = call i64 @mkdir(i8* %22, i32 448)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EEXIST
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i8*, i8** %4
store i8 47, i8* %30
store i32 -1, i32* %2
br label %40
31: ; preds = %25, %20
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %4
store i8 47, i8* %32
%34 = load i8*, i8** %4
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31
store i32 -1, i32* %2
br label %40
38: ; preds = %31
br label %15
39: ; preds = %15
store i32 0, i32* %2
br label %40
40: ; preds = %39, %37, %29, %13
%41 = load i32, i32* %2
ret i32 %41
}

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