#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 */
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ perror(char *);
scalar_t__ vfork();
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  pid_t res = vfork();
  if (res == 0) {
    char *args[] = {"echo", "hello", NULL};
    execv("/bin/echo", args);
  } else if (res > 0) {
    int status;
    waitpid(res, &status, 0);
    if (WIFEXITED(status))
      return 0;
    else
      return 1;
  } else {
    perror("vfork");
    return 2;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <vfork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ab <main+0x5b>
mov    0x2c84(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c81(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c7e(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1070 <execv@plt>
jmp    1202 <main+0xb2>
cmpq   $0x0,-0x10(%rbp)
jle    11ef <main+0x9f>
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    -0x34(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e3 <main+0x93>
movl   $0x0,-0x4(%rbp)
jmp    1202 <main+0xb2>
movl   $0x1,-0x4(%rbp)
jmp    1202 <main+0xb2>
lea    0xe1a(%rip),%rdi
call   1040 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <vfork@plt>
test   %rax,%rax
je     1184 <main+0x34>
jle    11b2 <main+0x62>
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    (%rsp),%edi
call   1060 <WIFEXITED@plt>
xor    %ebx,%ebx
test   %rax,%rax
sete   %bl
jmp    11aa <main+0x5a>
mov    0x2c85(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c69(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe64(%rip),%rdi
mov    %rsp,%rsi
call   1070 <execv@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe57(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%ebx
jmp    11aa <main+0x5a>

LLVM IR (-O0):
@.str = [5 x i8] c"echo\00"
@.str.1 = [6 x i8] c"hello\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [10 x i8] c"/bin/echo\00"
@.str.3 = [6 x i8] c"vfork\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [3 x i8*]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @vfork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = bitcast [3 x i8*]* %3 to i8*
%10 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%11 = call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %10)
br label %25
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i64, i64* %2
%17 = call i32 @waitpid(i64 %16, i32* %4, i32 0)
%18 = load i32, i32* %4
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 0, i32* %1
br label %25
22: ; preds = %15
store i32 1, i32* %1
br label %25
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %25
25: ; preds = %21, %22, %23, %8
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"echo\00"
@.str.1 = [6 x i8] c"hello\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [10 x i8] c"/bin/echo\00"
@.str.3 = [6 x i8] c"vfork\00"
define i32 @main() {
%1 = alloca [3 x i8*]
%2 = alloca i32
%3 = tail call i64 (...) @vfork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = bitcast [3 x i8*]* %1 to i8*
%7 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%8 = call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %7)
br label %20
9: ; preds = %0
%10 = icmp sgt i64 %3, 0
br i1 %10, label %11, label %18
11: ; preds = %9
%12 = bitcast i32* %2 to i8*
%13 = call i32 @waitpid(i64 %3, i32* %2, i32 0)
%14 = load i32, i32* %2
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp eq i64 %15, 0
%17 = zext i1 %16 to i32
br label %20
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %5, %18, %11
%21 = phi i32 [ 0, %5 ], [ %17, %11 ], [ 2, %18 ]
ret i32 %21
}