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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char const *, char *const *);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int readFromC(const char *cmd, char *const argv[]) {
  pid_t pid;
  int fd[2];
  if (pipe(fd) == -1)
    return -1;
  pid = fork();
  if (pid < 0) {
    return -2;
  } else if (pid != 0) {
    wait(NULL);
    close(fd[1]);
  } else {
    close(fd[0]);
    dup2(fd[1], 1);
    execv(cmd, argv);
    close(fd[1]);
  }
  return fd[0];
}


x86 assembly (clang, -O0):
<readFromC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    118e <readFromC+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <readFromC+0xa6>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11b0 <readFromC+0x50>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1206 <readFromC+0xa6>
cmpq   $0x0,-0x20(%rbp)
je     11d1 <readFromC+0x71>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    11fb <readFromC+0x9b>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <execv@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    1200 <readFromC+0xa0>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFromC>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     118e <readFromC+0x2e>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     1195 <readFromC+0x35>
je     119c <readFromC+0x3c>
xor    %edi,%edi
call   1060 <wait@plt>
jmp    11bd <readFromC+0x5d>
mov    $0xffffffff,%eax
jmp    11c9 <readFromC+0x69>
mov    $0xfffffffe,%eax
jmp    11c9 <readFromC+0x69>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <execv@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @readFromC(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [2 x i32]
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %42
12: ; preds = %2
%13 = call i64 (...) @fork()
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -2, i32* %3
br label %42
17: ; preds = %12
%18 = load i64, i64* %6
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = call i32 @wait(i32* null)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
br label %38
25: ; preds = %17
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @dup2(i32 %30, i32 1)
%32 = load i8*, i8** %4
%33 = load i8**, i8*** %5
%34 = call i32 @execv(i8* %32, i8** %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %25, %20
br label %39
39: ; preds = %38
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%41 = load i32, i32* %40
store i32 %41, i32* %3
br label %42
42: ; preds = %39, %16, %11
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
define i32 @readFromC(i8* %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %28, label %8
8: ; preds = %2
%9 = call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %8
%12 = icmp eq i64 %9, 0
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = call i32 @wait(i32* null)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %23
16: ; preds = %11
%17 = load i32, i32* %5
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = call i32 @execv(i8* %0, i8** %1)
br label %23
23: ; preds = %13, %16
%24 = phi i32* [ %15, %13 ], [ %19, %16 ]
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %5
br label %28
28: ; preds = %8, %2, %23
%29 = phi i32 [ %27, %23 ], [ -1, %2 ], [ -2, %8 ]
ret i32 %29
}