#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 */

/* Variables and functions */
int /*<<< orphan*/ X_OK;
int /*<<< orphan*/ access(char const *, int /*<<< orphan*/);
scalar_t__ chdir(char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
const char *find_init(const char *root, const char *user) {
  const char *init_paths[] = {"/sbin/init", "/bin/init", "/etc/init", "/bin/sh",
                              NULL};
  const char **p;
  const char *path;
  if (chdir(root)) {
    perror("chdir");
    exit(1);
  }
  if (user)
    ((void)0);
  if (user && user[0] == '/' && !access(user + 1, X_OK)) {
    path = user;
  } else {
    for (p = init_paths; *p; p++) {
      ((void)0);
      if (!access(*p + 1, X_OK))
        break;
    }
    path = *p;
  }
  chdir("/");
  return path;
}


x86 assembly (clang, -O0):
<find_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0xe89(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe89(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe88(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11c5 <find_init+0x75>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     11d5 <find_init+0x85>
jmp    11d5 <find_init+0x85>
cmpq   $0x0,-0x10(%rbp)
je     121c <find_init+0xcc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    121c <find_init+0xcc>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%eax
jne    121c <find_init+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    127a <find_init+0x12a>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmpq   $0x0,(%rax)
je     126f <find_init+0x11f>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%eax
jne    1259 <find_init+0x109>
jmp    126f <find_init+0x11f>
jmp    125e <find_init+0x10e>
mov    -0x48(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
jmp    1224 <find_init+0xd4>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
lea    0xdac(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x50(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_init>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
movq   $0x0,0x20(%rsp)
lea    0xea4(%rip),%r15
mov    %r15,(%rsp)
lea    0xea4(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xea2(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xea0(%rip),%rax
mov    %rax,0x18(%rsp)
call   1030 <chdir@plt>
test   %rax,%rax
jne    11fc <find_init+0xbc>
test   %r14,%r14
je     11af <find_init+0x6f>
cmpb   $0x2f,(%r14)
jne    11af <find_init+0x6f>
lea    0x1(%r14),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
test   %eax,%eax
je     11e3 <find_init+0xa3>
mov    %rsp,%rbx
mov    0x2e2f(%rip),%r14
nopl   0x0(%rax)
add    $0x1,%r15
mov    (%r14),%esi
mov    %r15,%rdi
call   1040 <access@plt>
test   %eax,%eax
je     11e0 <find_init+0xa0>
mov    0x8(%rbx),%r15
add    $0x8,%rbx
test   %r15,%r15
jne    11c0 <find_init+0x80>
mov    (%rbx),%r14
lea    0xe43(%rip),%rdi
call   1030 <chdir@plt>
mov    %r14,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe24(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/sbin/init\00"
@.str.1 = [10 x i8] c"/bin/init\00"
@.str.2 = [10 x i8] c"/etc/init\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [6 x i8] c"chdir\00"
@X_OK = global i32 0
@.str.5 = [2 x i8] c"/\00"
define i8* @find_init(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [5 x i8*]
%6 = alloca i8**
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = bitcast [5 x i8*]* %5 to i8*
%9 = bitcast i8* %8 to [5 x i8*]*
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i32 0, i32 0), i8** %13
%14 = load i8*, i8** %3
%15 = call i64 @chdir(i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20
br label %24
24: ; preds = %23, %20
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %41
27: ; preds = %24
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 47
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i32, i32* @X_OK
%37 = call i32 @access(i8* %35, i32 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %33
%40 = load i8*, i8** %4
store i8* %40, i8** %7
br label %62
41: ; preds = %33, %27, %24
%42 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 0
store i8** %42, i8*** %6
br label %43
43: ; preds = %56, %41
%44 = load i8**, i8*** %6
%45 = load i8*, i8** %44
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = load i8**, i8*** %6
%49 = load i8*, i8** %48
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = load i32, i32* @X_OK
%52 = call i32 @access(i8* %50, i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %47
br label %59
55: ; preds = %47
br label %56
56: ; preds = %55
%57 = load i8**, i8*** %6
%58 = getelementptr inbounds i8*, i8** %57, i32 1
store i8** %58, i8*** %6
br label %43
59: ; preds = %54, %43
%60 = load i8**, i8*** %6
%61 = load i8*, i8** %60
store i8* %61, i8** %7
br label %62
62: ; preds = %59, %39
%63 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%64 = load i8*, i8** %7
ret i8* %64
}

LLVM IR (-O2):
@.str = [11 x i8] c"/sbin/init\00"
@.str.1 = [10 x i8] c"/bin/init\00"
@.str.2 = [10 x i8] c"/etc/init\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [6 x i8] c"chdir\00"
@X_OK = global i32 0
@.str.5 = [2 x i8] c"/\00"
define i8* @find_init(i8* %0, i8* %1) {
%3 = alloca [5 x i8*]
%4 = bitcast [5 x i8*]* %3 to i8*
%5 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 4
%6 = bitcast i8** %5 to i64*
store i64 0, i64* %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %10
%11 = tail call i64 @chdir(i8* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = icmp eq i8* %1, null
br i1 %17, label %26, label %18
18: ; preds = %16
%19 = load i8, i8* %1
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %1, i64 1
%23 = load i32, i32* @X_OK
%24 = tail call i32 @access(i8* %22, i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %16, %18, %21
br label %27
27: ; preds = %26, %34
%28 = phi i8* [ %36, %34 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %26 ]
%29 = phi i8** [ %35, %34 ], [ %7, %26 ]
%30 = getelementptr inbounds i8, i8* %28, i64 1
%31 = load i32, i32* @X_OK
%32 = tail call i32 @access(i8* %30, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %27
%35 = getelementptr inbounds i8*, i8** %29, i64 1
%36 = load i8*, i8** %35
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %27
38: ; preds = %34, %27
%39 = phi i8** [ %35, %34 ], [ %29, %27 ]
%40 = load i8*, i8** %39
br label %41
41: ; preds = %21, %38
%42 = phi i8* [ %40, %38 ], [ %1, %21 ]
%43 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
ret i8* %42
}