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

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pipe(int *);
pid_t swallow_cmd(FILE **send, FILE **receive, char *cmd, char *argv[],
                  int noblock) {
  int fdin[2], fdout[2];
  pid_t childpid;
  pipe(fdin);
  pipe(fdout);
  if ((childpid = fork()) == -1) {
    return (-1);
  }
  if (childpid == 0) {
    close(fdin[1]);
    close(fdout[0]);
    close(0);
    dup(fdin[0]);
    close(1);
    dup(fdout[1]);
    execvp(cmd, argv);
    exit(-1);
  } else {
    close(fdin[0]);
    close(fdout[1]);
    if ((noblock && (fcntl(fdout[0], F_SETFL, O_NONBLOCK) == -1)) ||
        ((*send = fdopen(fdin[1], "w")) == NULL) ||
        ((*receive = fdopen(fdout[0], "r")) == NULL)) {
      if (*receive != NULL)
        fclose(*receive);
      if (*send != NULL)
        fclose(*send);
      kill(childpid, SIGHUP);
      kill(childpid, SIGKILL);
      return (-1);
    }
    return (childpid);
  }
}


x86 assembly (clang, -O0):
<swallow_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
lea    -0x34(%rbp),%rdi
call   1070 <pipe@plt>
lea    -0x3c(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x40(%rbp)
cmp    $0xffffffff,%eax
jne    11ed <swallow_cmd+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <swallow_cmd+0x184>
cmpl   $0x0,-0x40(%rbp)
jne    123f <swallow_cmd+0x9f>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
call   1050 <dup@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%edi
call   1050 <dup@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <execvp@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
je     127c <swallow_cmd+0xdc>
mov    -0x3c(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12bc <swallow_cmd+0x11c>
mov    -0x30(%rbp),%edi
lea    0xd7a(%rip),%rsi
call   1090 <fdopen@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
je     12bc <swallow_cmd+0x11c>
mov    -0x3c(%rbp),%edi
lea    0xd5c(%rip),%rsi
call   1090 <fdopen@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    131e <swallow_cmd+0x17e>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12d6 <swallow_cmd+0x136>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     12f0 <swallow_cmd+0x150>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%edi
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
mov    -0x40(%rbp),%edi
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <swallow_cmd+0x184>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swallow_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1292 <swallow_cmd+0xf2>
test   %ebx,%ebx
je     12a3 <swallow_cmd+0x103>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
test   %ebp,%ebp
je     1221 <swallow_cmd+0x81>
mov    0x8(%rsp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1253 <swallow_cmd+0xb3>
mov    0x14(%rsp),%edi
lea    0xdd4(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1253 <swallow_cmd+0xb3>
mov    0x8(%rsp),%edi
lea    0xdbe(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    1292 <swallow_cmd+0xf2>
jmp    1260 <swallow_cmd+0xc0>
mov    (%r15),%rdi
test   %rdi,%rdi
je     1260 <swallow_cmd+0xc0>
call   1040 <fclose@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     126d <swallow_cmd+0xcd>
call   1040 <fclose@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <kill@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <kill@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
call   1050 <dup@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <dup@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   10a0 <execvp@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@SIGHUP = global i32 0
@SIGKILL = global i32 0
define i32 @swallow_cmd(i32** %0, i32** %1, i8* %2, i8** %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32**
%8 = alloca i32**
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca [2 x i32]
%13 = alloca [2 x i32]
%14 = alloca i32
store i32** %0, i32*** %7
store i32** %1, i32*** %8
store i8* %2, i8** %9
store i8** %3, i8*** %10
store i32 %4, i32* %11
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %14
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 -1, i32* %6
br label %97
22: ; preds = %5
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = call i32 @close(i32 0)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @dup(i32 %34)
%36 = call i32 @close(i32 1)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @dup(i32 %38)
%40 = load i8*, i8** %9
%41 = load i8**, i8*** %10
%42 = call i32 @execvp(i8* %40, i8** %41)
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %22
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %11
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %44
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%55 = load i32, i32* %54
%56 = load i32, i32* @F_SETFL
%57 = load i32, i32* @O_NONBLOCK
%58 = call i32 @fcntl(i32 %55, i32 %56, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %72, label %60
60: ; preds = %53, %44
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32* @fdopen(i32 %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%64 = load i32**, i32*** %7
store i32* %63, i32** %64
%65 = icmp eq i32* %63, null
br i1 %65, label %72, label %66
66: ; preds = %60
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%68 = load i32, i32* %67
%69 = call i32* @fdopen(i32 %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%70 = load i32**, i32*** %8
store i32* %69, i32** %70
%71 = icmp eq i32* %69, null
br i1 %71, label %72, label %95
72: ; preds = %66, %60, %53
%73 = load i32**, i32*** %8
%74 = load i32*, i32** %73
%75 = icmp ne i32* %74, null
br i1 %75, label %76, label %80
76: ; preds = %72
%77 = load i32**, i32*** %8
%78 = load i32*, i32** %77
%79 = call i32 @fclose(i32* %78)
br label %80
80: ; preds = %76, %72
%81 = load i32**, i32*** %7
%82 = load i32*, i32** %81
%83 = icmp ne i32* %82, null
br i1 %83, label %84, label %88
84: ; preds = %80
%85 = load i32**, i32*** %7
%86 = load i32*, i32** %85
%87 = call i32 @fclose(i32* %86)
br label %88
88: ; preds = %84, %80
%89 = load i32, i32* %14
%90 = load i32, i32* @SIGHUP
%91 = call i32 @kill(i32 %89, i32 %90)
%92 = load i32, i32* %14
%93 = load i32, i32* @SIGKILL
%94 = call i32 @kill(i32 %92, i32 %93)
store i32 -1, i32* %6
br label %97
95: ; preds = %66
%96 = load i32, i32* %14
store i32 %96, i32* %6
br label %97
97: ; preds = %95, %88, %21
%98 = load i32, i32* %6
ret i32 %98
}

LLVM IR (-O2):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@SIGHUP = global i32 0
@SIGKILL = global i32 0
define i32 @swallow_cmd(i32** nocapture %0, i32** nocapture %1, i8* %2, i8** %3, i32 %4) {
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = bitcast [2 x i32]* %6 to i8*
%9 = bitcast [2 x i32]* %7 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
switch i32 %14, label %30 [
i32 -1, label %67
i32 0, label %15
]
15: ; preds = %5
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %12
%20 = call i32 @close(i32 %19)
%21 = call i32 @close(i32 0)
%22 = load i32, i32* %10
%23 = call i32 @dup(i32 %22)
%24 = call i32 @close(i32 1)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup(i32 %26)
%28 = call i32 @execvp(i8* %2, i8** %3)
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %5
%31 = load i32, i32* %10
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = icmp eq i32 %4, 0
br i1 %36, label %43, label %37
37: ; preds = %30
%38 = load i32, i32* %12
%39 = load i32, i32* @F_SETFL
%40 = load i32, i32* @O_NONBLOCK
%41 = call i32 @fcntl(i32 %38, i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %52, label %43
43: ; preds = %37, %30
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32* @fdopen(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %46, i32** %0
%47 = icmp eq i32* %46, null
br i1 %47, label %52, label %48
48: ; preds = %43
%49 = load i32, i32* %12
%50 = call i32* @fdopen(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %50, i32** %1
%51 = icmp eq i32* %50, null
br i1 %51, label %57, label %67
52: ; preds = %43, %37
%53 = load i32*, i32** %1
%54 = icmp eq i32* %53, null
br i1 %54, label %57, label %55
55: ; preds = %52
%56 = call i32 @fclose(i32* %53)
br label %57
57: ; preds = %48, %55, %52
%58 = load i32*, i32** %0
%59 = icmp eq i32* %58, null
br i1 %59, label %62, label %60
60: ; preds = %57
%61 = call i32 @fclose(i32* %58)
br label %62
62: ; preds = %60, %57
%63 = load i32, i32* @SIGHUP
%64 = call i32 @kill(i32 %14, i32 %63)
%65 = load i32, i32* @SIGKILL
%66 = call i32 @kill(i32 %14, i32 %65)
br label %67
67: ; preds = %48, %5, %62
%68 = phi i32 [ -1, %62 ], [ %14, %5 ], [ %14, %48 ]
ret i32 %68
}