#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ _debug(char *, int, int /*<<< orphan*/, char *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ cleanup_fds();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
pid_t safe_fork(void) {
  pid_t result;
  result = fork();
  if (result == -1) {
    _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/richardhmm/HMMCodeRepository/refs/heads/"
           "master/c_demo/src/safe.c",
           189, LOG_CRIT, "Failed to fork: %s.  Bailing out", strerror(errno));
    exit(1);
  } else if (result == 0) {
    cleanup_fds();
  }
  return result;
}


x86 assembly (clang, -O0):
<safe_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11ae <safe_fork+0x5e>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x8(%rbp),%edx
mov    %eax,%r8d
lea    0xe6d(%rip),%rdi
mov    $0xbd,%esi
lea    0xefc(%rip),%rcx
call   1030 <_debug@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11bf <safe_fork+0x6f>
mov    $0x0,%al
call   1040 <cleanup_fds@plt>
jmp    11c4 <safe_fork+0x74>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fork>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11a1 <safe_fork+0x51>
cmp    $0xffffffff,%ebx
jne    11a8 <safe_fork+0x58>
mov    0x2e7e(%rip),%rax
mov    (%rax),%ebx
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7f(%rip),%rdi
lea    0xf13(%rip),%rcx
mov    $0xbd,%esi
mov    %ebx,%edx
mov    %eax,%r8d
call   1030 <_debug@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
call   1040 <cleanup_fds@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [155 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/richardhmm/HMMCodeRepository/refs/heads/master/c_demo/src/safe.c\00"
@LOG_CRIT = global i32 0
@.str.1 = [33 x i8] c"Failed to fork: %s. Bailing out\00"
@errno = global i32 0
define i32 @safe_fork() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = load i32, i32* @LOG_CRIT
%7 = load i32, i32* @errno
%8 = call i32 @strerror(i32 %7)
%9 = call i32 @_debug(i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str, i64 0, i64 0), i32 189, i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %1
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (...) @cleanup_fds()
br label %16
16: ; preds = %14, %11
br label %17
17: ; preds = %16
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@.str = [155 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/richardhmm/HMMCodeRepository/refs/heads/master/c_demo/src/safe.c\00"
@LOG_CRIT = global i32 0
@.str.1 = [33 x i8] c"Failed to fork: %s. Bailing out\00"
@errno = global i32 0
define i32 @safe_fork() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %10 [
i32 -1, label %2
i32 0, label %8
]
2: ; preds = %0
%3 = load i32, i32* @LOG_CRIT
%4 = load i32, i32* @errno
%5 = tail call i32 @strerror(i32 %4)
%6 = tail call i32 @_debug(i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str, i64 0, i64 0), i32 189, i32 %3, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 (...) @cleanup_fds()
br label %10
10: ; preds = %0, %8
ret i32 %1
}