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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ dbg(int, char *, char const *);
scalar_t__ errno;
scalar_t__ mkdir(char const *, int);
scalar_t__ stat(char const *, struct stat *);
char const *strerror(scalar_t__);
int chk_and_prep_bak_dir(const char *dirname) {
  struct stat st = {0};
  if (stat(dirname, &st) != 0) {
    if (errno == ENOENT) {
      if (mkdir(dirname, 0777) != 0) {
        dbg((1 << 0), "Create directory: %s failed.", dirname);
        return -1;
      }
    } else {
      dbg((1 << 0), "Invoke stat() failed: %s.", strerror(errno));
      return -1;
    }
  } else {
    if (!S_ISDIR(st.st_mode)) {
      dbg((1 << 0), "%s has been existed but not a directory.", dirname);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<chk_and_prep_bak_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     121f <chk_and_prep_bak_dir+0xbf>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <chk_and_prep_bak_dir+0x8b>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11e6 <chk_and_prep_bak_dir+0x86>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe2b(%rip),%rsi
call   1060 <dbg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <chk_and_prep_bak_dir+0xfd>
jmp    121a <chk_and_prep_bak_dir+0xba>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xe14(%rip),%rsi
call   1060 <dbg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <chk_and_prep_bak_dir+0xfd>
jmp    1256 <chk_and_prep_bak_dir+0xf6>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1251 <chk_and_prep_bak_dir+0xf1>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdf7(%rip),%rsi
call   1060 <dbg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <chk_and_prep_bak_dir+0xfd>
jmp    1256 <chk_and_prep_bak_dir+0xf6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chk_and_prep_bak_dir>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11a1 <chk_and_prep_bak_dir+0x51>
mov    0x2e69(%rip),%rax
mov    (%rax),%rdi
mov    0x2e4f(%rip),%rax
cmp    (%rax),%rdi
jne    11c3 <chk_and_prep_bak_dir+0x73>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11e7 <chk_and_prep_bak_dir+0x97>
lea    0xe61(%rip),%rsi
jmp    11b9 <chk_and_prep_bak_dir+0x69>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11e1 <chk_and_prep_bak_dir+0x91>
lea    0xe7e(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
jmp    11d7 <chk_and_prep_bak_dir+0x87>
call   1070 <strerror@plt>
lea    0xe4e(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rdx
call   1050 <dbg@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
jmp    11e1 <chk_and_prep_bak_dir+0x91>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [29 x i8] c"Create directory: %s failed.\00"
@.str.1 = [26 x i8] c"Invoke stat() failed: %s.\00"
@.str.2 = [41 x i8] c"%s has been existed but not a directory.\00"
define i32 @chk_and_prep_bak_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i8*, i8** %3
%7 = call i64 @stat(i8* %6, %struct.stat* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %21
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i64 @mkdir(i8* %14, i32 511)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 @dbg(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %2
br label %36
20: ; preds = %13
br label %25
21: ; preds = %9
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 @dbg(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 -1, i32* %2
br label %36
25: ; preds = %20
br label %35
26: ; preds = %1
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, 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 i8*, i8** %3
%33 = call i32 @dbg(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %2
br label %36
34: ; preds = %26
br label %35
35: ; preds = %34, %25
store i32 0, i32* %2
br label %36
36: ; preds = %35, %31, %21, %17
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [29 x i8] c"Create directory: %s failed.\00"
@.str.1 = [26 x i8] c"Invoke stat() failed: %s.\00"
@.str.2 = [41 x i8] c"%s has been existed but not a directory.\00"
define i32 @chk_and_prep_bak_dir(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.stat*
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = call i64 @mkdir(i8* %0, i32 511)
%13 = icmp eq i64 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %11
%15 = call i32 @dbg(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %26
16: ; preds = %7
%17 = call i8* @strerror(i64 %8)
%18 = call i32 @dbg(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %26
19: ; preds = %1
%20 = bitcast i64* %2 to i32*
%21 = load i32, i32* %20
%22 = call i32 @S_ISDIR(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @dbg(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %26
26: ; preds = %11, %19, %24, %16, %14
%27 = phi i32 [ -1, %14 ], [ -1, %16 ], [ -1, %24 ], [ 0, %19 ], [ 0, %11 ]
ret i32 %27
}