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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int read(int, char *, int);
int readFromC(char *, char **);
int main(void) {
  char buf[1024];
  char *argv[] = {"ls", "-al", NULL};
  int pipe_fd = readFromC("/bin/ls", argv);
  int len = read(pipe_fd, buf, sizeof(buf));
  FILE *fp;
  fp = fopen("test.txt", "w");
  fwrite(buf, 1, len, fp);
  close(pipe_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c97(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    0x2c91(%rip),%rax
mov    %rax,-0x428(%rbp)
mov    0x2c8b(%rip),%rax
mov    %rax,-0x420(%rbp)
lea    -0x430(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1070 <readFromC@plt>
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x438(%rbp)
lea    0xe43(%rip),%rdi
lea    0xe45(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x440(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x438(%rbp),%edx
mov    -0x440(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x434(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    0x2cae(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c92(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe8b(%rip),%rdi
mov    %rsp,%rsi
call   1070 <readFromC@plt>
mov    %eax,%ebx
lea    0x20(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
lea    0xe6c(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1050 <fopen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ls\00"
@.str.1 = [4 x i8] c"-al\00"
@__const.main.argv = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [9 x i8] c"test.txt\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca [3 x i8*]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = bitcast [3 x i8*]* %3 to i8*
%8 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%9 = call i32 @readFromC(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 1024)
store i32 %12, i32* %5
%13 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %13, i32** %6
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* %5
%16 = load i32*, i32** %6
%17 = call i32 @fwrite(i8* %14, i32 1, i32 %15, i32* %16)
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [4 x i8] c"-al\00"
@__const.main.argv = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [9 x i8] c"test.txt\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca [3 x i8*]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast [3 x i8*]* %2 to i8*
%5 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%6 = call i32 @readFromC(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %5)
%7 = call i32 @read(i32 %6, i8* %3, i32 1024)
%8 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%9 = call i32 @fwrite(i8* %3, i32 1, i32 %7, i32* %8)
%10 = call i32 @close(i32 %6)
ret i32 0
}