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

/* Variables and functions */
int /*<<< orphan*/ *execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ free(char **);
char **parse_cmdline(char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdin;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  int status;
  pid_t pid;
  while (status != -1) {
    char *cmdline;
    if (fgets(cmdline, 516, stdin) == 0) {
      perror("Unsuccessful line read");
      status = -1;
    }
    pid = fork();
    if (pid < 0) {
      status = -1;
      perror("Unsuccessful fork");
    } else if (pid == 0) {
      char **cmd_proccesed = parse_cmdline(&cmdline);
      if (execv(cmd_proccesed[0], cmd_proccesed) != NULL) {
        perror("Unsuccessful execute");
        status = -1;
      }
      free(cmd_proccesed);
      exit(0);
    } else {
      if (waitpid(pid, &status, 0) != pid) {
        status = -1;
      }
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     127f <main+0xff>
mov    -0x18(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    $0x204,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11cd <main+0x4d>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11fb <main+0x7b>
movl   $0xffffffff,-0x8(%rbp)
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
jmp    127a <main+0xfa>
cmpq   $0x0,-0x10(%rbp)
jne    1250 <main+0xd0>
lea    -0x18(%rbp),%rdi
call   1070 <parse_cmdline@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <execv@plt>
cmp    $0x0,%rax
je     1240 <main+0xc0>
lea    0xdf5(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     1270 <main+0xf0>
movl   $0xffffffff,-0x8(%rbp)
jmp    1275 <main+0xf5>
jmp    127a <main+0xfa>
jmp    118f <main+0xf>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e54(%rip),%r13
lea    0xe65(%rip),%r14
lea    0xe75(%rip),%r15
lea    0x4(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    $0x204,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     11ef <main+0x6f>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1201 <main+0x81>
je     122b <main+0xab>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
jne    1218 <main+0x98>
cmpl   $0xffffffff,0x4(%rsp)
jne    11b0 <main+0x30>
jmp    1218 <main+0x98>
mov    %r14,%rdi
call   1060 <perror@plt>
movl   $0xffffffff,0x4(%rsp)
ff
jmp    11c3 <main+0x43>
movl   $0xffffffff,0x4(%rsp)
ff
mov    %r15,%rdi
call   1060 <perror@plt>
cmpl   $0xffffffff,0x4(%rsp)
jne    11b0 <main+0x30>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0x8(%rsp),%rdi
call   1070 <parse_cmdline@plt>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %rax,%rsi
call   1090 <execv@plt>
test   %rax,%rax
je     125c <main+0xdc>
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,0x4(%rsp)
ff
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [23 x i8] c"Unsuccessful line read\00"
@.str.1 = [18 x i8] c"Unsuccessful fork\00"
@.str.2 = [21 x i8] c"Unsuccessful execute\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
store i32 0, i32* %1
br label %6
6: ; preds = %47, %0
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, -1
br i1 %8, label %9, label %48
9: ; preds = %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @stdin
%12 = call i64 @fgets(i8* %10, i32 516, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %16
16: ; preds = %14, %9
%17 = call i64 (...) @fork()
store i64 %17, i64* %3
%18 = load i64, i64* %3
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
store i32 -1, i32* %2
%21 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %47
22: ; preds = %16
%23 = load i64, i64* %3
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %22
%26 = call i8** @parse_cmdline(i8** %4)
store i8** %26, i8*** %5
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
%31 = call i32* @execv(i8* %29, i8** %30)
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
35: ; preds = %33, %25
%36 = load i8**, i8*** %5
%37 = call i32 @free(i8** %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %22
%40 = load i64, i64* %3
%41 = call i64 @waitpid(i64 %40, i32* %2, i32 0)
%42 = load i64, i64* %3
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 -1, i32* %2
br label %45
45: ; preds = %44, %39
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46, %20
br label %6
48: ; preds = %6
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [23 x i8] c"Unsuccessful line read\00"
@.str.1 = [18 x i8] c"Unsuccessful fork\00"
@.str.2 = [21 x i8] c"Unsuccessful execute\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = bitcast i32* %1 to i8*
%4 = bitcast i8** %2 to i8*
br label %5
5: ; preds = %0, %32
%6 = load i32, i32* @stdin
%7 = call i64 @fgets(i8* undef, i32 516, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %11
11: ; preds = %9, %5
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
store i32 -1, i32* %1
%15 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %32
16: ; preds = %11
%17 = icmp eq i64 %12, 0
br i1 %17, label %18, label %28
18: ; preds = %16
%19 = call i8** @parse_cmdline(i8** %2)
%20 = load i8*, i8** %19
%21 = call i32* @execv(i8* %20, i8** %19)
%22 = icmp eq i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %25
25: ; preds = %23, %18
%26 = call i32 @free(i8** %19)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %16
%29 = call i64 @waitpid(i64 %12, i32* %1, i32 0)
%30 = icmp eq i64 %29, %12
br i1 %30, label %32, label %31
31: ; preds = %28
br label %35
32: ; preds = %28, %14
%33 = load i32, i32* %1
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %5
35: ; preds = %32, %31
ret i32 -1
}