#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int BUFSIZ;
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int O_NONBLOCK;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int readfd, writefd;
  struct stat status;
  char *fifonam;
  char buffer[BUFSIZ];
  ssize_t bytes;
  signal(SIGPIPE, SIG_IGN);
  if (2 != argc) {
    printf("Usage:\n someprog 2>&1 | %s FIFO\n FIFO - path to a"
           " named pipe, required argument\n",
           argv[0]);
    exit(EXIT_FAILURE);
  }
  fifonam = argv[1];
  readfd = open(fifonam, O_RDONLY | O_NONBLOCK);
  if (-1 == readfd) {
    perror("pipe_echo: readfd: open()");
    exit(EXIT_FAILURE);
  }
  if (-1 == fstat(readfd, &status)) {
    perror("pipe_echo: fstat");
    close(readfd);
    exit(EXIT_FAILURE);
  }
  if (!S_ISFIFO(status.st_mode)) {
    printf("%s: %s in not a fifo!\n", argv[0], fifonam);
    close(readfd);
    exit(EXIT_FAILURE);
  }
  writefd = open(fifonam, O_WRONLY | O_NONBLOCK);
  if (-1 == writefd) {
    perror("pipe_echo: writefd: open()");
    close(readfd);
    exit(EXIT_FAILURE);
  }
  close(readfd);
  while (1) {
    bytes = read(STDIN_FILENO, buffer, sizeof(buffer));
    if (bytes < 0 && errno == EINTR)
      continue;
    if (bytes <= 0)
      break;
    bytes = write(writefd, buffer, bytes);
    if (-1 == bytes)
      ;
  }
  close(writefd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     122e <main+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2d9a(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1280 <main+0xe0>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12bd <main+0x11d>
lea    0xdc9(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1040 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    12fd <main+0x15d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    0x2c88(%rip),%rax
mov    (%rax),%esi
mov    0x2cd7(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    134b <main+0x1ab>
lea    0xd63(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    1358 <main+0x1b8>
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rax
mov    0x2c39(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    139c <main+0x1fc>
mov    0x2c34(%rip),%rax
mov    (%rax),%rax
mov    0x2c1a(%rip),%rcx
cmp    (%rcx),%rax
jne    139c <main+0x1fc>
jmp    1353 <main+0x1b3>
cmpl   $0x0,-0x3c(%rbp)
jg     13ab <main+0x20b>
jmp    13d5 <main+0x235>
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x3c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x3c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x3c(%rbp),%eax
jne    13d0 <main+0x230>
jmp    13d0 <main+0x230>
jmp    1353 <main+0x1b3>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %edi,%r15d
mov    %rsp,-0x40(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
cmp    $0x2,%r15d
jne    12e0 <main+0x140>
mov    %r12,-0x38(%rbp)
mov    0x8(%r12),%r13
mov    0x2dd9(%rip),%rax
mov    0x2dda(%rip),%r12
mov    (%r12),%esi
or     (%rax),%esi
mov    %r13,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f4 <main+0x154>
mov    %eax,%r15d
lea    -0x30(%rbp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1302 <main+0x162>
mov    -0x30(%rbp),%edi
call   1040 <S_ISFIFO@plt>
test   %eax,%eax
je     130b <main+0x16b>
mov    0x2d3d(%rip),%rax
mov    (%r12),%esi
or     (%rax),%esi
mov    %r13,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1325 <main+0x185>
mov    %eax,%r12d
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2d24(%rip),%r15
mov    0x2d25(%rip),%r13
jmp    12a0 <main+0x100>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2d21(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    12c3 <main+0x123>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1290 <main+0xf0>
je     12c3 <main+0x123>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    12a0 <main+0x100>
mov    %r12d,%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%rsi
lea    0xd15(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1339 <main+0x199>
lea    0xd56(%rip),%rdi
call   10a0 <perror@plt>
jmp    1339 <main+0x199>
lea    0xd62(%rip),%rdi
jmp    132c <main+0x18c>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xd63(%rip),%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1331 <main+0x191>
lea    0xd67(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@BUFSIZ = global i32 0
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [81 x i8] c"Usage:\0A someprog 2>&1 | %s FIFO\0A FIFO - path to a named pipe, required argument\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c"pipe_echo: readfd: open()\00"
@.str.2 = [17 x i8] c"pipe_echo: fstat\00"
@.str.3 = [23 x i8] c"%s: %s in not a fifo!\0A\00"
@O_WRONLY = global i32 0
@.str.4 = [27 x i8] c"pipe_echo: writefd: open()\00"
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32, i32* @SIGPIPE
%18 = load i32, i32* @SIG_IGN
%19 = call i32 @signal(i32 %17, i32 %18)
%20 = load i32, i32* %4
%21 = icmp ne i32 2, %20
br i1 %21, label %22, label %29
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = load i32, i32* @O_RDONLY
%35 = load i32, i32* @O_NONBLOCK
%36 = or i32 %34, %35
%37 = call i32 @open(i8* %33, i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp eq i32 -1, %38
br i1 %39, label %40, label %44
40: ; preds = %29
%41 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %29
%45 = load i32, i32* %6
%46 = call i32 @fstat(i32 %45, %struct.stat* %8)
%47 = icmp eq i32 -1, %46
br i1 %47, label %48, label %54
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %44
%55 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @S_ISFIFO(i32 %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %69, label %59
59: ; preds = %54
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load i8*, i8** %61
%63 = load i8*, i8** %9
%64 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %62, i8* %63)
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %54
%70 = load i8*, i8** %9
%71 = load i32, i32* @O_WRONLY
%72 = load i32, i32* @O_NONBLOCK
%73 = or i32 %71, %72
%74 = call i32 @open(i8* %70, i32 %73)
store i32 %74, i32* %7
%75 = load i32, i32* %7
%76 = icmp eq i32 -1, %75
br i1 %76, label %77, label %83
77: ; preds = %69
%78 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* @EXIT_FAILURE
%82 = call i32 @exit(i32 %81)
unreachable
83: ; preds = %69
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
br label %86
86: ; preds = %109, %97, %83
br label %87
87: ; preds = %86
%88 = load i32, i32* @STDIN_FILENO
%89 = trunc i64 %14 to i32
%90 = call i32 @read(i32 %88, i8* %16, i32 %89)
store i32 %90, i32* %12
%91 = load i32, i32* %12
%92 = icmp slt i32 %91, 0
br i1 %92, label %93, label %98
93: ; preds = %87
%94 = load i64, i64* @errno
%95 = load i64, i64* @EINTR
%96 = icmp eq i64 %94, %95
br i1 %96, label %97, label %98
97: ; preds = %93
br label %86
98: ; preds = %93, %87
%99 = load i32, i32* %12
%100 = icmp sle i32 %99, 0
br i1 %100, label %101, label %102
101: ; preds = %98
br label %110
102: ; preds = %98
%103 = load i32, i32* %7
%104 = load i32, i32* %12
%105 = call i32 @write(i32 %103, i8* %16, i32 %104)
store i32 %105, i32* %12
%106 = load i32, i32* %12
%107 = icmp eq i32 -1, %106
br i1 %107, label %108, label %109
108: ; preds = %102
br label %109
109: ; preds = %108, %102
br label %86
110: ; preds = %101
%111 = load i32, i32* %7
%112 = call i32 @close(i32 %111)
store i32 0, i32* %3
%113 = load i8*, i8** %10
%114 = load i32, i32* %3
ret i32 %114
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@BUFSIZ = global i32 0
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [81 x i8] c"Usage:\0A someprog 2>&1 | %s FIFO\0A FIFO - path to a named pipe, required argument\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c"pipe_echo: readfd: open()\00"
@.str.2 = [17 x i8] c"pipe_echo: fstat\00"
@.str.3 = [23 x i8] c"%s: %s in not a fifo!\0A\00"
@O_WRONLY = global i32 0
@.str.4 = [27 x i8] c"pipe_echo: writefd: open()\00"
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = load i32, i32* @SIGPIPE
%10 = load i32, i32* @SIG_IGN
%11 = tail call i32 @signal(i32 %9, i32 %10)
%12 = icmp eq i32 %0, 2
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i8*, i8** %1
%15 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %22, %21
%24 = tail call i32 @open(i8* %20, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %18
%31 = call i32 @fstat(i32 %24, %struct.stat* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i32 @close(i32 %24)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%40 = load i32, i32* %39
%41 = tail call i32 @S_ISFIFO(i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %38
%44 = load i8*, i8** %1
%45 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %20)
%46 = tail call i32 @close(i32 %24)
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %38
%50 = load i32, i32* @O_WRONLY
%51 = load i32, i32* @O_NONBLOCK
%52 = or i32 %51, %50
%53 = tail call i32 @open(i8* %20, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%57 = tail call i32 @close(i32 %24)
%58 = load i32, i32* @EXIT_FAILURE
%59 = tail call i32 @exit(i32 %58)
unreachable
60: ; preds = %49
%61 = tail call i32 @close(i32 %24)
br label %62
62: ; preds = %74, %60
%63 = load i32, i32* @STDIN_FILENO
%64 = call i32 @read(i32 %63, i8* %8, i32 %5)
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i64, i64* @errno
%68 = load i64, i64* @EINTR
%69 = icmp eq i64 %67, %68
br i1 %69, label %74, label %75
70: ; preds = %62
%71 = icmp eq i32 %64, 0
br i1 %71, label %75, label %72
72: ; preds = %70
%73 = call i32 @write(i32 %53, i8* %8, i32 %64)
br label %74
74: ; preds = %72, %66
br label %62
75: ; preds = %66, %70
%76 = tail call i32 @close(i32 %53)
tail call void @llvm.stackrestore(i8* %7)
ret i32 0
}