#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 */
typedef int /*<<< orphan*/ db_root;

/* Variables and functions */
int /*<<< orphan*/ EX_DATAERR;
scalar_t__ ensure_db_root(char *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int getopt(int, char **, char *);
int optind;
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ usage(char **);
int main(int argc, char *argv[]) {
  char db_root[0x100];
  db_root[0] = '\0';
  int opt;
  while ((opt = getopt(argc, argv, "")) != -1) {
    switch (opt) {
    case '?':
      usage(argv);
    }
  }
  if (optind == (argc - 1))
    strncpy(db_root, argv[optind], sizeof(db_root) - 2);
  else if (optind == argc)
    strncpy(db_root, getenv("HOME"), sizeof(db_root) - 2);
  else
    usage(argv);
  db_root[sizeof(db_root) - 1] = '\0';
  if (ensure_db_root(db_root))
    err(EX_DATAERR, "chdir to %s failed", db_root);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x110(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x114(%rbp)
cmp    $0xffffffff,%eax
je     11c4 <main+0x64>
mov    -0x114(%rbp),%eax
sub    $0x3f,%eax
jne    11bf <main+0x5f>
jmp    11b6 <main+0x56>
mov    -0x10(%rbp),%rdi
call   1070 <usage@plt>
jmp    1180 <main+0x20>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1203 <main+0xa3>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x2df3(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rsi
mov    $0xfe,%edx
call   1040 <strncpy@plt>
jmp    1256 <main+0xf6>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jne    1248 <main+0xe8>
lea    -0x110(%rbp),%rax
mov    %rax,-0x120(%rbp)
lea    0xdd6(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x120(%rbp),%rdi
mov    %rax,%rsi
mov    $0xfe,%edx
call   1040 <strncpy@plt>
jmp    1251 <main+0xf1>
mov    -0x10(%rbp),%rdi
call   1070 <usage@plt>
jmp    1256 <main+0xf6>
movb   $0x0,-0x11(%rbp)
lea    -0x110(%rbp),%rdi
call   1050 <ensure_db_root@plt>
cmp    $0x0,%rax
je     128c <main+0x12c>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xd7e(%rip),%rsi
call   1060 <err@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
movb   $0x0,(%rsp)
lea    0xe87(%rip),%r15
jmp    1188 <main+0x28>
nop
mov    %rbx,%rdi
call   1070 <usage@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getopt@plt>
cmp    $0x3f,%eax
je     1180 <main+0x20>
cmp    $0xffffffff,%eax
jne    1188 <main+0x28>
mov    0x2e39(%rip),%rax
movslq (%rax),%rax
lea    -0x1(%r14),%ecx
cmp    %ecx,%eax
jne    11bb <main+0x5b>
mov    (%rbx,%rax,8),%rsi
mov    %rsp,%rdi
jmp    11d2 <main+0x72>
cmp    %r14d,%eax
jne    11de <main+0x7e>
lea    0xe39(%rip),%rdi
call   1030 <getenv@plt>
mov    %rsp,%rdi
mov    %rax,%rsi
mov    $0xfe,%edx
call   1040 <strncpy@plt>
jmp    11e6 <main+0x86>
mov    %rbx,%rdi
call   1070 <usage@plt>
movb   $0x0,0xff(%rsp)
mov    %rsp,%rdi
call   1050 <ensure_db_root@plt>
test   %rax,%rax
je     1213 <main+0xb3>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
mov    %rsp,%rdx
call   1060 <err@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@optind = global i32 0
@.str.1 = [5 x i8] c"HOME\00"
@EX_DATAERR = global i32 0
@.str.2 = [19 x i8] c"chdir to %s failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
store i8 0, i8* %8
br label %9
9: ; preds = %19, %2
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @getopt(i32 %10, i8** %11, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %7
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* %7
switch i32 %15, label %19 [
i32 63, label %16
]
16: ; preds = %14
%17 = load i8**, i8*** %5
%18 = call i32 @usage(i8** %17)
br label %19
19: ; preds = %16, %14
br label %9
20: ; preds = %9
%21 = load i32, i32* @optind
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = icmp eq i32 %21, %23
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = load i8**, i8*** %5
%28 = load i32, i32* @optind
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = call i32 @strncpy(i8* %26, i8* %31, i32 254)
br label %45
33: ; preds = %20
%34 = load i32, i32* @optind
%35 = load i32, i32* %4
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%39 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @strncpy(i8* %38, i8* %39, i32 254)
br label %44
41: ; preds = %33
%42 = load i8**, i8*** %5
%43 = call i32 @usage(i8** %42)
br label %44
44: ; preds = %41, %37
br label %45
45: ; preds = %44, %25
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 255
store i8 0, i8* %46
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%48 = call i64 @ensure_db_root(i8* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = load i32, i32* @EX_DATAERR
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%53 = call i32 @err(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %50, %45
ret i32 0
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@optind = global i32 0
@.str.1 = [5 x i8] c"HOME\00"
@EX_DATAERR = global i32 0
@.str.2 = [19 x i8] c"chdir to %s failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8 0, i8* %4
br label %5
5: ; preds = %7, %2
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
switch i32 %6, label %7 [
i32 -1, label %10
i32 63, label %8
]
7: ; preds = %5, %8
br label %5
8: ; preds = %5
%9 = tail call i32 @usage(i8** %1)
br label %7
10: ; preds = %5
%11 = load i32, i32* @optind
%12 = add nsw i32 %0, -1
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = sext i32 %11 to i64
%16 = getelementptr inbounds i8*, i8** %1, i64 %15
%17 = load i8*, i8** %16
%18 = call i32 @strncpy(i8* %4, i8* %17, i32 254)
br label %26
19: ; preds = %10
%20 = icmp eq i32 %11, %0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @strncpy(i8* %4, i8* %22, i32 254)
br label %26
24: ; preds = %19
%25 = tail call i32 @usage(i8** %1)
br label %26
26: ; preds = %21, %24, %14
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 255
store i8 0, i8* %27
%28 = call i64 @ensure_db_root(i8* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %26
%31 = load i32, i32* @EX_DATAERR
%32 = call i32 @err(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %33
33: ; preds = %30, %26
ret i32 0
}