#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*/ F_OK;
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int enter_docroot(char *path) {
  struct stat path_stat;
  stat(path, &path_stat);
  if (access(path, F_OK)) {
    perror(path);
    return 1;
  } else if (access(path, R_OK)) {
    perror(path);
    return 1;
  } else if (!S_ISDIR(path_stat.st_mode)) {
    fprintf(stderr, "%s: Not a directory\n", path);
    return 1;
  }
  if (chdir(path))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<enter_docroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     11aa <enter_docroot+0x4a>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1241 <enter_docroot+0xe1>
mov    -0x10(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     11db <enter_docroot+0x7b>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1241 <enter_docroot+0xe1>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1211 <enter_docroot+0xb1>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe00(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1241 <enter_docroot+0xe1>
jmp    1216 <enter_docroot+0xb6>
jmp    121b <enter_docroot+0xbb>
mov    -0x10(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     123a <enter_docroot+0xda>
movl   $0x1,-0x4(%rbp)
jmp    1241 <enter_docroot+0xe1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enter_docroot>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    11da <enter_docroot+0x7a>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    11da <enter_docroot+0x7a>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11c0 <enter_docroot+0x60>
mov    %rbx,%rdi
call   1030 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
jmp    11e7 <enter_docroot+0x87>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
jmp    11e2 <enter_docroot+0x82>
mov    %rbx,%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@F_OK = global i32 0
@R_OK = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: Not a directory\0A\00"
define i32 @enter_docroot(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @stat(i8* %5, %struct.stat* %4)
%7 = load i8*, i8** %3
%8 = load i32, i32* @F_OK
%9 = call i64 @access(i8* %7, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @perror(i8* %12)
store i32 1, i32* %2
br label %39
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i32, i32* @R_OK
%17 = call i64 @access(i8* %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = call i32 @perror(i8* %20)
store i32 1, i32* %2
br label %39
22: ; preds = %14
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @S_ISDIR(i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %29)
store i32 1, i32* %2
br label %39
31: ; preds = %22
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32
%34 = load i8*, i8** %3
%35 = call i64 @chdir(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 1, i32* %2
br label %39
38: ; preds = %33
store i32 0, i32* %2
br label %39
39: ; preds = %38, %37, %27, %19, %11
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@F_OK = global i32 0
@R_OK = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: Not a directory\0A\00"
define i32 @enter_docroot(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = load i32, i32* @F_OK
%6 = tail call i64 @access(i8* %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @perror(i8* %0)
br label %28
10: ; preds = %1
%11 = load i32, i32* @R_OK
%12 = tail call i64 @access(i8* %0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* %0)
br label %28
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @S_ISDIR(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %28
24: ; preds = %16
%25 = tail call i64 @chdir(i8* %0)
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %24, %21, %14, %8
%29 = phi i32 [ 1, %8 ], [ 1, %14 ], [ 1, %21 ], [ %27, %24 ]
ret i32 %29
}