#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 st_mode;
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ geteuid();
int /*<<< orphan*/ log_error(char *);
int /*<<< orphan*/ log_perror(char *);
scalar_t__ mkdir(char *, int);
char *printf_dup(char *, char const *, char const *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char const *);
int checkdir(const char *dir, const char *subdir, int create) {
  char *path;
  int res = 0;
  if (subdir)
    path = printf_dup("%s/%s", dir, subdir);
  else
    path = strdup(dir);
  if (!path)
    return -1;
  struct stat st;
  int do_create = 0;
  if (stat(path, &st)) {
    if (create && errno == ENOENT) {
      do_create = 1;
    } else {
      log_perror("stat");
      goto err;
    }
  }
  if (!S_ISDIR(st.st_mode)) {
    log_error("expected a dir but got a file");
    goto err;
  }
  if (do_create) {
    if (mkdir(path, 0700)) {
      log_perror("mkdir");
      goto err;
    }
    if (stat(path, &st)) {
      log_perror("stat (2)");
      goto err;
    }
  }
  if (st.st_mode & 0077 || st.st_uid != geteuid()) {
    log_error("bad permissions");
    goto err;
  }
  goto ok;
err:
  res = -1;
ok:
  free(path);
  return res;
}


x86 assembly (clang, -O0):
<checkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d2 <checkdir+0x42>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe3c(%rip),%rdi
call   1070 <printf_dup@plt>
mov    %rax,-0x28(%rbp)
jmp    11df <checkdir+0x4f>
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f6 <checkdir+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <checkdir+0x1a6>
movl   $0x0,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     125a <checkdir+0xca>
cmpl   $0x0,-0x1c(%rbp)
je     1244 <checkdir+0xb4>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <checkdir+0xb4>
movl   $0x1,-0x44(%rbp)
jmp    1255 <checkdir+0xc5>
lea    0xdbb(%rip),%rdi
call   10a0 <log_perror@plt>
jmp    1320 <checkdir+0x190>
jmp    125a <checkdir+0xca>
mov    -0x40(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    127c <checkdir+0xec>
lea    0xd99(%rip),%rdi
call   1050 <log_error@plt>
jmp    1320 <checkdir+0x190>
cmpl   $0x0,-0x44(%rbp)
je     12dc <checkdir+0x14c>
mov    -0x28(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12af <checkdir+0x11f>
lea    0xd84(%rip),%rdi
call   10a0 <log_perror@plt>
jmp    1320 <checkdir+0x190>
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     12d7 <checkdir+0x147>
lea    0xd62(%rip),%rdi
call   10a0 <log_perror@plt>
jmp    1320 <checkdir+0x190>
jmp    12dc <checkdir+0x14c>
mov    -0x40(%rbp),%eax
and    $0x3f,%eax
cmp    $0x0,%eax
jne    130a <checkdir+0x17a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
je     131b <checkdir+0x18b>
lea    0xd27(%rip),%rdi
call   1050 <log_error@plt>
jmp    1320 <checkdir+0x190>
jmp    1327 <checkdir+0x197>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkdir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%rdx
mov    %rdi,%rsi
test   %rdx,%rdx
je     11b3 <checkdir+0x23>
lea    0xe54(%rip),%rdi
call   1070 <printf_dup@plt>
jmp    11bb <checkdir+0x2b>
mov    %rsi,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1219 <checkdir+0x89>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %rax,%rax
je     122e <checkdir+0x9e>
test   %ebp,%ebp
je     1220 <checkdir+0x90>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <checkdir+0x90>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     125c <checkdir+0xcc>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1280 <checkdir+0xf0>
lea    0xe17(%rip),%rdi
call   10a0 <log_perror@plt>
jmp    1268 <checkdir+0xd8>
mov    $0xffffffff,%ebp
jmp    1275 <checkdir+0xe5>
lea    0xddf(%rip),%rdi
call   10a0 <log_perror@plt>
jmp    1268 <checkdir+0xd8>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     125c <checkdir+0xcc>
testb  $0x3f,(%rsp)
jne    1253 <checkdir+0xc3>
mov    0x8(%rsp),%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <geteuid@plt>
cmp    %rax,%r14
je     126d <checkdir+0xdd>
lea    0xdde(%rip),%rdi
jmp    1263 <checkdir+0xd3>
lea    0xda8(%rip),%rdi
call   1050 <log_error@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %rax,%rax
je     123a <checkdir+0xaa>
lea    0xd98(%rip),%rdi
call   10a0 <log_perror@plt>
jmp    1268 <checkdir+0xd8>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@.str = [6 x i8] c"%s/%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [30 x i8] c"expected a dir but got a file\00"
@.str.3 = [6 x i8] c"mkdir\00"
@.str.4 = [9 x i8] c"stat (2)\00"
@.str.5 = [16 x i8] c"bad permissions\00"
define i32 @checkdir(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = call i8* @printf_dup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
store i8* %17, i8** %8
br label %21
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i8* @strdup(i8* %19)
store i8* %20, i8** %8
br label %21
21: ; preds = %18, %14
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21
store i32 -1, i32* %4
br label %81
25: ; preds = %21
store i32 0, i32* %11
%26 = load i8*, i8** %8
%27 = call i64 @stat(i8* %26, %struct.stat* %10)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 1, i32* %11
br label %39
37: ; preds = %32, %29
%38 = call i32 @log_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %76
39: ; preds = %36
br label %40
40: ; preds = %39, %25
%41 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @S_ISDIR(i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %40
%46 = call i32 @log_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %76
47: ; preds = %40
%48 = load i32, i32* %11
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %63
50: ; preds = %47
%51 = load i8*, i8** %8
%52 = call i64 @mkdir(i8* %51, i32 448)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @log_perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %76
56: ; preds = %50
%57 = load i8*, i8** %8
%58 = call i64 @stat(i8* %57, %struct.stat* %10)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @log_perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %76
62: ; preds = %56
br label %63
63: ; preds = %62, %47
%64 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%65 = load i32, i32* %64
%66 = and i32 %65, 63
%67 = icmp ne i32 %66, 0
br i1 %67, label %73, label %68
68: ; preds = %63
%69 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%70 = load i64, i64* %69
%71 = call i64 (...) @geteuid()
%72 = icmp ne i64 %70, %71
br i1 %72, label %73, label %75
73: ; preds = %68, %63
%74 = call i32 @log_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %76
75: ; preds = %68
br label %77
76: ; preds = %73, %60, %54, %45, %37
store i32 -1, i32* %9
br label %77
77: ; preds = %76, %75
%78 = load i8*, i8** %8
%79 = call i32 @free(i8* %78)
%80 = load i32, i32* %9
store i32 %80, i32* %4
br label %81
81: ; preds = %77, %24
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@.str = [6 x i8] c"%s/%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [30 x i8] c"expected a dir but got a file\00"
@.str.3 = [6 x i8] c"mkdir\00"
@.str.4 = [9 x i8] c"stat (2)\00"
@.str.5 = [16 x i8] c"bad permissions\00"
define i32 @checkdir(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = icmp eq i8* %1, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i8* @printf_dup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %10
8: ; preds = %3
%9 = tail call i8* @strdup(i8* %0)
br label %10
10: ; preds = %8, %6
%11 = phi i8* [ %7, %6 ], [ %9, %8 ]
%12 = icmp eq i8* %11, null
br i1 %12, label %62, label %13
13: ; preds = %10
%14 = bitcast %struct.stat* %4 to i8*
%15 = call i64 @stat(i8* %11, %struct.stat* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = icmp eq i32 %2, 0
br i1 %18, label %23, label %19
19: ; preds = %17
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOENT
%22 = icmp eq i64 %20, %21
br i1 %22, label %30, label %23
23: ; preds = %19, %17
%24 = call i32 @log_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %59
25: ; preds = %13
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %35, label %47
30: ; preds = %19
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @S_ISDIR(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30, %25
%36 = call i32 @log_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %59
37: ; preds = %30
%38 = call i64 @mkdir(i8* %11, i32 448)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @log_perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %59
42: ; preds = %37
%43 = call i64 @stat(i8* %11, %struct.stat* %4)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 @log_perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %59
47: ; preds = %25, %42
%48 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%49 = load i32, i32* %48
%50 = and i32 %49, 63
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %47
%53 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%54 = load i64, i64* %53
%55 = call i64 (...) @geteuid()
%56 = icmp eq i64 %54, %55
br i1 %56, label %59, label %57
57: ; preds = %52, %47
%58 = call i32 @log_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %23, %35, %40, %45, %57, %52
%60 = phi i32 [ 0, %52 ], [ -1, %57 ], [ -1, %45 ], [ -1, %40 ], [ -1, %35 ], [ -1, %23 ]
%61 = call i32 @free(i8* %11)
br label %62
62: ; preds = %10, %59
%63 = phi i32 [ %60, %59 ], [ -1, %10 ]
ret i32 %63
}