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

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int mkpath_helper(const char *path, mode_t mode) {
  struct stat st;
  if (stat(path, &st) != 0) {
    if ((mkdir(path, mode) != 0) && (errno != EEXIST))
      return 0;
  } else if (!S_ISDIR(st.st_mode)) {
    errno = ENOTDIR;
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<mkpath_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1197 <mkpath_helper+0x67>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     1192 <mkpath_helper+0x62>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     1192 <mkpath_helper+0x62>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <mkpath_helper+0xa4>
jmp    11cd <mkpath_helper+0x9d>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11c8 <mkpath_helper+0x98>
mov    0x2e49(%rip),%rax
mov    (%rax),%rcx
mov    0x2e17(%rip),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11d4 <mkpath_helper+0xa4>
jmp    11cd <mkpath_helper+0x9d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath_helper>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     116e <mkpath_helper+0x3e>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     117a <mkpath_helper+0x4a>
mov    0x2e75(%rip),%rax
mov    (%rax),%rcx
mov    0x2e73(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     117a <mkpath_helper+0x4a>
jmp    117f <mkpath_helper+0x4f>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     1186 <mkpath_helper+0x56>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
mov    %rax,(%rcx)
xor    %eax,%eax
jmp    117f <mkpath_helper+0x4f>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@ENOTDIR = global i64 0
define i32 @mkpath_helper(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i64 @stat(i8* %7, %struct.stat* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = call i64 @mkdir(i8* %11, i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %3
br label %30
20: ; preds = %15, %10
br label %29
21: ; preds = %2
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @S_ISDIR(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = load i64, i64* @ENOTDIR
store i64 %27, i64* @errno
store i32 0, i32* %3
br label %30
28: ; preds = %21
br label %29
29: ; preds = %28, %20
store i32 1, i32* %3
br label %30
30: ; preds = %29, %26, %19
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@ENOTDIR = global i64 0
define i32 @mkpath_helper(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %2
%8 = call i64 @mkdir(i8* %0, i32 %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EEXIST
%13 = icmp eq i64 %11, %12
br i1 %13, label %21, label %22
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @S_ISDIR(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i64, i64* @ENOTDIR
store i64 %20, i64* @errno
br label %22
21: ; preds = %14, %7, %10
br label %22
22: ; preds = %10, %21, %19
%23 = phi i32 [ 1, %21 ], [ 0, %19 ], [ 0, %10 ]
ret i32 %23
}