#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 fork();
int getpid();
int getppid();
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ wait();
int main() {
  char cmd[20];
  int i;
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    printf("Parent process ID = %d\n", getppid());
    printf("Child process ID = %d\n", getpid());
    gets(cmd);
    system(cmd);
  } else if (pid == -1)
    printf("Error creating child process\n");
  else
    wait();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11d8 <main+0x68>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1060 <gets@plt>
lea    -0x20(%rbp),%rdi
call   1040 <system@plt>
jmp    1201 <main+0x91>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11f5 <main+0x85>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11fc <main+0x8c>
mov    $0x0,%al
call   1080 <wait@plt>
jmp    1201 <main+0x91>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11d8 <main+0x58>
test   %eax,%eax
jne    11e6 <main+0x66>
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe5d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <gets@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
jmp    11ed <main+0x6d>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
jmp    11ed <main+0x6d>
xor    %eax,%eax
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Parent process ID = %d\0A\00"
@.str.1 = [23 x i8] c"Child process ID = %d\0A\00"
@.str.2 = [30 x i8] c"Error creating child process\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %0
%9 = call i32 (...) @getppid()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 (...) @getpid()
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%14 = call i32 @gets(i8* %13)
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%16 = call i32 @system(i8* %15)
br label %25
17: ; preds = %0
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %24
22: ; preds = %17
%23 = call i32 (...) @wait()
br label %24
24: ; preds = %22, %20
br label %25
25: ; preds = %24, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Parent process ID = %d\0A\00"
@.str.1 = [23 x i8] c"Child process ID = %d\0A\00"
@str = [29 x i8] c"Error creating child process\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @fork()
switch i32 %3, label %13 [
i32 0, label %4
i32 -1, label %11
]
4: ; preds = %0
%5 = tail call i32 (...) @getppid()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32 @gets(i8* %2)
%10 = call i32 @system(i8* %2)
br label %15
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %15
13: ; preds = %0
%14 = tail call i32 (...) @wait()
br label %15
15: ; preds = %11, %13, %4
ret i32 0
}