#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 /*<<< orphan*/ poll(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv, char **envp) {
  int r;
  r = fork();
  if (r < 0) {
    char msg[] = "fork() failed\n";
    write(2, msg, sizeof msg - 1);
  } else if (r == 0) {
    char msg[] = "child!\n";
    char msg2[] = "child exitting...\n";
    write(2, msg, sizeof msg - 1);
    poll(0, 0, 2000);
    write(2, msg2, sizeof msg2 - 1);
  } else {
    int status = 0;
    char msg[] = "parent!\n";
    char msg2[] = "child exitted\n";
    write(2, msg, sizeof msg - 1);
    waitpid(-1, &status, 0);
    write(2, msg2, sizeof msg2 - 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ae <main+0x6e>
mov    0xe8b(%rip),%rax
mov    %rax,-0x2b(%rbp)
mov    0xe89(%rip),%eax
mov    %eax,-0x23(%rbp)
mov    0xe83(%rip),%ax
mov    %ax,-0x1f(%rbp)
mov    0xe7b(%rip),%al
mov    %al,-0x1d(%rbp)
lea    -0x2b(%rbp),%rsi
mov    $0x2,%edi
mov    $0xe,%edx
call   1030 <write@plt>
jmp    12a4 <main+0x164>
cmpl   $0x0,-0x1c(%rbp)
jne    1226 <main+0xe6>
mov    0xe50(%rip),%rax
mov    %rax,-0x33(%rbp)
mov    0xe66(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe63(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe60(%rip),%ax
mov    %ax,-0x40(%rbp)
mov    0xe58(%rip),%al
mov    %al,-0x3e(%rbp)
lea    -0x33(%rbp),%rsi
mov    $0x2,%edi
mov    $0x7,%edx
call   1030 <write@plt>
xor    %esi,%esi
mov    $0x7d0,%edx
mov    %esi,%edi
call   1040 <poll@plt>
lea    -0x50(%rbp),%rsi
mov    $0x2,%edi
mov    $0x12,%edx
call   1030 <write@plt>
jmp    129f <main+0x15f>
movl   $0x0,-0x54(%rbp)
mov    0xde3(%rip),%rax
mov    %rax,-0x5d(%rbp)
mov    0xde1(%rip),%al
mov    %al,-0x55(%rbp)
mov    0xdd8(%rip),%rax
mov    %rax,-0x6c(%rbp)
mov    0xdd6(%rip),%eax
mov    %eax,-0x64(%rbp)
mov    0xdd0(%rip),%ax
mov    %ax,-0x60(%rbp)
mov    0xdc8(%rip),%al
mov    %al,-0x5e(%rbp)
lea    -0x5d(%rbp),%rsi
mov    $0x2,%edi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0xffffffff,%edi
lea    -0x54(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
lea    -0x6c(%rbp),%rsi
mov    $0x2,%edi
mov    $0xe,%edx
call   1030 <write@plt>
jmp    12a4 <main+0x164>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     11b3 <main+0x73>
je     11e2 <main+0xa2>
movl   $0x0,0xc(%rsp)
movabs $0xa21746e65726170,%rax
74 21 0a
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
movabs $0xa646574746978,%rax
mov    %rax,0x17(%rsp)
movabs $0x786520646c696863,%rax
20 65 78
mov    %rax,0x10(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edi
mov    $0x8,%edx
call   1030 <write@plt>
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    11d1 <main+0x91>
movabs $0xa64656c696166,%rax
mov    %rax,0x17(%rsp)
movabs $0x662029286b726f66,%rax
29 20 66
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    $0x2,%edi
mov    $0xe,%edx
jmp    1233 <main+0xf3>
movabs $0xa21646c696863,%rax
mov    %rax,(%rsp)
movaps 0xe39(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0xa2e2e,0x1f(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edi
mov    $0x7,%edx
call   1030 <write@plt>
xor    %edi,%edi
xor    %esi,%esi
mov    $0x7d0,%edx
call   1040 <poll@plt>
lea    0x10(%rsp),%rsi
mov    $0x2,%edi
mov    $0x12,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
@__const.main.msg = [15 x i8] c"fork() failed\0A\00"
@__const.main.msg.1 = [8 x i8] c"child!\0A\00"
@__const.main.msg2 = [19 x i8] c"child exitting...\0A\00"
@__const.main.msg.2 = [9 x i8] c"parent!\0A\00"
@__const.main.msg2.3 = [15 x i8] c"child exitted\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [15 x i8]
%10 = alloca [8 x i8]
%11 = alloca [19 x i8]
%12 = alloca i32
%13 = alloca [9 x i8]
%14 = alloca [15 x i8]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%15 = call i32 (...) @fork()
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = bitcast [15 x i8]* %9 to i8*
%20 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%21 = call i32 @write(i32 2, i8* %20, i32 14)
br label %42
22: ; preds = %3
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = bitcast [8 x i8]* %10 to i8*
%27 = bitcast [19 x i8]* %11 to i8*
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%29 = call i32 @write(i32 2, i8* %28, i32 7)
%30 = call i32 @poll(i32 0, i32 0, i32 2000)
%31 = getelementptr inbounds [19 x i8], [19 x i8]* %11, i64 0, i64 0
%32 = call i32 @write(i32 2, i8* %31, i32 18)
br label %41
33: ; preds = %22
store i32 0, i32* %12
%34 = bitcast [9 x i8]* %13 to i8*
%35 = bitcast [15 x i8]* %14 to i8*
%36 = getelementptr inbounds [9 x i8], [9 x i8]* %13, i64 0, i64 0
%37 = call i32 @write(i32 2, i8* %36, i32 8)
%38 = call i32 @waitpid(i32 -1, i32* %12, i32 0)
%39 = getelementptr inbounds [15 x i8], [15 x i8]* %14, i64 0, i64 0
%40 = call i32 @write(i32 2, i8* %39, i32 14)
br label %41
41: ; preds = %33, %25
br label %42
42: ; preds = %41, %18
ret i32 0
}

LLVM IR (-O2):
@__const.main.msg = [15 x i8] c"fork() failed\0A\00"
@__const.main.msg2 = [19 x i8] c"child exitting...\0A\00"
@__const.main.msg.2 = [9 x i8] c"parent!\0A\00"
@__const.main.msg2.3 = [15 x i8] c"child exitted\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca [15 x i8]
%5 = alloca i64
%6 = alloca [19 x i8]
%7 = alloca i32
%8 = alloca [9 x i8]
%9 = alloca [15 x i8]
%10 = tail call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%14 = call i32 @write(i32 2, i8* %13, i32 14)
br label %30
15: ; preds = %3
%16 = icmp eq i32 %10, 0
br i1 %16, label %17, label %23
17: ; preds = %15
%18 = bitcast i64* %5 to i8*
store i64 2851464966400099, i64* %5
%19 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%20 = call i32 @write(i32 2, i8* %18, i32 7)
%21 = tail call i32 @poll(i32 0, i32 0, i32 2000)
%22 = call i32 @write(i32 2, i8* %19, i32 18)
br label %30
23: ; preds = %15
%24 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%25 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
%26 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%27 = call i32 @write(i32 2, i8* %25, i32 8)
%28 = call i32 @waitpid(i32 -1, i32* %7, i32 0)
%29 = call i32 @write(i32 2, i8* %26, i32 14)
br label %30
30: ; preds = %17, %23, %12
ret i32 0
}