#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int stat(char *, struct stat *);
char *open_documentroot(int argc, char **argv) {
  if (argc != 2) {
    printf("usage: ./sushila DIRECTORY\n");
    exit(1);
  }
  struct stat s;
  int err = stat(argv[1], &s);
  if (-1 == err) {
    if (ENOENT == errno) {
      perror("Repository error");
      exit(1);
    } else {
      perror("stat");
      exit(1);
    }
  } else {
    if (!S_ISDIR(s.st_mode)) {
      perror("Document root error");
      exit(1);
    }
  }
  return argv[1];
}


x86 assembly (clang, -O0):
<open_documentroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     117f <open_documentroot+0x2f>
lea    0xe90(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11e7 <open_documentroot+0x97>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    11d1 <open_documentroot+0x81>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    120e <open_documentroot+0xbe>
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1213 <open_documentroot+0xc3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_documentroot>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1187 <open_documentroot+0x37>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     119d <open_documentroot+0x4d>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11bc <open_documentroot+0x6c>
mov    0x8(%rbx),%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <open_documentroot+0x75>
lea    0xe46(%rip),%rdi
jmp    11cc <open_documentroot+0x7c>
lea    0xe53(%rip),%rdi
jmp    11cc <open_documentroot+0x7c>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [28 x i8] c"usage: ./sushila DIRECTORY\0A\00"
@ENOENT = global i64 0
@errno = global i64 0
@.str.1 = [17 x i8] c"Repository error\00"
@.str.2 = [5 x i8] c"stat\00"
@.str.3 = [20 x i8] c"Document root error\00"
define i8* @open_documentroot(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.stat
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @stat(i8* %15, %struct.stat* %5)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %29
19: ; preds = %12
%20 = load i64, i64* @ENOENT
%21 = load i64, i64* @errno
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %12
%30 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISDIR(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
br label %38
38: ; preds = %37
%39 = load i8**, i8*** %4
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
ret i8* %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOENT = global i64 0
@errno = global i64 0
@.str.1 = [17 x i8] c"Repository error\00"
@.str.2 = [5 x i8] c"stat\00"
@.str.3 = [20 x i8] c"Document root error\00"
@str = [27 x i8] c"usage: ./sushila DIRECTORY\00"
define i8* @open_documentroot(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = bitcast %struct.stat* %3 to i8*
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @stat(i8* %11, %struct.stat* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %24
14: ; preds = %8
%15 = load i64, i64* @ENOENT
%16 = load i64, i64* @errno
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %8
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i32 @S_ISDIR(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = load i8*, i8** %10
ret i8* %33
}