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

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int mkdir(char *, int /*<<< orphan*/);
char *strchr(char *, char);
char *strdup(char const *);
int mkpath(const char *file_path_in, mode_t mode) {
  char *file_path = strdup(file_path_in);
  char *p = NULL;
  for (p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
    *p = '\0';
    if (mkdir(file_path, mode) == -1) {
      if (errno != EEXIST) {
        free(file_path);
        return -1;
      }
    }
    *p = '/';
  }
  free(file_path);
  return 0;
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f7 <mkpath+0xb7>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    11d5 <mkpath+0x95>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <mkpath+0x90>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <mkpath+0xc7>
jmp    11d5 <mkpath+0x95>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    117a <mkpath+0x3a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
call   1060 <strdup@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11c1 <mkpath+0x81>
mov    %rax,%rbp
mov    0x2e62(%rip),%r12
mov    0x2e63(%rip),%r13
jmp    119d <mkpath+0x5d>
nop
movb   $0x2f,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11c1 <mkpath+0x81>
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1180 <mkpath+0x40>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1180 <mkpath+0x40>
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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