#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main() {
  int mem = 0, pid;
  pid = fork();
  if (pid == -1) {
    mem = 1;
    perror("fork");
    exit(1);
  }
  if (pid == 0) {
    if (mem == 0)
      exit(0);
    else
      exit(1);
  } else {
    int ret;
    int status;
    while ((ret = waitpid(pid, &status, 0)) != pid) {
      if (errno != EINTR) {
        perror("waitpid");
        exit(1);
      }
    }
    mem = status;
  }
  if (mem == 0)
    printf("mem is zero\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1197 <main+0x47>
movl   $0x1,-0x8(%rbp)
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11bc <main+0x6c>
cmpl   $0x0,-0x8(%rbp)
jne    11b2 <main+0x62>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11c1 <main+0x71>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0x10(%rbp)
cmp    -0xc(%rbp),%eax
je     1210 <main+0xc0>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
je     120b <main+0xbb>
lea    0xe09(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11c1 <main+0x71>
mov    -0x14(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    122c <main+0xdc>
lea    0xde6(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11d0 <main+0x80>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11d7 <main+0x87>
lea    0x4(%rsp),%r14
mov    0x2e6a(%rip),%r15
mov    0x2e5b(%rip),%r12
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    %ebx,%eax
je     11af <main+0x5f>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1180 <main+0x30>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,0x4(%rsp)
jne    11c2 <main+0x72>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe22(%rip),%rdi
jmp    11a0 <main+0x50>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [13 x i8] c"mem is zero\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%6 = call i32 (...) @fork()
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
store i32 1, i32* %2
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %15
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
br label %23
23: ; preds = %35, %22
%24 = load i32, i32* %3
%25 = call i32 @waitpid(i32 %24, i32* %5, i32 0)
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %36
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
br label %23
36: ; preds = %23
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %38
38: ; preds = %36
%39 = load i32, i32* %2
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"waitpid\00"
@str = [12 x i8] c"mem is zero\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
switch i32 %2, label %8 [
i32 -1, label %3
i32 0, label %6
]
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = bitcast i32* %1 to i8*
br label %10
10: ; preds = %13, %8
%11 = call i32 @waitpid(i32 %2, i32* %1, i32 0)
%12 = icmp eq i32 %11, %2
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = load i32, i32* %1
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
ret i32 0
}