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

/* Variables and functions */
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
rodeveja(int argc, char **argv, int bg) {
  char *nome;
  int retVal = 42;
  if (argc < 1) {
    if (!bg)
      printf("Modo de uso: rodeveja <executavel> <argumentos>\n");
    else
      printf("Modo de uso: rode <executavel> <argumentos>\n");
    return -1;
  }
  if (fork()) {
    if (!bg) {
      wait(&retVal);
      retVal = retVal >> 8;
      printf("Valor retornado => %d\n", retVal);
    }
  } else {
    if (bg)
      close(0);
    nome = argv[0];
    execve(nome, argv, NULL);
    printf("Não foi possível encontrar o programa: '%s'\n", nome);
  }
  return 0;
}


x86 assembly (clang, -O0):
<rodeveja>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x2a,-0x24(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    11aa <rodeveja+0x5a>
cmpl   $0x0,-0x14(%rbp)
jne    1190 <rodeveja+0x40>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    119e <rodeveja+0x4e>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <rodeveja+0xe3>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11ed <rodeveja+0x9d>
cmpl   $0x0,-0x14(%rbp)
jne    11e8 <rodeveja+0x98>
lea    -0x24(%rbp),%rdi
call   1060 <wait@plt>
mov    -0x24(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122c <rodeveja+0xdc>
cmpl   $0x0,-0x14(%rbp)
je     11fe <rodeveja+0xae>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execve@plt>
mov    -0x20(%rbp),%rsi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rodeveja>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
movl   $0x2a,0xc(%rsp)
test   %edi,%edi
jle    11b2 <rodeveja+0x52>
mov    %rsi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11bf <rodeveja+0x5f>
test   %ebp,%ebp
jne    1200 <rodeveja+0xa0>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    0xc(%rsp),%esi
sar    $0x8,%esi
mov    %esi,0xc(%rsp)
lea    0xe59(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1200 <rodeveja+0xa0>
test   %ebp,%ebp
je     11ef <rodeveja+0x8f>
lea    0xeb9(%rip),%rdi
jmp    11f6 <rodeveja+0x96>
test   %ebp,%ebp
je     11ca <rodeveja+0x6a>
xor    %edi,%edi
call   1050 <close@plt>
mov    (%r14),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <execve@plt>
lea    0xe34(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1200 <rodeveja+0xa0>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"Modo de uso: rodeveja <executavel> <argumentos>\0A\00"
@.str.1 = [45 x i8] c"Modo de uso: rode <executavel> <argumentos>\0A\00"
@.str.2 = [23 x i8] c"Valor retornado => %d\0A\00"
@.str.3 = [47 x i8] c"N\C3\A3o foi poss\C3\ADvel encontrar o programa: '%s'\0A\00"
define i32 @rodeveja(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 42, i32* %9
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %19
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
store i32 -1, i32* %4
br label %48
20: ; preds = %3
%21 = call i64 (...) @fork()
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = call i32 @wait(i32* %9)
%28 = load i32, i32* %9
%29 = ashr i32 %28, 8
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %26, %23
br label %47
33: ; preds = %20
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @close(i32 0)
br label %38
38: ; preds = %36, %33
%39 = load i8**, i8*** %6
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = load i8**, i8*** %6
%44 = call i32 @execve(i8* %42, i8** %43, i32* null)
%45 = load i8*, i8** %8
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %38, %32
store i32 0, i32* %4
br label %48
48: ; preds = %47, %19
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str.2 = [23 x i8] c"Valor retornado => %d\0A\00"
@.str.3 = [47 x i8] c"N\C3\A3o foi poss\C3\ADvel encontrar o programa: '%s'\0A\00"
@str = [48 x i8] c"Modo de uso: rodeveja <executavel> <argumentos>\00"
@str.4 = [44 x i8] c"Modo de uso: rode <executavel> <argumentos>\00"
define i32 @rodeveja(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 42, i32* %4
%6 = icmp slt i32 %0, 1
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = icmp eq i32 %2, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %30
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.4, i64 0, i64 0))
br label %30
13: ; preds = %3
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
%16 = icmp eq i32 %2, 0
br i1 %15, label %23, label %17
17: ; preds = %13
br i1 %16, label %18, label %30
18: ; preds = %17
%19 = call i32 @wait(i32* %4)
%20 = load i32, i32* %4
%21 = ashr i32 %20, 8
store i32 %21, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %30
23: ; preds = %13
br i1 %16, label %26, label %24
24: ; preds = %23
%25 = tail call i32 @close(i32 0)
br label %26
26: ; preds = %24, %23
%27 = load i8*, i8** %1
%28 = tail call i32 @execve(i8* %27, i8** %1, i32* null)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %30
30: ; preds = %26, %18, %17, %9, %11
%31 = phi i32 [ -1, %11 ], [ -1, %9 ], [ 0, %17 ], [ 0, %18 ], [ 0, %26 ]
ret i32 %31
}