#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 */
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  int status;
  pid_t child;
  if (argc <= 1) {
    fprintf(stderr, "Uso: %s commando [argumentos ...]\n", argv[0]);
    exit(1);
  }
  child = fork();
  if (child == -1) {
    perror("ERROR fork");
    return 1;
  }
  if (child == 0) {
    execvp(argv[1], argv + 1);
    perror("ERROR child exec(...)");
    exit(1);
  } else {
    while (1) {
      if (wait(&status) < 0) {
        perror("waitpid");
        break;
      }
      if (WIFEXITED(status))
        break;
    }
  }
  return 0;
}


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)
cmpl   $0x1,-0x8(%rbp)
jg     11b6 <main+0x46>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e2 <main+0x72>
lea    0xe52(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <main+0xf8>
cmpl   $0x0,-0x18(%rbp)
jne    1217 <main+0xa7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1050 <execvp@plt>
lea    0xe26(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    121c <main+0xac>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0x0,%rax
jge    1240 <main+0xd0>
lea    0xe0e(%rip),%rdi
call   1040 <perror@plt>
jmp    125c <main+0xec>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1257 <main+0xe7>
jmp    125c <main+0xec>
jmp    121c <main+0xac>
jmp    1261 <main+0xf1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    11e4 <main+0x74>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11c3 <main+0x53>
test   %eax,%eax
je     1206 <main+0x96>
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <wait@plt>
test   %rax,%rax
js     11d6 <main+0x66>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11a0 <main+0x30>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe59(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
jmp    11bd <main+0x4d>
lea    0xe67(%rip),%rdi
call   1040 <perror@plt>
jmp    11bb <main+0x4b>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe09(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1050 <execvp@plt>
lea    0xe11(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"Uso: %s commando [argumentos ...]\0A\00"
@.str.1 = [11 x i8] c"ERROR fork\00"
@.str.2 = [22 x i8] c"ERROR child exec(...)\00"
@.str.3 = [8 x i8] c"waitpid\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 1
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 (...) @fork()
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
23: ; preds = %17
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = call i32 @execvp(i8* %29, i8** %31)
%33 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %23
br label %36
36: ; preds = %35, %46
%37 = call i64 @wait(i32* %6)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %47
41: ; preds = %36
%42 = load i32, i32* %6
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
br label %47
46: ; preds = %41
br label %36
47: ; preds = %45, %39
br label %48
48: ; preds = %47
store i32 0, i32* %3
br label %49
49: ; preds = %48, %21
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"Uso: %s commando [argumentos ...]\0A\00"
@.str.1 = [11 x i8] c"ERROR fork\00"
@.str.2 = [22 x i8] c"ERROR child exec(...)\00"
@.str.3 = [8 x i8] c"waitpid\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 (...) @fork()
switch i32 %12, label %21 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %11
%14 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %30
15: ; preds = %11
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @execvp(i8* %17, i8** %16)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %11, %26
%22 = call i64 @wait(i32* %3)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %30
26: ; preds = %21
%27 = load i32, i32* %3
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %21, label %30
30: ; preds = %26, %24, %13
%31 = phi i32 [ 1, %13 ], [ 0, %24 ], [ 0, %26 ]
ret i32 %31
}