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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ printf(char *, ...);
size_t readline(int, char *, int);
int /*<<< orphan*/ strcmp(char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
size_t execline(int fd) {
  char cmd[1024];
  size_t cmd_len = readline(fd, cmd, 1024);
  char *cmd_argv[100];
  size_t cmd_pos = 0;
  cmd_argv[cmd_pos++] = strtok(cmd, " ");
  do
    cmd_argv[cmd_pos] = strtok(NULL, " ");
  while (cmd_argv[cmd_pos++]);
  if (!strcmp(cmd_argv[0], "exit") || !strcmp(cmd_argv[0], "quit"))
    exit(0);
  pid_t pid = fork();
  if (!pid && execvp(cmd_argv[0], cmd_argv) < 0) {
    printf("error: command `%s` does not exist\n", cmd_argv[0]);
    exit(0);
  } else {
    int status;
    waitpid(pid, &status, 0);
    if (WIFEXITED(status))
      printf("%d\n", WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<execline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x750,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <readline@plt>
mov    %rax,-0x418(%rbp)
movq   $0x0,-0x748(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,%rcx
mov    -0x748(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x748(%rbp)
mov    %rcx,-0x740(%rbp,%rax,8)
ff
xor    %eax,%eax
mov    %eax,%edi
lea    0xdee(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,%rcx
mov    -0x748(%rbp),%rax
mov    %rcx,-0x740(%rbp,%rax,8)
ff
mov    -0x748(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x748(%rbp)
cmpq   $0x0,-0x740(%rbp,%rax,8)
jne    1207 <execline+0x67>
mov    -0x740(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     1285 <execline+0xe5>
mov    -0x740(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    128c <execline+0xec>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x74c(%rbp)
cmpl   $0x0,-0x74c(%rbp)
jne    12df <execline+0x13f>
mov    -0x740(%rbp),%rdi
lea    -0x740(%rbp),%rsi
call   1090 <execvp@plt>
cmp    $0x0,%rax
jge    12df <execline+0x13f>
mov    -0x740(%rbp),%rsi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x74c(%rbp),%edi
lea    -0x750(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    -0x750(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1323 <execline+0x183>
mov    -0x750(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1328 <execline+0x188>
mov    -0x8(%rbp),%rax
add    $0x750,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execline>:
push   %rbp
push   %rbx
sub    $0x738,%rsp
lea    0x330(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <readline@plt>
lea    0xe3b(%rip),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strtok@plt>
mov    %rax,0x10(%rsp)
lea    0x18(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <strtok@plt>
mov    %rax,0x0(%rbp)
add    $0x8,%rbp
test   %rax,%rax
jne    11e0 <execline+0x40>
mov    0x10(%rsp),%rbx
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1280 <execline+0xe0>
lea    0xdf1(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1280 <execline+0xe0>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1241 <execline+0xa1>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <execvp@plt>
test   %rax,%rax
js     1287 <execline+0xe7>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
je     1276 <execline+0xd6>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xdc3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x738,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x10(%rsp),%rsi
lea    0xd79(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [36 x i8] c"error: command `%s` does not exist\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i64 @execline(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i64
%6 = alloca [100 x i8*]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i64 @readline(i32 %10, i8* %11, i32 1024)
store i64 %12, i64* %5
store i64 0, i64* %7
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = call i8* @strtok(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = load i64, i64* %7
%16 = add i64 %15, 1
store i64 %16, i64* %7
%17 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 %15
store i8* %14, i8** %17
br label %18
18: ; preds = %22, %1
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = load i64, i64* %7
%21 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 %20
store i8* %19, i8** %21
br label %22
22: ; preds = %18
%23 = load i64, i64* %7
%24 = add i64 %23, 1
store i64 %24, i64* %7
%25 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 %23
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %18, label %28
28: ; preds = %22
%29 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 0
%30 = load i8*, i8** %29
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 0
%35 = load i8*, i8** %34
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33, %28
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %33
%41 = call i32 (...) @fork()
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp ne i32 %42, 0
br i1 %43, label %55, label %44
44: ; preds = %40
%45 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 0
%46 = load i8*, i8** %45
%47 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 0
%48 = call i64 @execvp(i8* %46, i8** %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 0
%52 = load i8*, i8** %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %44, %40
%56 = load i32, i32* %8
%57 = call i32 @waitpid(i32 %56, i32* %9, i32 0)
%58 = load i32, i32* %9
%59 = call i64 @WIFEXITED(i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %55
%62 = load i32, i32* %9
%63 = call i32 @WEXITSTATUS(i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %61, %55
br label %66
66: ; preds = %65
%67 = load i64, i64* %2
ret i64 %67
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [36 x i8] c"error: command `%s` does not exist\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i64 @execline(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [100 x i8*]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = call i64 @readline(i32 %0, i8* %5, i32 1024)
%7 = bitcast [100 x i8*]* %3 to i8*
%8 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 0
store i8* %8, i8** %9
br label %10
10: ; preds = %10, %1
%11 = phi i64 [ 1, %1 ], [ %14, %10 ]
%12 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 %11
store i8* %12, i8** %13
%14 = add i64 %11, 1
%15 = icmp eq i8* %12, null
br i1 %15, label %16, label %10
16: ; preds = %10
%17 = load i8*, i8** %9
%18 = call i32 @strcmp(i8* dereferenceable(1) %17, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32 @strcmp(i8* dereferenceable(1) %17, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20, %16
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %20
%26 = call i32 (...) @fork()
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = call i64 @execvp(i8* %17, i8** %9)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i8*, i8** %9
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %28, %25
%36 = bitcast i32* %4 to i8*
%37 = call i32 @waitpid(i32 %26, i32* %4, i32 0)
%38 = load i32, i32* %4
%39 = call i64 @WIFEXITED(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %35
%42 = load i32, i32* %4
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %41, %35
ret i64 undef
}