#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 fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int wait(int *);
int main() {
  int status;
  int pid;
  if ((pid = fork()) == -1)
    perror("fork error");
  else if (pid == 0) {
    printf("%d\n", getpid());
    while (1)
      ;
  }
  pid = wait(&status);
  printf("pid = %d  status = %x \n", pid, status);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1183 <main+0x33>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
jmp    11ae <main+0x5e>
cmpl   $0x0,-0xc(%rbp)
jne    11a9 <main+0x59>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a4 <main+0x54>
jmp    11ae <main+0x5e>
lea    -0x8(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1183 <main+0x33>
cmp    $0xffffffff,%eax
je     11a2 <main+0x52>
lea    0x4(%rsp),%rdi
call   1060 <wait@plt>
mov    0x4(%rsp),%edx
lea    0xe99(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
nopw   0x0(%rax,%rax,1)
jmp    11a0 <main+0x50>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
jmp    1161 <main+0x11>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [24 x i8] c"pid = %d status = %x \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %16
8: ; preds = %0
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %14
br label %14
15: ; preds = %8
br label %16
16: ; preds = %15, %6
%17 = call i32 @wait(i32* %2)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = load i32, i32* %2
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [24 x i8] c"pid = %d status = %x \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %10 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %10
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %9, %6
br label %9
10: ; preds = %0, %4
%11 = call i32 @wait(i32* %1)
%12 = load i32, i32* %1
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %12)
ret i32 0
}