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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int strlen(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  char command[255];
  pid_t pid;
  int status;
  printf("%% ");
  while (fgets(command, 255, stdin) != NULL) {
    if (command[strlen(command) - 1] == '\n')
      command[strlen(command) - 1] = 0;
    pid = fork();
    switch (pid) {
    case -1:
      printf("Error\n");
      break;
    case 0:
      execlp("sh", "sh", "-c", command, (char *)NULL);
      printf("Unable to execute: %s", command);
      exit(EXIT_FAILURE);
      break;
    default:
      if ((pid = waitpid(pid, &status, 0)) < 0)
        printf("waitpid error");
      if (WIFEXITED(status))
        printf("Process's exit code: %d\n", WEXITSTATUS(status));
      printf("%% ");
      break;
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1314 <main+0x184>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    120f <main+0x7f>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%eax
mov    %eax,-0x11c(%rbp)
sub    $0xffffffff,%eax
je     1249 <main+0xb9>
jmp    1236 <main+0xa6>
mov    -0x11c(%rbp),%eax
test   %eax,%eax
je     125c <main+0xcc>
jmp    12a1 <main+0x111>
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130f <main+0x17f>
lea    -0x110(%rbp),%rcx
lea    0xda1(%rip),%rsi
lea    0xd9d(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10a0 <execlp@plt>
lea    -0x110(%rbp),%rsi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x114(%rbp),%edi
lea    -0x118(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jge    12d2 <main+0x142>
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x118(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1301 <main+0x171>
mov    -0x118(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b0 <main+0x20>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e22(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11ef <main+0x4f>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe51(%rip),%r14
lea    0xc(%rsp),%r15
lea    0xdfe(%rip),%r13
lea    0xe25(%rip),%rbp
jmp    122e <main+0x8e>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11e1 <main+0x41>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    124e <main+0xae>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0x70>
test   %eax,%eax
je     12ad <main+0x10d>
mov    %eax,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %eax,%eax
jns    127c <main+0xdc>
lea    0xdab(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     129e <main+0xfe>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1218 <main+0x78>
lea    0xd50(%rip),%rdi
lea    0xd4c(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10b0 <execlp@plt>
lea    0xd35(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.1 = [7 x i8] c"Error\0A\00"
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [22 x i8] c"Unable to execute: %s\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [14 x i8] c"waitpid error\00"
@.str.6 = [25 x i8] c"Process's exit code: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [255 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %54, %0
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32* @fgets(i8* %7, i32 255, i32 %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %55
11: ; preds = %6
%12 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %20, %11
%27 = call i32 (...) @fork()
store i32 %27, i32* %3
%28 = load i32, i32* %3
switch i32 %28, label %38 [
i32 -1, label %29
i32 0, label %31
]
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %54
31: ; preds = %26
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%33 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* null)
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %26
%39 = load i32, i32* %3
%40 = call i32 @waitpid(i32 %39, i32* %4, i32 0)
store i32 %40, i32* %3
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = load i32, i32* %4
%46 = call i32 @WIFEXITED(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i32, i32* %4
%50 = call i32 @WEXITSTATUS(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %48, %44
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %54
54: ; preds = %52, %29
br label %6
55: ; preds = %6
%56 = load i32, i32* @EXIT_SUCCESS
%57 = call i32 @exit(i32 %56)
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [22 x i8] c"Unable to execute: %s\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [14 x i8] c"waitpid error\00"
@.str.6 = [25 x i8] c"Process's exit code: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [6 x i8] c"Error\00"
define i32 @main() {
%1 = alloca [255 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32* @fgets(i8* %3, i32 255, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %49, label %9
9: ; preds = %0, %45
%10 = call i32 @strlen(i8* %3)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %21
16: ; preds = %9
%17 = call i32 @strlen(i8* %3)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %16, %9
%22 = call i32 (...) @fork()
switch i32 %22, label %30 [
i32 -1, label %23
i32 0, label %25
]
23: ; preds = %21
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %45
25: ; preds = %21
%26 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* null)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
%31 = call i32 @waitpid(i32 %22, i32* %2, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = load i32, i32* %2
%37 = call i32 @WIFEXITED(i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i32, i32* %2
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %39, %35
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %23
%46 = load i32, i32* @stdin
%47 = call i32* @fgets(i8* %3, i32 255, i32 %46)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %9
49: ; preds = %45, %0
%50 = load i32, i32* @EXIT_SUCCESS
%51 = call i32 @exit(i32 %50)
unreachable
}