#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__ ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *const);
int open(char *, int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ putenv(int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int spawn_bg(char *const *argv, char *const *extra_env) {
  int pipefd[2];
  if (pipe(pipefd) < 0)
    return -1;
  pid_t child_pid = fork();
  if (child_pid < 0)
    return -1;
  if (!child_pid) {
    close(pipefd[0]);
    pid_t grandson_pid = fork();
    if (grandson_pid < 0)
      _exit(EXIT_FAILURE);
    if (!grandson_pid) {
      chdir("/");
      int fd = open("/dev/null", O_RDONLY);
      dup2(fd, STDIN_FILENO);
      close(fd);
      fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
      if (extra_env) {
        while (*extra_env)
          putenv(*(extra_env++));
      }
      execvp(argv[0], argv);
      fprintf(stderr, "can't execute '%s'\n", argv[0]);
      fflush(stderr);
      char dummybyte = 0;
      write(pipefd[1], &dummybyte, 1);
    }
    _exit(EXIT_SUCCESS);
  }
  close(pipefd[1]);
  int status;
  waitpid(child_pid, &status, 0);
  if (WEXITSTATUS(status)) {
    close(pipefd[0]);
    return -1;
  }
  char dummybyte;
  ssize_t rd = read(pipefd[0], &dummybyte, 1);
  close(pipefd[0]);
  if (rd) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spawn_bg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
jge    122f <spawn_bg+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1400 <spawn_bg+0x200>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1251 <spawn_bg+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1400 <spawn_bg+0x200>
cmpq   $0x0,-0x28(%rbp)
jne    1388 <spawn_bg+0x188>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1288 <spawn_bg+0x88>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    137a <spawn_bg+0x17a>
lea    0xd66(%rip),%rdi
call   1060 <chdir@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
lea    0xd53(%rip),%rdi
call   1100 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
cmpq   $0x0,-0x18(%rbp)
je     132b <spawn_bg+0x12b>
jmp    12fa <spawn_bg+0xfa>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1326 <spawn_bg+0x126>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%rax
mov    %eax,%edi
call   10b0 <putenv@plt>
jmp    12fa <spawn_bg+0xfa>
jmp    132b <spawn_bg+0x12b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1110 <execvp@plt>
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xcba(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
movb   $0x0,-0x35(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x35(%rbp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    0x2c37(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
xor    %edx,%edx
call   10f0 <waitpid@plt>
mov    -0x3c(%rbp),%edi
call   10e0 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     13c5 <spawn_bg+0x1c5>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1400 <spawn_bg+0x200>
mov    -0x20(%rbp),%edi
lea    -0x3d(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
cmpq   $0x0,-0x48(%rbp)
je     13f9 <spawn_bg+0x1f9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1400 <spawn_bg+0x200>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_bg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     128f <spawn_bg+0x8f>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     128f <spawn_bg+0x8f>
je     129c <spawn_bg+0x9c>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1080 <close@plt>
lea    0x14(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   10f0 <waitpid@plt>
mov    0x14(%rsp),%edi
call   10e0 <WEXITSTATUS@plt>
test   %rax,%rax
je     126b <spawn_bg+0x6b>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%ebp
jmp    128f <spawn_bg+0x8f>
mov    0x8(%rsp),%edi
lea    0x13(%rsp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1080 <close@plt>
neg    %rbx
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
jns    12bf <spawn_bg+0xbf>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jne    137e <spawn_bg+0x17e>
lea    0xd34(%rip),%rdi
call   1060 <chdir@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
lea    0xd21(%rip),%rdi
call   1100 <open@plt>
mov    %eax,%ebx
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <dup2@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
mov    0x2cb5(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
test   %r15,%r15
je     133c <spawn_bg+0x13c>
mov    (%r15),%rdi
test   %rdi,%rdi
je     133c <spawn_bg+0x13c>
add    $0x8,%r15
call   10b0 <putenv@plt>
mov    (%r15),%rdi
add    $0x8,%r15
test   %rdi,%rdi
jne    132b <spawn_bg+0x12b>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1110 <execvp@plt>
mov    0x2c8a(%rip),%rbx
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0xcb2(%rip),%rsi
call   10c0 <fprintf@plt>
mov    (%rbx),%edi
call   10d0 <fflush@plt>
movb   $0x0,0x7(%rsp)
mov    0xc(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"can't execute '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @spawn_bg(i8** %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca i8
%13 = alloca i64
store i8** %0, i8*** %4
store i8** %1, i8*** %5
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %110
18: ; preds = %2
%19 = call i64 (...) @fork()
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %110
23: ; preds = %18
%24 = load i64, i64* %7
%25 = icmp ne i64 %24, 0
br i1 %25, label %86, label %26
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = call i64 (...) @fork()
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @_exit(i32 %34)
unreachable
36: ; preds = %26
%37 = load i64, i64* %8
%38 = icmp ne i64 %37, 0
br i1 %38, label %83, label %39
39: ; preds = %36
%40 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %41)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = load i32, i32* @STDIN_FILENO
%45 = call i32 @dup2(i32 %43, i32 %44)
%46 = load i32, i32* %9
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%49 = load i32, i32* %48
%50 = load i32, i32* @F_SETFD
%51 = load i32, i32* @FD_CLOEXEC
%52 = call i32 @fcntl(i32 %49, i32 %50, i32 %51)
%53 = load i8**, i8*** %5
%54 = icmp ne i8** %53, null
br i1 %54, label %55, label %67
55: ; preds = %39
br label %56
56: ; preds = %60, %55
%57 = load i8**, i8*** %5
%58 = load i8*, i8** %57
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %66
60: ; preds = %56
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i32 1
store i8** %62, i8*** %5
%63 = load i8*, i8** %61
%64 = ptrtoint i8* %63 to i32
%65 = call i32 @putenv(i32 %64)
br label %56
66: ; preds = %56
br label %67
67: ; preds = %66, %39
%68 = load i8**, i8*** %4
%69 = getelementptr inbounds i8*, i8** %68, i64 0
%70 = load i8*, i8** %69
%71 = load i8**, i8*** %4
%72 = call i32 @execvp(i8* %70, i8** %71)
%73 = load i32, i32* @stderr
%74 = load i8**, i8*** %4
%75 = getelementptr inbounds i8*, i8** %74, i64 0
%76 = load i8*, i8** %75
%77 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %76)
%78 = load i32, i32* @stderr
%79 = call i32 @fflush(i32 %78)
store i8 0, i8* %10
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%81 = load i32, i32* %80
%82 = call i32 @write(i32 %81, i8* %10, i32 1)
br label %83
83: ; preds = %67, %36
%84 = load i32, i32* @EXIT_SUCCESS
%85 = call i32 @_exit(i32 %84)
unreachable
86: ; preds = %23
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%88 = load i32, i32* %87
%89 = call i32 @close(i32 %88)
%90 = load i64, i64* %7
%91 = call i32 @waitpid(i64 %90, i32* %11, i32 0)
%92 = load i32, i32* %11
%93 = call i64 @WEXITSTATUS(i32 %92)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %99
95: ; preds = %86
%96 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%97 = load i32, i32* %96
%98 = call i32 @close(i32 %97)
store i32 -1, i32* %3
br label %110
99: ; preds = %86
%100 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%101 = load i32, i32* %100
%102 = call i64 @read(i32 %101, i8* %12, i32 1)
store i64 %102, i64* %13
%103 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%104 = load i32, i32* %103
%105 = call i32 @close(i32 %104)
%106 = load i64, i64* %13
%107 = icmp ne i64 %106, 0
br i1 %107, label %108, label %109
108: ; preds = %99
store i32 -1, i32* %3
br label %110
109: ; preds = %99
store i32 0, i32* %3
br label %110
110: ; preds = %109, %108, %95, %22, %17
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"can't execute '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @spawn_bg(i8** %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8
%7 = bitcast [2 x i32]* %3 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %85, label %11
11: ; preds = %2
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %85, label %14
14: ; preds = %11
%15 = icmp eq i64 %12, 0
br i1 %15, label %16, label %64
16: ; preds = %14
%17 = load i32, i32* %8
%18 = call i32 @close(i32 %17)
%19 = call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @_exit(i32 %22)
unreachable
24: ; preds = %16
%25 = icmp eq i64 %19, 0
br i1 %25, label %26, label %61
26: ; preds = %24
%27 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = call i32 @close(i32 %29)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i32, i32* @F_SETFD
%36 = load i32, i32* @FD_CLOEXEC
%37 = call i32 @fcntl(i32 %34, i32 %35, i32 %36)
%38 = icmp eq i8** %1, null
br i1 %38, label %51, label %39
39: ; preds = %26
%40 = load i8*, i8** %1
%41 = icmp eq i8* %40, null
br i1 %41, label %51, label %42
42: ; preds = %39, %42
%43 = phi i8* [ %49, %42 ], [ %40, %39 ]
%44 = phi i8** [ %45, %42 ], [ %1, %39 ]
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = ptrtoint i8* %43 to i64
%47 = trunc i64 %46 to i32
%48 = call i32 @putenv(i32 %47)
%49 = load i8*, i8** %45
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %42
51: ; preds = %42, %39, %26
%52 = load i8*, i8** %0
%53 = call i32 @execvp(i8* %52, i8** %0)
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %0
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @stderr
%58 = call i32 @fflush(i32 %57)
store i8 0, i8* %4
%59 = load i32, i32* %33
%60 = call i32 @write(i32 %59, i8* %4, i32 1)
br label %61
61: ; preds = %51, %24
%62 = load i32, i32* @EXIT_SUCCESS
%63 = call i32 @_exit(i32 %62)
unreachable
64: ; preds = %14
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%66 = load i32, i32* %65
%67 = call i32 @close(i32 %66)
%68 = bitcast i32* %5 to i8*
%69 = call i32 @waitpid(i64 %12, i32* %5, i32 0)
%70 = load i32, i32* %5
%71 = call i64 @WEXITSTATUS(i32 %70)
%72 = icmp eq i64 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %64
%74 = load i32, i32* %8
%75 = call i32 @close(i32 %74)
br label %83
76: ; preds = %64
%77 = load i32, i32* %8
%78 = call i64 @read(i32 %77, i8* %6, i32 1)
%79 = load i32, i32* %8
%80 = call i32 @close(i32 %79)
%81 = icmp ne i64 %78, 0
%82 = sext i1 %81 to i32
br label %83
83: ; preds = %76, %73
%84 = phi i32 [ -1, %73 ], [ %82, %76 ]
br label %85
85: ; preds = %83, %11, %2
%86 = phi i32 [ -1, %2 ], [ %84, %83 ], [ -1, %11 ]
ret i32 %86
}