#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ list_tar(int);
int pipe(int *);
void run_index_reader(int fdin, int *fdout, int closechild1) {
  int pid;
  int res;
  int mypipe[2];
  res = pipe(mypipe);
  if (res == -1)
    error("Cannot pipe");
  pid = fork();
  if (pid == -1)
    error("Cannot fork");
  if (pid == 0) {
    close(0);
    close(mypipe[0]);
    close(closechild1);
    list_tar(fdin);
    exit(0);
  } else {
    close(mypipe[1]);
    *fdout = mypipe[0];
    close(fdin);
  }
}


x86 assembly (clang, -O0):
<run_index_reader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1194 <run_index_reader+0x34>
lea    0xe71(%rip),%rdi
call   1060 <error@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b4 <run_index_reader+0x54>
lea    0xe5d(%rip),%rdi
call   1060 <error@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11e4 <run_index_reader+0x84>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <list_tar@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_index_reader>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    118b <run_index_reader+0x2b>
lea    0xe7a(%rip),%rdi
call   1060 <error@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11c1 <run_index_reader+0x61>
test   %eax,%eax
jne    11cd <run_index_reader+0x6d>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1030 <list_tar@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe44(%rip),%rdi
call   1060 <error@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"Cannot pipe\00"
@.str.1 = [12 x i8] c"Cannot fork\00"
define void @run_index_reader(i32 %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = call i32 (...) @fork()
switch i32 %12, label %22 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %11
%14 = call i32 @error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %22
15: ; preds = %11
%16 = call i32 @close(i32 0)
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
%19 = call i32 @close(i32 %2)
%20 = call i32 @list_tar(i32 %0)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %13, %11
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %6
store i32 %26, i32* %1
%27 = call i32 @close(i32 %0)
ret void
}