#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 /*<<< orphan*/ posix_spawn_file_actions_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ perror(char *);
int posix_spawn(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *,
                int /*<<< orphan*/ *, char **, char **);
int posix_spawn_file_actions_adddup2(int /*<<< orphan*/ *, int, int);
int posix_spawn_file_actions_destroy(int /*<<< orphan*/ *);
int posix_spawn_file_actions_init(int /*<<< orphan*/ *);
int main(void) {
  int res = 1;
  int err;
  posix_spawn_file_actions_t file_actions;
  char *argv_exe[] = {"true", NULL};
  char *envv_exe[] = {NULL};
  err = posix_spawn_file_actions_init(&file_actions);
  if (err != 0) {
    errno = err;
    perror("posix_spawn_file_actions_init");
    return 1;
  }
  err = posix_spawn_file_actions_adddup2(&file_actions, 3, 4);
  if (err != 0) {
    errno = err;
    perror("posix_spawn_file_actions_adddup2");
    goto out;
  }
  err = posix_spawn(NULL, "/bin/true", &file_actions, NULL, argv_exe, envv_exe);
  if (err != 0) {
    errno = err;
    perror("posix_spawn");
    goto out;
  }
  res = 0;
out:
  err = posix_spawn_file_actions_destroy(&file_actions);
  if (err != 0) {
    errno = err;
    perror("posix_spawn_file_actions_destroy");
    res = 1;
  }
  return res;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    0x2c83(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c80(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1030 <posix_spawn_file_actions_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11d6 <main+0x76>
mov    -0xc(%rbp),%ecx
mov    0x2e1c(%rip),%rax
mov    %ecx,(%rax)
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x13e>
lea    -0x10(%rbp),%rdi
mov    $0x3,%esi
mov    $0x4,%edx
call   1080 <posix_spawn_file_actions_adddup2@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1213 <main+0xb3>
mov    -0xc(%rbp),%ecx
mov    0x2dd8(%rip),%rax
mov    %ecx,(%rax)
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
jmp    1263 <main+0x103>
lea    -0x20(%rbp),%r8
lea    -0x28(%rbp),%r9
xor    %eax,%eax
mov    %eax,%ecx
lea    0xe19(%rip),%rsi
lea    -0x10(%rbp),%rdx
mov    %rcx,%rdi
call   1070 <posix_spawn@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     125c <main+0xfc>
mov    -0xc(%rbp),%ecx
mov    0x2d8f(%rip),%rax
mov    %ecx,(%rax)
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
jmp    1263 <main+0x103>
movl   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <posix_spawn_file_actions_destroy@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1298 <main+0x138>
mov    -0xc(%rbp),%ecx
mov    0x2d55(%rip),%rax
mov    %ecx,(%rax)
lea    0xdc9(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movaps 0x2ca4(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x4(%rsp),%rdi
call   1030 <posix_spawn_file_actions_init@plt>
test   %eax,%eax
jne    11cb <main+0x7b>
lea    0x4(%rsp),%rdi
mov    $0x3,%esi
mov    $0x4,%edx
call   1070 <posix_spawn_file_actions_adddup2@plt>
test   %eax,%eax
jne    11d4 <main+0x84>
lea    0xea8(%rip),%rsi
xor    %ebx,%ebx
lea    0x4(%rsp),%rdx
lea    0x10(%rsp),%r8
lea    0x8(%rsp),%r9
xor    %edi,%edi
xor    %ecx,%ecx
call   1060 <posix_spawn@plt>
test   %eax,%eax
jne    11f9 <main+0xa9>
lea    0x4(%rsp),%rdi
call   1040 <posix_spawn_file_actions_destroy@plt>
test   %eax,%eax
jne    11dd <main+0x8d>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe2e(%rip),%rdi
jmp    11e4 <main+0x94>
lea    0xe43(%rip),%rdi
jmp    1200 <main+0xb0>
lea    0xe71(%rip),%rdi
mov    0x2ded(%rip),%rcx
mov    %eax,(%rcx)
call   1050 <perror@plt>
mov    $0x1,%ebx
jmp    11c3 <main+0x73>
lea    0xe49(%rip),%rdi
mov    0x2dd1(%rip),%rcx
mov    %eax,(%rcx)
call   1050 <perror@plt>
mov    $0x1,%ebx
jmp    11b5 <main+0x65>

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

LLVM IR (-O2):
@.str = [5 x i8] c"true\00"
@__const.main.argv_exe = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@errno = global i32 0
@.str.1 = [30 x i8] c"posix_spawn_file_actions_init\00"
@.str.2 = [33 x i8] c"posix_spawn_file_actions_adddup2\00"
@.str.3 = [10 x i8] c"/bin/true\00"
@.str.4 = [12 x i8] c"posix_spawn\00"
@.str.5 = [33 x i8] c"posix_spawn_file_actions_destroy\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i8*]
%3 = alloca [1 x i8*]
%4 = bitcast i32* %1 to i8*
%5 = bitcast [2 x i8*]* %2 to i8*
%6 = bitcast [1 x i8*]* %3 to i8*
%7 = bitcast [1 x i8*]* %3 to i64*
store i64 0, i64* %7
%8 = call i32 @posix_spawn_file_actions_init(i32* %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %0
%11 = call i32 @posix_spawn_file_actions_adddup2(i32* %1, i32 3, i32 4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
%15 = getelementptr inbounds [1 x i8*], [1 x i8*]* %3, i64 0, i64 0
%16 = call i32 @posix_spawn(i32* null, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* %1, i32* null, i8** %14, i8** %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13, %10
%19 = phi i32 [ %11, %10 ], [ %16, %13 ]
%20 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), %13 ]
store i32 %19, i32* @errno
%21 = call i32 @perror(i8* %20)
br label %22
22: ; preds = %18, %13
%23 = phi i32 [ 0, %13 ], [ 1, %18 ]
%24 = call i32 @posix_spawn_file_actions_destroy(i32* %1)
%25 = icmp eq i32 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22, %0
%27 = phi i32 [ %8, %0 ], [ %24, %22 ]
%28 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), %22 ]
store i32 %27, i32* @errno
%29 = call i32 @perror(i8* %28)
br label %30
30: ; preds = %26, %22
%31 = phi i32 [ %23, %22 ], [ 1, %26 ]
ret i32 %31
}