#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__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid1, pid2;
  if ((pid1 = fork()) < 0)
    printf("fork error");
  else if (pid1 == 0) {
    printf("child, %d\n", getpid());
    sleep(5);
  } else {
    pid2 = wait(NULL);
    printf("father, %d\n", getpid());
    printf("wait %ld\n", (long)pid2);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1187 <main+0x37>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f3 <main+0xa3>
cmpq   $0x0,-0x10(%rbp)
jne    11b8 <main+0x68>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1050 <sleep@plt>
jmp    11ee <main+0x9e>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f3 <main+0xa3>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1193 <main+0x43>
je     11a3 <main+0x53>
xor    %edi,%edi
call   1060 <wait@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe9b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c4 <main+0x74>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c4 <main+0x74>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x5,%edi
call   1050 <sleep@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [11 x i8] c"child, %d\0A\00"
@.str.2 = [12 x i8] c"father, %d\0A\00"
@.str.3 = [10 x i8] c"wait %ld\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %16
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %11
7: ; preds = %5
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 @sleep(i32 5)
br label %16
11: ; preds = %5
%12 = tail call i64 @wait(i32* null)
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %12)
br label %16
16: ; preds = %7, %11, %3
ret i32 0
}