#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 */
int ENOMEM;
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int pthread_atfork(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid;
  int ret;
  ret = pthread_atfork(NULL, NULL, NULL);
  if (ret != 0) {
    if (ret == ENOMEM) {
      printf("Error: ran out of memory\n");
      return 2;
    }
    printf("Test FAILED: Expected return value success, instead received %d\n",
           ret);
    return 1;
  }
  pid = fork();
  if (pid < 0) {
    perror("Error in fork()\n");
    return 2;
  }
  if (pid == 0) {
    pthread_exit(0);
  } else {
    wait(NULL);
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
mov    %rdx,%rsi
call   1240 <__pthread_atfork>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d4 <main+0x74>
mov    -0x14(%rbp),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
jne    11b7 <main+0x57>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1237 <main+0xd7>
mov    -0x14(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1237 <main+0xd7>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1202 <main+0xa2>
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1237 <main+0xd7>
cmpq   $0x0,-0x10(%rbp)
jne    1219 <main+0xb9>
xor    %edi,%edi
call   1050 <pthread_exit@plt>
jmp    1222 <main+0xc2>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1200 <__pthread_atfork>
test   %eax,%eax
je     119e <main+0x2e>
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%eax
jne    11b5 <main+0x45>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11e3 <main+0x73>
je     11cc <main+0x5c>
xor    %edi,%edi
call   1080 <wait@plt>
jmp    11d3 <main+0x63>
lea    0xe44(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret
xor    %edi,%edi
call   1060 <pthread_exit@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
jmp    1197 <main+0x27>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)

LLVM IR (-O0):
@ENOMEM = global i32 0
@.str = [26 x i8] c"Error: ran out of memory\0A\00"
@.str.1 = [65 x i8] c"Test FAILED: Expected return value success, instead received %d\0A\00"
@.str.2 = [17 x i8] c"Error in fork()\0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @pthread_atfork(i32* null, i32* null, i32* null)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %0
%8 = load i32, i32* %3
%9 = load i32, i32* @ENOMEM
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %31
13: ; preds = %7
%14 = load i32, i32* %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 1, i32* %1
br label %31
16: ; preds = %0
%17 = call i64 (...) @fork()
store i64 %17, i64* %2
%18 = load i64, i64* %2
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %31
22: ; preds = %16
%23 = load i64, i64* %2
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @pthread_exit(i32 0)
br label %29
27: ; preds = %22
%28 = call i32 @wait(i32* null)
br label %29
29: ; preds = %27, %25
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %31
31: ; preds = %29, %20, %13, %11
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str.1 = [65 x i8] c"Test FAILED: Expected return value success, instead received %d\0A\00"
@.str.2 = [17 x i8] c"Error in fork()\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.4 = [25 x i8] c"Error: ran out of memory\00"
define i32 @main() {
%1 = tail call i32 @pthread_atfork(i32* null, i32* null, i32* null)
%2 = icmp eq i32 %1, 0
br i1 %2, label %10, label %3
3: ; preds = %0
%4 = load i32, i32* @ENOMEM
%5 = icmp eq i32 %1, %4
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
br label %23
8: ; preds = %3
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %23
10: ; preds = %0
%11 = tail call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %23
15: ; preds = %10
%16 = icmp eq i64 %11, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @pthread_exit(i32 0)
br label %21
19: ; preds = %15
%20 = tail call i32 @wait(i32* null)
br label %21
21: ; preds = %19, %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %13, %8, %6
%24 = phi i32 [ 2, %6 ], [ 1, %8 ], [ 2, %13 ], [ 0, %21 ]
ret i32 %24
}