#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ say(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
pid_t dofork(void (*func)(void)) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    say("fork: %s\n", strerror(errno));
    return -1;
  }
  if (pid == 0) {
    func();
    exit(0);
  }
  return pid;
}


x86 assembly (clang, -O0):
<dofork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    118b <dofork+0x4b>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    %eax,%esi
lea    0xe87(%rip),%rdi
call   1060 <say@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a8 <dofork+0x68>
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <dofork+0x60>
call   *-0x10(%rbp)
xor    %edi,%edi
call   1030 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dofork>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
js     1154 <dofork+0x14>
je     1179 <dofork+0x39>
pop    %rbx
ret
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe97(%rip),%rdi
mov    %eax,%esi
call   1060 <say@plt>
mov    $0xffffffffffffffff,%rax
pop    %rbx
ret
call   *%rbx
xor    %edi,%edi
call   1030 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"fork: %s\0A\00"
@errno = global i32 0
define i64 @dofork(void ()* %0) {
%2 = alloca i64
%3 = alloca void ()*
%4 = alloca i64
store void ()* %0, void ()** %3
%5 = call i64 (...) @fork()
store i64 %5, i64* %4
%6 = load i64, i64* %4
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 @say(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i64 -1, i64* %2
br label %20
12: ; preds = %1
%13 = load i64, i64* %4
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load void ()*, void ()** %3
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %12
%19 = load i64, i64* %4
store i64 %19, i64* %2
br label %20
20: ; preds = %18, %8
%21 = load i64, i64* %2
ret i64 %21
}

LLVM IR (-O2):
@.str = [10 x i8] c"fork: %s\0A\00"
@errno = global i32 0
define i64 @dofork(void ()* nocapture %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i32 @strerror(i32 %5)
%7 = tail call i32 @say(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %12
8: ; preds = %1
%9 = icmp eq i64 %2, 0
br i1 %9, label %10, label %12
10: ; preds = %8
tail call void %0()
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8, %4
%13 = phi i64 [ -1, %4 ], [ %2, %8 ]
ret i64 %13
}