#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 */
struct pthreadpool {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int pthreadpool_destroy(struct pthreadpool *);
int pthreadpool_init(int, struct pthreadpool **);
int /*<<< orphan*/ stderr;
char *strerror(int);
int wait(int *);
int test_fork(void) {
  struct pthreadpool *p;
  pid_t child, waited;
  int status, ret;
  ret = pthreadpool_init(1, &p);
  if (ret != 0) {
    fprintf(stderr, "pthreadpool_init failed: %s\n", strerror(ret));
    return -1;
  }
  ret = pthreadpool_destroy(p);
  if (ret != 0) {
    fprintf(stderr, "pthreadpool_destroy failed: %s\n", strerror(ret));
    return -1;
  }
  child = fork();
  if (child < 0) {
    perror("fork failed");
    return -1;
  }
  if (child == 0) {
    exit(0);
  }
  waited = wait(&status);
  if (waited == -1) {
    perror("wait failed");
    return -1;
  }
  if (waited != child) {
    fprintf(stderr, "expected child %d, got %d\n", (int)child, (int)waited);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x1,%edi
lea    -0x10(%rbp),%rsi
call   1040 <pthreadpool_init@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11d7 <test_fork+0x57>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <test_fork+0x148>
mov    -0x10(%rbp),%rdi
call   1030 <pthreadpool_destroy@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1221 <test_fork+0xa1>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <test_fork+0x148>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124d <test_fork+0xcd>
lea    0xe01(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <test_fork+0x148>
cmpl   $0x0,-0x14(%rbp)
jne    125e <test_fork+0xde>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x1c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    128c <test_fork+0x10c>
lea    0xdce(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <test_fork+0x148>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12c1 <test_fork+0x141>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <test_fork+0x148>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fork>:
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rsi
mov    $0x1,%edi
call   1040 <pthreadpool_init@plt>
test   %eax,%eax
je     11af <test_fork+0x2f>
mov    0x2e43(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1080 <strerror@plt>
lea    0xe53(%rip),%rsi
jmp    11d3 <test_fork+0x53>
mov    (%rsp),%rdi
call   1030 <pthreadpool_destroy@plt>
test   %eax,%eax
je     11ec <test_fork+0x6c>
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1080 <strerror@plt>
lea    0xe4a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     122d <test_fork+0xad>
je     1249 <test_fork+0xc9>
mov    %eax,%ebx
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
je     123b <test_fork+0xbb>
xor    %ecx,%ecx
cmp    %ebx,%eax
je     11e4 <test_fork+0x64>
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%edi
lea    0xe35(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11df <test_fork+0x5f>
lea    0xe09(%rip),%rdi
call   1060 <perror@plt>
jmp    11df <test_fork+0x5f>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
jmp    11df <test_fork+0x5f>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.pthreadpool = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"pthreadpool_init failed: %s\0A\00"
@.str.1 = [32 x i8] c"pthreadpool_destroy failed: %s\0A\00"
@.str.2 = [12 x i8] c"fork failed\00"
@.str.3 = [12 x i8] c"wait failed\00"
@.str.4 = [27 x i8] c"expected child %d, got %d\0A\00"
define i32 @test_fork() {
%1 = alloca i32
%2 = alloca %struct.pthreadpool*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = call i32 @pthreadpool_init(i32 1, %struct.pthreadpool** %2)
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* %6
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %1
br label %52
15: ; preds = %0
%16 = load %struct.pthreadpool*, %struct.pthreadpool** %2
%17 = call i32 @pthreadpool_destroy(%struct.pthreadpool* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* %6
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 -1, i32* %1
br label %52
25: ; preds = %15
%26 = call i32 (...) @fork()
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %52
31: ; preds = %25
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %31
%37 = call i32 @wait(i32* %5)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %52
42: ; preds = %36
%43 = load i32, i32* %4
%44 = load i32, i32* %3
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = load i32, i32* %3
%49 = load i32, i32* %4
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %48, i32 %49)
store i32 -1, i32* %1
br label %52
51: ; preds = %42
store i32 0, i32* %1
br label %52
52: ; preds = %51, %46, %40, %29, %20, %10
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.pthreadpool = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"pthreadpool_init failed: %s\0A\00"
@.str.1 = [32 x i8] c"pthreadpool_destroy failed: %s\0A\00"
@.str.2 = [12 x i8] c"fork failed\00"
@.str.3 = [12 x i8] c"wait failed\00"
@.str.4 = [27 x i8] c"expected child %d, got %d\0A\00"
define i32 @test_fork() {
%1 = alloca %struct.pthreadpool*
%2 = alloca i32
%3 = bitcast %struct.pthreadpool** %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 @pthreadpool_init(i32 1, %struct.pthreadpool** %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i8* @strerror(i32 %5)
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %38
11: ; preds = %0
%12 = load %struct.pthreadpool*, %struct.pthreadpool** %1
%13 = call i32 @pthreadpool_destroy(%struct.pthreadpool* %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i8* @strerror(i32 %13)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %38
19: ; preds = %11
%20 = call i32 (...) @fork()
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %38
24: ; preds = %19
%25 = icmp eq i32 %20, 0
br i1 %25, label %26, label %28
26: ; preds = %24
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %24
%29 = call i32 @wait(i32* %2)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %38
33: ; preds = %28
%34 = icmp eq i32 %29, %20
br i1 %34, label %38, label %35
35: ; preds = %33
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %29)
br label %38
38: ; preds = %33, %35, %31, %22, %15, %7
%39 = phi i32 [ -1, %7 ], [ -1, %15 ], [ -1, %22 ], [ -1, %31 ], [ -1, %35 ], [ 0, %33 ]
ret i32 %39
}