#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*/ EXIT_FAILURE;
int /*<<< orphan*/ S_ISDIR(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
scalar_t__ getuid();
scalar_t__ mkdir(char const *, int);
scalar_t__ stat(char const *, struct stat *);
void config_ensure_directory(const char *path) {
  struct stat sb;
  if (stat(path, &sb) != 0) {
    if (errno == ENOENT) {
      if (mkdir(path, 0700) != 0) {
        err(EXIT_FAILURE, "can't create %s", path);
      }
      if (stat(path, &sb) != 0) {
        err(EXIT_FAILURE,
            "can't stat newly created "
            "file %s",
            path);
      }
    } else {
      err(EXIT_FAILURE, "can't access %s", path);
    }
  }
  if (!S_ISDIR(sb.st_mode))
    errx(EXIT_FAILURE, "%s is not a directory", path);
  if (sb.st_uid != 0 && sb.st_uid != getuid())
    errx(EXIT_FAILURE, "bad owner on %s", path);
  if ((sb.st_mode & 0077) != 0)
    errx(EXIT_FAILURE, "bad permissions on %s", path);
}


x86 assembly (clang, -O0):
<config_ensure_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1221 <config_ensure_directory+0xc1>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <config_ensure_directory+0xa3>
mov    -0x8(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11ce <config_ensure_directory+0x6e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1080 <err@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11fe <config_ensure_directory+0x9e>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe17(%rip),%rsi
call   1080 <err@plt>
jmp    121c <config_ensure_directory+0xbc>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1080 <err@plt>
jmp    1221 <config_ensure_directory+0xc1>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    124b <config_ensure_directory+0xeb>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdfb(%rip),%rsi
call   1050 <errx@plt>
cmpq   $0x0,-0x10(%rbp)
je     128e <config_ensure_directory+0x12e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     128e <config_ensure_directory+0x12e>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdce(%rip),%rsi
call   1050 <errx@plt>
mov    -0x18(%rbp),%eax
and    $0x3f,%eax
cmp    $0x0,%eax
je     12b6 <config_ensure_directory+0x156>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb6(%rip),%rsi
call   1050 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_ensure_directory>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11ec <config_ensure_directory+0x8c>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11d4 <config_ensure_directory+0x74>
mov    %r14,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11b9 <config_ensure_directory+0x59>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    %r14,%rdx
call   1080 <err@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     11ec <config_ensure_directory+0x8c>
lea    0xe3e(%rip),%rsi
jmp    11db <config_ensure_directory+0x7b>
lea    0xe56(%rip),%rsi
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
call   1080 <err@plt>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
jne    1211 <config_ensure_directory+0xb1>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    %r14,%rdx
call   1050 <errx@plt>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     123f <config_ensure_directory+0xdf>
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    %rax,%rbx
je     123f <config_ensure_directory+0xdf>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    %r14,%rdx
call   1050 <errx@plt>
testb  $0x3f,0x8(%rsp)
je     125e <config_ensure_directory+0xfe>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    %r14,%rdx
call   1050 <errx@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@ENOENT = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"can't create %s\00"
@.str.1 = [33 x i8] c"can't stat newly created file %s\00"
@.str.2 = [16 x i8] c"can't access %s\00"
@.str.3 = [22 x i8] c"%s is not a directory\00"
@.str.4 = [16 x i8] c"bad owner on %s\00"
@.str.5 = [22 x i8] c"bad permissions on %s\00"
define void @config_ensure_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @stat(i8* %4, %struct.stat* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %33
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 %28
11: ; preds = %7
%12 = load i8*, i8** %2
%13 = call i64 @mkdir(i8* %12, i32 448)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @EXIT_FAILURE
%17 = load i8*, i8** %2
%18 = call i32 @err(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %11
%20 = load i8*, i8** %2
%21 = call i64 @stat(i8* %20, %struct.stat* %3)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @EXIT_FAILURE
%25 = load i8*, i8** %2
%26 = call i32 @err(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %23, %19
br label %32
28: ; preds = %7
%29 = load i32, i32* @EXIT_FAILURE
%30 = load i8*, i8** %2
%31 = call i32 @err(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %28, %27
br label %33
33: ; preds = %32, %1
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i32, i32* @EXIT_FAILURE
%40 = load i8*, i8** %2
%41 = call i32 @errx(i32 %39, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38, %33
%43 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%44 = load i64, i64* %43
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %42
%47 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%48 = load i64, i64* %47
%49 = call i64 (...) @getuid()
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = load i32, i32* @EXIT_FAILURE
%53 = load i8*, i8** %2
%54 = call i32 @errx(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %46, %42
%56 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%57 = load i32, i32* %56
%58 = and i32 %57, 63
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = load i32, i32* @EXIT_FAILURE
%62 = load i8*, i8** %2
%63 = call i32 @errx(i32 %61, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %60, %55
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@ENOENT = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"can't create %s\00"
@.str.1 = [33 x i8] c"can't stat newly created file %s\00"
@.str.2 = [16 x i8] c"can't access %s\00"
@.str.3 = [22 x i8] c"%s is not a directory\00"
@.str.4 = [16 x i8] c"bad owner on %s\00"
@.str.5 = [22 x i8] c"bad permissions on %s\00"
define void @config_ensure_directory(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %19
10: ; preds = %6
%11 = call i64 @mkdir(i8* %0, i32 448)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @err(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %13, %10
%17 = call i64 @stat(i8* %0, %struct.stat* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %6, %16
%20 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %6 ]
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @err(i32 %21, i8* %20, i8* %0)
br label %23
23: ; preds = %19, %16, %1
%24 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISDIR(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @errx(i32 %29, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %28, %23
%32 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%33 = load i64, i64* %32
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %31
%36 = call i64 (...) @getuid()
%37 = icmp eq i64 %33, %36
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @errx(i32 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %41
41: ; preds = %38, %35, %31
%42 = load i32, i32* %24
%43 = and i32 %42, 63
%44 = icmp eq i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @errx(i32 %46, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %48
48: ; preds = %45, %41
ret void
}