#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ PR_SET_PDEATHSIG;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ close(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
scalar_t__ prctl(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ set_environment();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
int spawn(char *dir, const char *module) {
  int fds[2];
  pid_t childpid;
  char path[4096];
  snprintf(path, 4096, "%s/modules/%s", dir, module);
  ;
  pipe(fds);
  if ((childpid = fork()) == -1) {
    perror("fork");
    exit(-1);
  }
  if (childpid == 0) {
    if (close(fds[0]) < 0) {
      perror("closing child input");
    }
    if (dup2(fds[1], STDOUT_FILENO) < 0) {
      perror("setting module stdout");
    }
    if (prctl(PR_SET_PDEATHSIG, SIGHUP) < 0) {
      perror("setting up deathsig");
    }
    signal(SIGUSR1, SIG_IGN);
    set_environment();
    execlp(path, module, NULL);
    exit(1);
  } else {
    if (close(fds[1]) < 0) {
      perror("parent closing output");
    }
    fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL, 0) | O_NONBLOCK);
    return fds[0];
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x1000,%esi
lea    0xe12(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1225 <spawn+0x65>
lea    0xdf8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12dd <spawn+0x11d>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
jge    124d <spawn+0x8d>
lea    0xdcb(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    1274 <spawn+0xb4>
lea    0xdb8(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   10a0 <prctl@plt>
cmp    $0x0,%rax
jge    12a1 <spawn+0xe1>
lea    0xda1(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
mov    $0x0,%al
call   1060 <set_environment@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10d0 <execlp@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
jge    12fb <spawn+0x13b>
lea    0xd5b(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1028(%rbp)
mov    0x2ca5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2c8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x1028(%rbp),%edi
mov    -0x1024(%rbp),%esi
mov    %eax,%edx
mov    0x2c95(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x18(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%rbx
mov    %rdi,%rcx
lea    0xe2a(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
mov    %rbx,%r8
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1263 <spawn+0xa3>
cmp    $0xffffffff,%eax
je     12ff <spawn+0x13f>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
test   %rax,%rax
js     1255 <spawn+0x95>
mov    0x8(%rsp),%ebx
mov    0x2d91(%rip),%rax
mov    (%rax),%ebp
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2d96(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    0x8(%rsp),%eax
add    $0x1018,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf5(%rip),%rdi
call   10b0 <perror@plt>
jmp    1214 <spawn+0x54>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
test   %rax,%rax
jns    127d <spawn+0xbd>
lea    0xd9b(%rip),%rdi
call   10b0 <perror@plt>
mov    0xc(%rsp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
test   %rax,%rax
jns    12a0 <spawn+0xe0>
lea    0xd8c(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
call   10a0 <prctl@plt>
test   %rax,%rax
jns    12c8 <spawn+0x108>
lea    0xd7a(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
xor    %eax,%eax
call   1060 <set_environment@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   10d0 <execlp@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd08(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"%s/modules/%s\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [20 x i8] c"closing child input\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [22 x i8] c"setting module stdout\00"
@PR_SET_PDEATHSIG = global i32 0
@SIGHUP = global i32 0
@.str.4 = [20 x i8] c"setting up deathsig\00"
@SIGUSR1 = global i32 0
@SIG_IGN = global i32 0
@.str.5 = [22 x i8] c"parent closing output\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @spawn(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = load i8*, i8** %4
%11 = call i32 @snprintf(i8* %8, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
store i32 %14, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %53
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i64 @close(i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i64 @dup2(i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = load i32, i32* @PR_SET_PDEATHSIG
%39 = load i32, i32* @SIGHUP
%40 = call i64 @prctl(i32 %38, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = load i32, i32* @SIGUSR1
%46 = load i32, i32* @SIG_IGN
%47 = call i32 @signal(i32 %45, i32 %46)
%48 = call i32 (...) @set_environment()
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%50 = load i8*, i8** %4
%51 = call i32 @execlp(i8* %49, i8* %50, i32* null)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %19
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i64 @close(i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%62 = load i32, i32* %61
%63 = load i32, i32* @F_SETFL
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%65 = load i32, i32* %64
%66 = load i32, i32* @F_GETFL
%67 = call i32 @fcntl(i32 %65, i32 %66, i32 0)
%68 = load i32, i32* @O_NONBLOCK
%69 = or i32 %67, %68
%70 = call i32 @fcntl(i32 %62, i32 %63, i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%72 = load i32, i32* %71
ret i32 %72
}

LLVM IR (-O2):
@.str = [14 x i8] c"%s/modules/%s\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [20 x i8] c"closing child input\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [22 x i8] c"setting module stdout\00"
@PR_SET_PDEATHSIG = global i32 0
@SIGHUP = global i32 0
@.str.4 = [20 x i8] c"setting up deathsig\00"
@SIGUSR1 = global i32 0
@SIG_IGN = global i32 0
@.str.5 = [22 x i8] c"parent closing output\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @spawn(i8* %0, i8* %1) {
%3 = alloca [2 x i32]
%4 = alloca [4096 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i32 (...) @fork()
switch i32 %10, label %42 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %8
%16 = call i64 @close(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i32, i32* @STDOUT_FILENO
%24 = call i64 @dup2(i32 %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* @PR_SET_PDEATHSIG
%30 = load i32, i32* @SIGHUP
%31 = call i64 @prctl(i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* @SIGUSR1
%37 = load i32, i32* @SIG_IGN
%38 = call i32 @signal(i32 %36, i32 %37)
%39 = call i32 (...) @set_environment()
%40 = call i32 @execlp(i8* %6, i8* %1, i32* null)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %2
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i64 @close(i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %42
%50 = load i32, i32* %8
%51 = load i32, i32* @F_SETFL
%52 = load i32, i32* @F_GETFL
%53 = call i32 @fcntl(i32 %50, i32 %52, i32 0)
%54 = load i32, i32* @O_NONBLOCK
%55 = or i32 %54, %53
%56 = call i32 @fcntl(i32 %50, i32 %51, i32 %55)
%57 = load i32, i32* %8
ret i32 %57
}