#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*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
char *root_device();
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char *root = root_device();
  int n = 2;
  if (root == NULL) {
    write(2, "substroot: unable to find root device.\n", 39);
    exit(255);
  }
  if (argc < 2) {
    write(2, "substroot app ...\n", 18);
    exit(255);
  }
  while (n < argc) {
    if (strcmp(argv[n], "%") == 0)
      argv[n] = root;
    n++;
  }
  execvp(argv[1], argv + 1);
  perror(argv[1]);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <root_device@plt>
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <main+0x53>
mov    $0x2,%edi
lea    0xe61(%rip),%rsi
mov    $0x27,%edx
call   1030 <write@plt>
mov    $0xff,%edi
call   1080 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11dd <main+0x7d>
mov    $0x2,%edi
lea    0xe5f(%rip),%rsi
mov    $0x12,%edx
call   1030 <write@plt>
mov    $0xff,%edi
call   1080 <exit@plt>
jmp    11e2 <main+0x82>
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    122e <main+0xce>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe3a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1220 <main+0xc0>
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1070 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <root_device@plt>
test   %rax,%rax
jne    118c <main+0x2c>
lea    0xe80(%rip),%rsi
mov    $0x2,%edi
mov    $0x27,%edx
jmp    11a2 <main+0x42>
cmp    $0x2,%ebp
jge    11b1 <main+0x51>
lea    0xe90(%rip),%rsi
mov    $0x2,%edi
mov    $0x12,%edx
call   1030 <write@plt>
mov    $0xff,%edi
call   1080 <exit@plt>
jne    11d6 <main+0x76>
lea    0x8(%r12),%rsi
mov    0x8(%r12),%rdi
call   1070 <execvp@plt>
mov    0x8(%r12),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%r14
mov    %ebp,%ebp
mov    $0x2,%ebx
lea    0xe54(%rip),%r15
jmp    11f9 <main+0x99>
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11b3 <main+0x53>
mov    (%r12,%rbx,8),%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11f0 <main+0x90>
mov    %r14,(%r12,%rbx,8)
jmp    11f0 <main+0x90>

LLVM IR (-O0):
@.str = [40 x i8] c"substroot: unable to find root device.\0A\00"
@.str.1 = [19 x i8] c"substroot app ...\0A\00"
@.str.2 = [2 x i8] c"%\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i8* (...) @root_device()
store i8* %8, i8** %6
store i32 2, i32* %7
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @write(i32 2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 39)
%13 = call i32 @exit(i32 255)
unreachable
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 18)
%19 = call i32 @exit(i32 255)
unreachable
20: ; preds = %14
br label %21
21: ; preds = %39, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %42
25: ; preds = %21
%26 = load i8**, i8*** %5
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %25
%34 = load i8*, i8** %6
%35 = load i8**, i8*** %5
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %35, i64 %37
store i8* %34, i8** %38
br label %39
39: ; preds = %33, %25
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %21
42: ; preds = %21
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = call i32 @execvp(i8* %45, i8** %47)
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i32 @perror(i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [40 x i8] c"substroot: unable to find root device.\0A\00"
@.str.1 = [19 x i8] c"substroot app ...\0A\00"
@.str.2 = [2 x i8] c"%\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i8* (...) @root_device()
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 39)
%7 = tail call i32 @exit(i32 255)
unreachable
8: ; preds = %2
%9 = icmp slt i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = icmp eq i32 %0, 2
br i1 %11, label %27, label %12
12: ; preds = %10
%13 = zext i32 %0 to i64
br label %17
14: ; preds = %8
%15 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 18)
%16 = tail call i32 @exit(i32 255)
unreachable
17: ; preds = %12, %24
%18 = phi i64 [ 2, %12 ], [ %25, %24 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 %18
%20 = load i8*, i8** %19
%21 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i8* %3, i8** %19
br label %24
24: ; preds = %23, %17
%25 = add nuw nsw i64 %18, 1
%26 = icmp eq i64 %25, %13
br i1 %26, label %27, label %17
27: ; preds = %24, %10
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = tail call i32 @execvp(i8* %29, i8** %28)
%31 = load i8*, i8** %28
%32 = tail call i32 @perror(i8* %31)
%33 = tail call i32 @exit(i32 1)
unreachable
}