#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 ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
scalar_t__ mkdir(char const *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
void do_mkdir(const char *dir_name, int mode, char *err, size_t err_len) {
  struct stat st_buf;
  int ret;
  memset(&st_buf, 0, sizeof(struct stat));
  if (stat(dir_name, &st_buf) == 0) {
    if (S_ISDIR(st_buf.st_mode) == 0) {
      snprintf(err, err_len,
               "base directory '%s' already exists, "
               "but not as a directory!",
               dir_name);
      return;
    }
    return;
  }
  ret = errno;
  if (ret != ENOENT) {
    snprintf(err, err_len, "error trying to stat '%s': error %d\n", dir_name,
             ret);
    return;
  }
  if (mkdir(dir_name, mode)) {
    snprintf(err, err_len, "error trying to create '%s': error %d\n", dir_name,
             ret);
    return;
  }
}


x86 assembly (clang, -O0):
<do_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11c4 <do_mkdir+0x74>
mov    -0x28(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    11bf <do_mkdir+0x6f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe4d(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1239 <do_mkdir+0xe9>
jmp    1239 <do_mkdir+0xe9>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%eax
je     1205 <do_mkdir+0xb5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
lea    0xe43(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1239 <do_mkdir+0xe9>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     1239 <do_mkdir+0xe9>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
lea    0xe2f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mkdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11b9 <do_mkdir+0x69>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e34(%rip),%rax
cmp    (%rax),%ebp
jne    11e0 <do_mkdir+0x90>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11fa <do_mkdir+0xaa>
lea    0xeaa(%rip),%rdx
jmp    11e7 <do_mkdir+0x97>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
jne    11fa <do_mkdir+0xaa>
lea    0xe32(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    11fa <do_mkdir+0xaa>
lea    0xe55(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [60 x i8] c"base directory '%s' already exists, but not as a directory!\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.1 = [37 x i8] c"error trying to stat '%s': error %d\0A\00"
@.str.2 = [39 x i8] c"error trying to create '%s': error %d\0A\00"
define void @do_mkdir(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%11 = call i32 @memset(%struct.stat* %9, i32 0, i32 4)
%12 = load i8*, i8** %5
%13 = call i64 @stat(i8* %12, %struct.stat* %9)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %4
%16 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISDIR(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = load i8*, i8** %5
%24 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %21, i64 %22, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %48
25: ; preds = %15
br label %48
26: ; preds = %4
%27 = load i32, i32* @errno
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = load i32, i32* @ENOENT
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i8*, i8** %7
%33 = load i64, i64* %8
%34 = load i8*, i8** %5
%35 = load i32, i32* %10
%36 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %32, i64 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %34, i32 %35)
br label %48
37: ; preds = %26
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = call i64 @mkdir(i8* %38, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = load i64, i64* %8
%45 = load i8*, i8** %5
%46 = load i32, i32* %10
%47 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %43, i64 %44, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %45, i32 %46)
br label %48
48: ; preds = %20, %25, %31, %42, %37
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [60 x i8] c"base directory '%s' already exists, but not as a directory!\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.1 = [37 x i8] c"error trying to stat '%s': error %d\0A\00"
@.str.2 = [39 x i8] c"error trying to create '%s': error %d\0A\00"
define void @do_mkdir(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = call i32 @memset(%struct.stat* %5, i32 0, i32 4)
%8 = call i64 @stat(i8* %0, %struct.stat* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %4
%11 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %10
%16 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %28
17: ; preds = %4
%18 = load i32, i32* @errno
%19 = load i32, i32* @ENOENT
%20 = icmp eq i32 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %18)
br label %28
23: ; preds = %17
%24 = call i64 @mkdir(i8* %0, i32 %1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %18)
br label %28
28: ; preds = %23, %10, %26, %21, %15
ret void
}