#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 errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int pid, n;
  if ((pid = fork()) == 0)
    exit(0);
  sleep(2);
  if ((n = waitpid(pid, NULL, 0)) != pid) {
    printf("wait pid failed, pid %d, n %d, errno %d\n", pid, n, errno);
    exit(1);
  } else {
    printf("wait pid succeeded, pid %d, n %d, errno %d\n", pid, n, errno);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1180 <main+0x30>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmp    -0x14(%rbp),%eax
je     11c9 <main+0x79>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1190 <main+0x40>
mov    %eax,%ebx
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0x2e66(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ebx,%eax
jne    1197 <main+0x47>
lea    0xea2(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xe62(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"wait pid failed, pid %d, n %d, errno %d\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"wait pid succeeded, pid %d, n %d, errno %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = call i32 @sleep(i32 2)
%14 = load i32, i32* %6
%15 = call i32 @waitpid(i32 %14, i32* null, i32 0)
store i32 %15, i32* %7
%16 = load i32, i32* %6
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = load i32, i32* @errno
%22 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %12
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = load i32, i32* @errno
%28 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %26, i32 %27)
%29 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [41 x i8] c"wait pid failed, pid %d, n %d, errno %d\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"wait pid succeeded, pid %d, n %d, errno %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %2
%8 = tail call i32 @sleep(i32 2)
%9 = tail call i32 @waitpid(i32 %3, i32* null, i32 0)
%10 = icmp eq i32 %9, %3
%11 = load i32, i32* @errno
br i1 %10, label %15, label %12
12: ; preds = %7
%13 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %9, i32 %11)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %3, i32 %11)
%17 = tail call i32 @exit(i32 0)
unreachable
}