#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 */
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ printf(char *, 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);
  }
}


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    0xe3e(%rip),%rsi
call   1070 <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    0xe0e(%rip),%rsi
call   1070 <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    11e7 <execline+0x67>
mov    -0x740(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     1265 <execline+0xe5>
mov    -0x740(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    126c <execline+0xec>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x74c(%rbp)
cmpl   $0x0,-0x74c(%rbp)
jne    12bd <execline+0x13d>
mov    -0x740(%rbp),%rdi
lea    -0x740(%rbp),%rsi
call   1080 <execvp@plt>
cmp    $0x0,%rax
jge    12bd <execline+0x13d>
mov    -0x740(%rbp),%rsi
lea    0xd5b(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x74c(%rbp),%edi
lea    -0x750(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
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    0xe5b(%rip),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <strtok@plt>
mov    %rax,0x10(%rsp)
lea    0x18(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <strtok@plt>
mov    %rax,0x0(%rbp)
add    $0x8,%rbp
test   %rax,%rax
jne    11c0 <execline+0x40>
mov    0x10(%rsp),%rbx
lea    0xe1f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1239 <execline+0xb9>
lea    0xe11(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1239 <execline+0xb9>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1221 <execline+0xa1>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <execvp@plt>
test   %rax,%rax
js     1240 <execline+0xc0>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
add    $0x738,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x10(%rsp),%rsi
lea    0xdc0(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1090 <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"
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 @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)
br label %58
58: ; preds = %55
%59 = load i64, i64* %2
ret i64 %59
}

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"
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 @printf(i8* 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)
ret i64 undef
}