#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*/ close(int);
int errno;
int fdevent_fork_execve(char *, char **, int /*<<< orphan*/ *, int const, int,
                        int, int);
int fdevent_open_devnull();
pid_t fdlog_pipe_spawn(const char *const fn, const int rfd) {
  char *args[4];
  int devnull = fdevent_open_devnull();
  pid_t pid;
  if (-1 == devnull) {
    return -1;
  }
  *(const char **)&args[0] = "/bin/sh";
  *(const char **)&args[1] = "-c";
  *(const char **)&args[2] = fn;
  args[3] = NULL;
  pid = fdevent_fork_execve(args[0], args, NULL, rfd, devnull, devnull, -1);
  if (pid > 0) {
    close(devnull);
  } else {
    int errnum = errno;
    close(devnull);
    errno = errnum;
  }
  return pid;
}


x86 assembly (clang, -O0):
<fdlog_pipe_spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1030 <fdevent_open_devnull@plt>
mov    %eax,-0x44(%rbp)
mov    $0xffffffff,%eax
cmp    -0x44(%rbp),%eax
jne    1163 <fdlog_pipe_spawn+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <fdlog_pipe_spawn+0xbc>
lea    0xe96(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    -0x44(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edx
movl   $0xffffffff,(%rsp)
call   1040 <fdevent_fork_execve@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jle    11c6 <fdlog_pipe_spawn+0x96>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
jmp    11e6 <fdlog_pipe_spawn+0xb6>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x4c(%rbp),%ecx
mov    0x2dfc(%rip),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdlog_pipe_spawn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
xor    %eax,%eax
call   1030 <fdevent_open_devnull@plt>
cmp    $0xffffffff,%eax
je     119f <fdlog_pipe_spawn+0x6f>
mov    %eax,%ebx
lea    0xeab(%rip),%rdi
mov    %rdi,0x10(%rsp)
lea    0xea7(%rip),%rax
mov    %rax,0x18(%rsp)
mov    %rbp,0x20(%rsp)
movq   $0x0,0x28(%rsp)
movl   $0xffffffff,(%rsp)
lea    0x10(%rsp),%rsi
xor    %edx,%edx
mov    %r14d,%ecx
mov    %ebx,%r8d
mov    %ebx,%r9d
call   1040 <fdevent_fork_execve@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11a6 <fdlog_pipe_spawn+0x76>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11ba <fdlog_pipe_spawn+0x8a>
mov    $0xffffffff,%ebp
jmp    11ba <fdlog_pipe_spawn+0x8a>
mov    0x2e33(%rip),%r14
mov    (%r14),%r15d
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,(%r14)
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@errno = global i32 0
define i32 @fdlog_pipe_spawn(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4 x i8*]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = call i32 (...) @fdevent_open_devnull()
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %39
14: ; preds = %2
%15 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %16
%17 = load i8*, i8** %4
%18 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 2
store i8* %17, i8** %18
%19 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 3
store i8* null, i8** %19
%20 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
%23 = load i32, i32* %5
%24 = load i32, i32* %7
%25 = load i32, i32* %7
%26 = call i32 @fdevent_fork_execve(i8* %21, i8** %22, i32* null, i32 %23, i32 %24, i32 %25, i32 -1)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %14
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
br label %37
32: ; preds = %14
%33 = load i32, i32* @errno
store i32 %33, i32* %9
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %9
store i32 %36, i32* @errno
br label %37
37: ; preds = %32, %29
%38 = load i32, i32* %8
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %13
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@errno = global i32 0
define i32 @fdlog_pipe_spawn(i8* %0, i32 %1) {
%3 = alloca [4 x i8*]
%4 = bitcast [4 x i8*]* %3 to i8*
%5 = tail call i32 (...) @fdevent_open_devnull()
%6 = icmp eq i32 %5, -1
br i1 %6, label %19, label %7
7: ; preds = %2
%8 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 2
store i8* %0, i8** %10
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 3
store i8* null, i8** %11
%12 = call i32 @fdevent_fork_execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %8, i32* null, i32 %1, i32 %5, i32 %5, i32 -1)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = call i32 @close(i32 %5)
br label %19
16: ; preds = %7
%17 = load i32, i32* @errno
%18 = call i32 @close(i32 %5)
store i32 %17, i32* @errno
br label %19
19: ; preds = %14, %16, %2
%20 = phi i32 [ -1, %2 ], [ %12, %16 ], [ %12, %14 ]
ret i32 %20
}