#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 EEXIST;
int ENOTDIR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int errno;
int mkdir(char const *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int mkdir_if_not_exist(const char *dirname) {
  int ret;
  struct stat statbuf;
  ret = mkdir(dirname, S_IRWXU);
  if (ret && errno != EEXIST)
    return -errno;
  ret = stat(dirname, &statbuf);
  if (ret)
    return -errno;
  if (!S_ISDIR(statbuf.st_mode))
    ret = -ENOTDIR;
  return ret;
}


x86 assembly (clang, -O0):
<mkdir_if_not_exist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1186 <mkdir_if_not_exist+0x56>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%eax
je     1186 <mkdir_if_not_exist+0x56>
xor    %eax,%eax
mov    0x2e4c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <mkdir_if_not_exist+0xa8>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b3 <mkdir_if_not_exist+0x83>
xor    %eax,%eax
mov    0x2e1f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <mkdir_if_not_exist+0xa8>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11d2 <mkdir_if_not_exist+0xa2>
xor    %eax,%eax
mov    0x2e2b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_if_not_exist>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
test   %eax,%eax
je     115e <mkdir_if_not_exist+0x2e>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%eax
jne    117c <mkdir_if_not_exist+0x4c>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %eax,%eax
je     1180 <mkdir_if_not_exist+0x50>
mov    0x2e52(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    119d <mkdir_if_not_exist+0x6d>
neg    %eax
jmp    119d <mkdir_if_not_exist+0x6d>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
mov    0x2e66(%rip),%rdx
xor    %eax,%eax
sub    (%rdx),%eax
xor    %edx,%edx
test   %ecx,%ecx
cmovne %edx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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