#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 ssize_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int close(int);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, size_t);
int feed_pipe(int pipefd[2], const char *filename) {
  int fd;
  ssize_t count;
  char buf[512];
  if (close(pipefd[1]) == -1) {
    perror("Error closing end of pipe");
    return 6;
  }
  if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == -1) {
    perror("Error opening file for redirection");
    return 7;
  }
  for (;;) {
    if ((count = read(pipefd[0], buf, sizeof buf)) == -1) {
      perror("Error reading from pipe");
      return 8;
    }
    if (count == 0)
      break;
    if (write(fd, buf, (size_t)count) == -1) {
      perror("Error writing to pipe");
      return 9;
    }
  }
  if (close(fd) == -1) {
    perror("Error closing file descriptor for I/O file");
    return 6;
  }
  if (close(pipefd[0]) == -1) {
    perror("Error closing redirection pipe");
    return 6;
  }
  return 0;
}


x86 assembly (clang, -O0):
<feed_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1190 <feed_pipe+0x40>
lea    0xe81(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    12e0 <feed_pipe+0x190>
mov    -0x18(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    0x2e11(%rip),%rax
or     (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
mov    0x2e07(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <feed_pipe+0xac>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
movl   $0x7,-0x4(%rbp)
jmp    12e0 <feed_pipe+0x190>
jmp    1201 <feed_pipe+0xb1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    123c <feed_pipe+0xec>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
movl   $0x8,-0x4(%rbp)
jmp    12e0 <feed_pipe+0x190>
cmpl   $0x0,-0x20(%rbp)
jne    124b <feed_pipe+0xfb>
jmp    1284 <feed_pipe+0x134>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
movslq -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    127f <feed_pipe+0x12f>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
movl   $0x9,-0x4(%rbp)
jmp    12e0 <feed_pipe+0x190>
jmp    1201 <feed_pipe+0xb1>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12ad <feed_pipe+0x15d>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    12e0 <feed_pipe+0x190>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12d9 <feed_pipe+0x189>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    12e0 <feed_pipe+0x190>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<feed_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
mov    0x4(%rdi),%edi
call   1040 <close@plt>
mov    $0x6,%r12d
cmp    $0xffffffff,%eax
je     123c <feed_pipe+0xec>
mov    0x2e23(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
mov    0x2e18(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1245 <feed_pipe+0xf5>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe67(%rip),%r14
lea    0xe78(%rip),%r13
nopl   (%rax)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1216 <feed_pipe+0xc6>
cmp    $0xffffffff,%eax
je     1254 <feed_pipe+0x104>
movslq %eax,%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11e0 <feed_pipe+0x90>
mov    $0x9,%r12d
mov    %r13,%r14
jmp    1263 <feed_pipe+0x113>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     125c <feed_pipe+0x10c>
mov    (%r15),%edi
call   1040 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    126e <feed_pipe+0x11e>
lea    0xe5c(%rip),%r14
jmp    1263 <feed_pipe+0x113>
lea    0xdbd(%rip),%r14
jmp    1263 <feed_pipe+0x113>
mov    $0x7,%r12d
lea    0xdc8(%rip),%r14
jmp    1263 <feed_pipe+0x113>
mov    $0x8,%r12d
jmp    1263 <feed_pipe+0x113>
lea    0xe08(%rip),%r14
mov    %r14,%rdi
call   1070 <perror@plt>
mov    %r12d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Error closing end of pipe\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.1 = [35 x i8] c"Error opening file for redirection\00"
@.str.2 = [24 x i8] c"Error reading from pipe\00"
@.str.3 = [22 x i8] c"Error writing to pipe\00"
@.str.4 = [43 x i8] c"Error closing file descriptor for I/O file\00"
@.str.5 = [31 x i8] c"Error closing redirection pipe\00"
define i32 @feed_pipe(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32*, i32** %4
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = load i32, i32* %10
%12 = call i32 @close(i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 6, i32* %3
br label %73
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IWGRP
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %17, i32 %22, i32 %29)
store i32 %30, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %16
%33 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 7, i32* %3
br label %73
34: ; preds = %16
br label %35
35: ; preds = %57, %34
%36 = load i32*, i32** %4
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 512)
store i32 %40, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 8, i32* %3
br label %73
44: ; preds = %35
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
br label %58
48: ; preds = %44
%49 = load i32, i32* %6
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = call i32 @write(i32 %49, i8* %50, i64 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 9, i32* %3
br label %73
57: ; preds = %48
br label %35
58: ; preds = %47
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
store i32 6, i32* %3
br label %73
64: ; preds = %58
%65 = load i32*, i32** %4
%66 = getelementptr inbounds i32, i32* %65, i64 0
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %72
70: ; preds = %64
%71 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 6, i32* %3
br label %73
72: ; preds = %64
store i32 0, i32* %3
br label %73
73: ; preds = %72, %70, %62, %55, %42, %32, %14
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [26 x i8] c"Error closing end of pipe\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.1 = [35 x i8] c"Error opening file for redirection\00"
@.str.2 = [24 x i8] c"Error reading from pipe\00"
@.str.3 = [22 x i8] c"Error writing to pipe\00"
@.str.4 = [43 x i8] c"Error closing file descriptor for I/O file\00"
@.str.5 = [31 x i8] c"Error closing redirection pipe\00"
define i32 @feed_pipe(i32* nocapture %0, i8* nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i32, i32* %0, i64 1
%6 = load i32, i32* %5
%7 = tail call i32 @close(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %38, label %9
9: ; preds = %2
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IWGRP
%21 = or i32 %19, %20
%22 = tail call i32 @open(i8* %1, i32 %14, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %38, label %24
24: ; preds = %9, %27
%25 = load i32, i32* %0
%26 = call i32 @read(i32 %25, i8* %4, i32 512)
switch i32 %26, label %27 [
i32 -1, label %38
i32 0, label %31
]
27: ; preds = %24
%28 = sext i32 %26 to i64
%29 = call i32 @write(i32 %22, i8* %4, i64 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %38, label %24
31: ; preds = %24
%32 = tail call i32 @close(i32 %22)
%33 = icmp eq i32 %32, -1
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i32, i32* %0
%36 = tail call i32 @close(i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %27, %24, %34, %31, %9, %2
%39 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), %31 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), %34 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), %24 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), %27 ]
%40 = phi i32 [ 6, %2 ], [ 7, %9 ], [ 6, %31 ], [ 6, %34 ], [ 8, %24 ], [ 9, %27 ]
%41 = tail call i32 @perror(i8* %39)
br label %42
42: ; preds = %38, %34
%43 = phi i32 [ 0, %34 ], [ %40, %38 ]
ret i32 %43
}