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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *pipe_fp, *infile;
  char readbuf[80];
  if (argc != 3) {
    fprintf(stderr, "USAGE:  popen3 [command] [filename]\n");
    exit(1);
  }
  if ((infile = fopen(argv[2], "rt")) == NULL) {
    perror("fopen");
    exit(1);
  }
  if ((pipe_fp = popen(argv[1], "w")) == NULL) {
    perror("popen");
    exit(1);
  }
  do {
    fgets(readbuf, 80, infile);
    if (feof(infile))
      break;
    fputs(readbuf, pipe_fp);
  } while (!feof(infile));
  fclose(infile);
  pclose(pipe_fp);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11df <main+0x3f>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe37(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1217 <main+0x77>
lea    0xe20(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe08(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    124f <main+0xaf>
lea    0xdf0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1254 <main+0xb4>
lea    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     127e <main+0xde>
jmp    12a5 <main+0x105>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1254 <main+0xb4>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
cmp    $0x3,%edi
jne    1241 <main+0xa1>
mov    %rsi,%r14
mov    0x10(%rsi),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1260 <main+0xc0>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
lea    0xe52(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1269 <main+0xc9>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    1225 <main+0x85>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     11f0 <main+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdc1(%rip),%rdi
jmp    1270 <main+0xd0>
lea    0xdc0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"USAGE: popen3 [command] [filename]\0A\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"popen\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [80 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %7
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32* @popen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %28, i32** %6
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %24
br label %34
34: ; preds = %46, %33
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%36 = load i32*, i32** %7
%37 = call i32 @fgets(i8* %35, i32 80, i32* %36)
%38 = load i32*, i32** %7
%39 = call i64 @feof(i32* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
br label %51
42: ; preds = %34
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%44 = load i32*, i32** %6
%45 = call i32 @fputs(i8* %43, i32* %44)
br label %46
46: ; preds = %42
%47 = load i32*, i32** %7
%48 = call i64 @feof(i32* %47)
%49 = icmp ne i64 %48, 0
%50 = xor i1 %49, true
br i1 %50, label %34, label %51
51: ; preds = %46, %41
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %6
%55 = call i32 @pclose(i32* %54)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"USAGE: popen3 [command] [filename]\0A\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"popen\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32* @popen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %18, %30
%27 = call i32 @fgets(i8* %4, i32 80, i32* %13)
%28 = call i64 @feof(i32* %13)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i32 @fputs(i8* %4, i32* %21)
%32 = call i64 @feof(i32* %13)
%33 = icmp eq i64 %32, 0
br i1 %33, label %26, label %34
34: ; preds = %26, %30
%35 = call i32 @fclose(i32* %13)
%36 = call i32 @pclose(i32* %21)
ret i32 0
}