#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 /*<<< orphan*/ EX_DATAERR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int chdir(char *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char *);
scalar_t__ stat(char *, struct stat *);
int ensure_db_root(char *db_root) {
  struct stat sb;
  if (stat(db_root, &sb))
    err(EX_DATAERR, "can't stat db_root: %s", db_root);
  if (!S_ISDIR(sb.st_mode))
    errx(EX_DATAERR, "db_root not a directory: %s", db_root);
  return chdir(db_root);
}


x86 assembly (clang, -O0):
<ensure_db_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     118c <ensure_db_root+0x3c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe79(%rip),%rsi
call   1070 <err@plt>
mov    -0x10(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11b6 <ensure_db_root+0x66>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1040 <errx@plt>
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_db_root>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     117f <ensure_db_root+0x2f>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
mov    %rbx,%rdx
call   1070 <err@plt>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
jne    11a4 <ensure_db_root+0x54>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdx
call   1040 <errx@plt>
mov    %rbx,%rdi
call   1030 <chdir@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@EX_DATAERR = global i32 0
@.str = [23 x i8] c"can't stat db_root: %s\00"
@.str.1 = [28 x i8] c"db_root not a directory: %s\00"
define i32 @ensure_db_root(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 %11
7: ; preds = %1
%8 = load i32, i32* @EX_DATAERR
%9 = load i8*, i8** %2
%10 = call i32 @err(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %7, %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @S_ISDIR(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = load i32, i32* @EX_DATAERR
%18 = load i8*, i8** %2
%19 = call i32 @errx(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %16, %11
%21 = load i8*, i8** %2
%22 = call i32 @chdir(i8* %21)
ret i32 %22
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EX_DATAERR = global i32 0
@.str = [23 x i8] c"can't stat db_root: %s\00"
@.str.1 = [28 x i8] c"db_root not a directory: %s\00"
define i32 @ensure_db_root(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 %9, label %6
6: ; preds = %1
%7 = load i32, i32* @EX_DATAERR
%8 = call i32 @err(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %6, %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @S_ISDIR(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @EX_DATAERR
%16 = call i32 @errx(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %14, %9
%18 = call i32 @chdir(i8* %0)
ret i32 %18
}