#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 WIFEXITED(int);
int execle(char *, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/ *);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ puts(char *);
int system(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main() {
  pid_t pid = getpid();
  printf("pid:%d\n", pid);
  pid_t fork_pid = fork();
  pid = getpid();
  if (fork_pid == -1) {
    puts("failed to fork.");
    return 1;
  } else if (fork_pid > 0) {
    printf("parent %d's child %d, parent %d.\n", pid, fork_pid, getppid());
    int status;
    pid_t wait_pid;
    wait_pid = waitpid(fork_pid, &status, 0);
    printf("wait return %d, WIFEXITED=%d.\n", wait_pid, WIFEXITED(status));
  } else {
    printf("child %d's parent %d.\n", pid, getppid());
    int system_ret = system("date");
    printf("system() return %d.\n", system_ret);
    int exec_ret = execle("/bin/date", "date", 0, 0, NULL);
    if (exec_ret == -1) {
      puts("failed to exec. ");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11f0 <main+0x60>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x150>
cmpl   $0x0,-0xc(%rbp)
jle    125a <main+0xca>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1080 <WIFEXITED@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12d4 <main+0x144>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe09(%rip),%rdi
call   1050 <system@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xde4(%rip),%rdi
lea    0xde2(%rip),%rsi
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r8d
mov    %ecx,%edx
call   10a0 <execle@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12cf <main+0x13f>
lea    0xdc5(%rip),%rdi
call   1030 <puts@plt>
jmp    12d4 <main+0x144>
jmp    12d9 <main+0x149>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    $0xffffffff,%ebp
je     1218 <main+0x88>
mov    %eax,%ebx
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <getppid@plt>
test   %ebp,%ebp
jle    122c <main+0x9c>
lea    0xe3d(%rip),%rdi
xor    %r14d,%r14d
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
lea    0xe2f(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1284 <main+0xf4>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    1284 <main+0xf4>
lea    0xe26(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe45(%rip),%rbx
mov    %rbx,%rdi
call   1050 <system@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <execle@plt>
cmp    $0xffffffff,%eax
jne    1284 <main+0xf4>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"pid:%d\0A\00"
@.str.1 = [16 x i8] c"failed to fork.\00"
@.str.2 = [34 x i8] c"parent %d's child %d, parent %d.\0A\00"
@.str.3 = [31 x i8] c"wait return %d, WIFEXITED=%d.\0A\00"
@.str.4 = [23 x i8] c"child %d's parent %d.\0A\00"
@.str.5 = [5 x i8] c"date\00"
@.str.6 = [21 x i8] c"system() return %d.\0A\00"
@.str.7 = [10 x i8] c"/bin/date\00"
@.str.8 = [17 x i8] c"failed to exec. \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (...) @getpid()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %3
%12 = call i32 (...) @getpid()
store i32 %12, i32* %2
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %46
17: ; preds = %0
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load i32, i32* %2
%22 = load i32, i32* %3
%23 = call i32 (...) @getppid()
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22, i32 %23)
%25 = load i32, i32* %3
%26 = call i32 @waitpid(i32 %25, i32* %4, i32 0)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = load i32, i32* %4
%29 = call i32 @WIFEXITED(i32 %28)
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %29)
br label %44
31: ; preds = %17
%32 = load i32, i32* %2
%33 = call i32 (...) @getppid()
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %33)
%35 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %36)
%38 = call i32 @execle(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 0, i32* null)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %43
43: ; preds = %41, %31
br label %44
44: ; preds = %43, %20
br label %45
45: ; preds = %44
store i32 0, i32* %1
br label %46
46: ; preds = %45, %15
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [8 x i8] c"pid:%d\0A\00"
@.str.1 = [16 x i8] c"failed to fork.\00"
@.str.2 = [34 x i8] c"parent %d's child %d, parent %d.\0A\00"
@.str.3 = [31 x i8] c"wait return %d, WIFEXITED=%d.\0A\00"
@.str.4 = [23 x i8] c"child %d's parent %d.\0A\00"
@.str.5 = [5 x i8] c"date\00"
@.str.6 = [21 x i8] c"system() return %d.\0A\00"
@.str.7 = [10 x i8] c"/bin/date\00"
@.str.8 = [17 x i8] c"failed to exec. \00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 (...) @fork()
%5 = tail call i32 (...) @getpid()
%6 = icmp eq i32 %4, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %27
9: ; preds = %0
%10 = icmp sgt i32 %4, 0
%11 = tail call i32 (...) @getppid()
br i1 %10, label %12, label %19
12: ; preds = %9
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 %4, i32 %11)
%14 = bitcast i32* %1 to i8*
%15 = call i32 @waitpid(i32 %4, i32* %1, i32 0)
%16 = load i32, i32* %1
%17 = call i32 @WIFEXITED(i32 %16)
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %17)
br label %27
19: ; preds = %9
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %5, i32 %11)
%21 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = tail call i32 @execle(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 0, i32* null)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %27
27: ; preds = %12, %25, %19, %7
%28 = phi i32 [ 1, %7 ], [ 0, %19 ], [ 0, %25 ], [ 0, %12 ]
ret i32 %28
}