#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 */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int ddbm_mkdir(char *dir, int create) {
  struct stat sbuf;
  if (stat(dir, &sbuf) == 0)
    if (S_ISDIR(sbuf.st_mode))
      return 0;
    else
      return -1;
  else if (!create)
    return -1;
  else if (mkdir(dir, 0755) < 0)
    return -1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<ddbm_mkdir>:
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
jne    1180 <ddbm_mkdir+0x50>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1174 <ddbm_mkdir+0x44>
movl   $0x0,-0x4(%rbp)
jmp    11c1 <ddbm_mkdir+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <ddbm_mkdir+0x91>
cmpl   $0x0,-0x14(%rbp)
jne    1196 <ddbm_mkdir+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <ddbm_mkdir+0x91>
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    11ba <ddbm_mkdir+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <ddbm_mkdir+0x91>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ddbm_mkdir>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     115c <ddbm_mkdir+0x2c>
test   %ebp,%ebp
je     116c <ddbm_mkdir+0x3c>
mov    %rbx,%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
sar    $0x3f,%rax
jmp    1171 <ddbm_mkdir+0x41>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
jmp    1171 <ddbm_mkdir+0x41>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @ddbm_mkdir(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 eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 0, i32* %3
br label %27
16: ; preds = %10
store i32 -1, i32* %3
br label %27
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %17
store i32 -1, i32* %3
br label %27
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = call i64 @mkdir(i8* %22, i32 493)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %3
br label %27
26: ; preds = %21
store i32 0, i32* %3
br label %27
27: ; preds = %26, %25, %20, %16, %15
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @ddbm_mkdir(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 %7, label %13
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i64 @S_ISDIR(i32 %9)
%11 = icmp eq i64 %10, 0
%12 = sext i1 %11 to i32
br label %19
13: ; preds = %2
%14 = icmp eq i32 %1, 0
br i1 %14, label %19, label %15
15: ; preds = %13
%16 = call i64 @mkdir(i8* %0, i32 493)
%17 = ashr i64 %16, 63
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %15, %13, %7
%20 = phi i32 [ %12, %7 ], [ -1, %13 ], [ %18, %15 ]
ret i32 %20
}