#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void run_backend(void) {
  pid_t pid = fork();
  if (pid == 0) {
    FILE *fd = fopen("/dev/null", "r");
    if (fd < 0) {
      perror("open file fail:");
      exit(EXIT_FAILURE);
    }
    if (fd != NULL) {
      dup2(fileno(fd), 0);
      fclose(fd);
    }
    fd = fopen("/dev/null", "w");
    if (fd != NULL) {
      dup2(fileno(fd), 1);
      dup2(fileno(fd), 2);
      fclose(fd);
    }
    setsid();
  } else if (pid > 0) {
    fprintf(stdout, "%d\n", (int)pid);
    exit(EXIT_SUCCESS);
  } else {
    fprintf(stderr, "could not fork new process:%m\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<run_backend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1271 <run_backend+0xe1>
lea    0xe4b(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jae    11ea <run_backend+0x5a>
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     1210 <run_backend+0x80>
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xde9(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1265 <run_backend+0xd5>
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
jmp    12cc <run_backend+0x13c>
cmpq   $0x0,-0x8(%rbp)
jle    12a7 <run_backend+0x117>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    %eax,%edx
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_backend>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    1218 <run_backend+0x98>
lea    0xe68(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c5 <run_backend+0x45>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe34(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1210 <run_backend+0x90>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %rbx,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
jmp    1050 <setsid@plt>
jle    1241 <run_backend+0xc1>
mov    0x2daf(%rip),%rcx
mov    (%rcx),%edi
lea    0xde4(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"open file fail:\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.5 = [31 x i8] c"could not fork new process:%m\0A\00"
define void @run_backend() {
%1 = alloca i64
%2 = alloca i32*
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %38
6: ; preds = %0
%7 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ult i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %6
%15 = load i32*, i32** %2
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32*, i32** %2
%19 = call i32 @fileno(i32* %18)
%20 = call i32 @dup2(i32 %19, i32 0)
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
br label %23
23: ; preds = %17, %14
%24 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %24, i32** %2
%25 = load i32*, i32** %2
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32*, i32** %2
%29 = call i32 @fileno(i32* %28)
%30 = call i32 @dup2(i32 %29, i32 1)
%31 = load i32*, i32** %2
%32 = call i32 @fileno(i32* %31)
%33 = call i32 @dup2(i32 %32, i32 2)
%34 = load i32*, i32** %2
%35 = call i32 @fclose(i32* %34)
br label %36
36: ; preds = %27, %23
%37 = call i32 (...) @setsid()
br label %53
38: ; preds = %0
%39 = load i64, i64* %1
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i32, i32* @stdout
%43 = load i64, i64* %1
%44 = trunc i64 %43 to i32
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %38
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %36
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.5 = [31 x i8] c"could not fork new process:%m\0A\00"
define void @run_backend() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %21
3: ; preds = %0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = tail call i32 @fileno(i32* %4)
%8 = tail call i32 @dup2(i32 %7, i32 0)
%9 = tail call i32 @fclose(i32* %4)
br label %10
10: ; preds = %6, %3
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = tail call i32 @fileno(i32* %11)
%15 = tail call i32 @dup2(i32 %14, i32 1)
%16 = tail call i32 @fileno(i32* %11)
%17 = tail call i32 @dup2(i32 %16, i32 2)
%18 = tail call i32 @fclose(i32* %11)
br label %19
19: ; preds = %13, %10
%20 = tail call i32 (...) @setsid()
ret void
21: ; preds = %0
%22 = icmp sgt i64 %1, 0
br i1 %22, label %23, label %29
23: ; preds = %21
%24 = load i32, i32* @stdout
%25 = trunc i64 %1 to i32
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @EXIT_SUCCESS
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
}