#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 */
int /*<<< orphan*/ change_pname(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(scalar_t__, int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int main(void) {
  pid_t p;
  int status;
  fprintf(stderr, "Parent, PID = %ld: Creating child...\n", (long)getpid());
  p = fork();
  if (p < 0) {
    perror("fork");
    exit(1);
  }
  if (p == 0) {
    change_pname("child");
    sleep(10);
    exit(101);
  }
  change_pname("father");
  printf("Parent, PID = %ld: Created child with PID = %ld, waiting for it to "
         "terminate...\n",
         (long)getpid(), (long)p);
  p = wait(&status);
  explain_wait_status(p, status);
  printf("Parent: All done, exiting...\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe31(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1200 <main+0x60>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    122b <main+0x8b>
lea    0xe19(%rip),%rdi
call   1040 <change_pname@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    $0x65,%edi
call   1080 <exit@plt>
lea    0xdff(%rip),%rdi
call   1040 <change_pname@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
mov    -0x10(%rbp),%rdx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x14(%rbp),%rdi
call   10a0 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10c0 <explain_wait_status@plt>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e24(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe34(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1238 <main+0x88>
je     124e <main+0x9e>
mov    %rax,%rbx
lea    0xe43(%rip),%rdi
call   1050 <change_pname@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe37(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10d0 <explain_wait_status@plt>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdd6(%rip),%rdi
call   1050 <change_pname@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
mov    $0x65,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Parent, PID = %ld: Creating child...\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [6 x i8] c"child\00"
@.str.3 = [7 x i8] c"father\00"
@.str.4 = [81 x i8] c"Parent, PID = %ld: Created child with PID = %ld, waiting for it to terminate...\0A\00"
@.str.5 = [30 x i8] c"Parent: All done, exiting...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @stderr
%5 = call i64 (...) @getpid()
%6 = call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %5)
%7 = call i64 (...) @fork()
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i64, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @change_pname(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @sleep(i32 10)
%19 = call i32 @exit(i32 101)
unreachable
20: ; preds = %13
%21 = call i32 @change_pname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = call i64 (...) @getpid()
%23 = load i64, i64* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.4, i64 0, i64 0), i64 %22, i64 %23)
%25 = call i64 @wait(i32* %3)
store i64 %25, i64* %2
%26 = load i64, i64* %2
%27 = load i32, i32* %3
%28 = call i32 @explain_wait_status(i64 %26, i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Parent, PID = %ld: Creating child...\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [6 x i8] c"child\00"
@.str.3 = [7 x i8] c"father\00"
@.str.4 = [81 x i8] c"Parent, PID = %ld: Created child with PID = %ld, waiting for it to terminate...\0A\00"
@str = [29 x i8] c"Parent: All done, exiting...\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @stderr
%4 = tail call i64 (...) @getpid()
%5 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %4)
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = icmp eq i64 %6, 0
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = tail call i32 @change_pname(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 10)
%16 = tail call i32 @exit(i32 101)
unreachable
17: ; preds = %11
%18 = tail call i32 @change_pname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i64 (...) @getpid()
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.4, i64 0, i64 0), i64 %19, i64 %6)
%21 = call i64 @wait(i32* %1)
%22 = load i32, i32* %1
%23 = call i32 @explain_wait_status(i64 %21, i32 %22)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
ret i32 0
}