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

/* Variables and functions */
scalar_t__ EINTR;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int fd[2];
  if (argc < 3) {
    fprintf(stdout, "usage: %s <filename><fifoname>\n:", argv[0]);
    exit(-1);
  }
  if ((fd[0] = open(argv[2], O_RDONLY)) < 0) {
    perror("fail to open");
    exit(-1);
  }
  if ((fd[1] = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
    perror("fail to open");
    exit(-1);
  }
  fprintf(stdout, "open fifo %s for reading successed.\n", argv[0]);
  char buffer[1024];
  ssize_t n;
  while (1) {
  again:
    if ((n = read(fd[0], buffer, 1024)) < 0) {
      if (errno == EINTR) {
        goto again;
      } else {
        perror("fail to read ");
        exit(-1);
      }
    } else if (n == 0) {
      fprintf(stderr, "peer closed fifo.\n");
      break;
    } else {
      write(fd[1], buffer, n);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ab <main+0x4b>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11e5 <main+0x85>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1236 <main+0xd6>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1259 <main+0xf9>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jge    12b3 <main+0x153>
mov    0x2d3b(%rip),%rax
mov    (%rax),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
jne    129d <main+0x13d>
jmp    1259 <main+0xf9>
lea    0xdaf(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x428(%rbp)
jne    12dd <main+0x17d>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fd <main+0x19d>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1030 <write@plt>
jmp    12f8 <main+0x198>
jmp    1254 <main+0xf4>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jle    1277 <main+0x117>
mov    0x10(%r14),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     126e <main+0x10e>
mov    %eax,%ebx
mov    0x2e00(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     126e <main+0x10e>
mov    %eax,%r15d
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe4c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rsp,%rbp
mov    0x2dcd(%rip),%r14
mov    0x2dbe(%rip),%r12
jmp    1209 <main+0xa9>
nopl   0x0(%rax)
mov    (%r14),%rax
cmp    (%r12),%rax
jne    1258 <main+0xf8>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %rax,%rax
js     1200 <main+0xa0>
je     122f <main+0xcf>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
jmp    1209 <main+0xa9>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xdac(%rip),%rdi
jmp    125f <main+0xff>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd76(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [33 x i8] c"usage: %s <filename><fifoname>\0A:\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [37 x i8] c"open fifo %s for reading successed.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [14 x i8] c"fail to read \00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"peer closed fifo.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [1024 x i8]
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stdout
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 (i8*, i32, ...) @open(i8* %21, i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
store i32 %23, i32* %24
%25 = icmp slt i32 %23, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %18
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %37, i32 420)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
store i32 %38, i32* %39
%40 = icmp slt i32 %38, 0
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %29
%45 = load i32, i32* @stdout
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %44, %78
br label %51
51: ; preds = %61, %50
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%53 = load i32, i32* %52
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%55 = call i64 @read(i32 %53, i8* %54, i32 1024)
store i64 %55, i64* %8
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %65
57: ; preds = %51
%58 = load i64, i64* @errno
%59 = load i64, i64* @EINTR
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %57
br label %51
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 -1)
unreachable
65: ; preds = %51
%66 = load i64, i64* %8
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %79
71: ; preds = %65
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%73 = load i32, i32* %72
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%75 = load i64, i64* %8
%76 = call i32 @write(i32 %73, i8* %74, i64 %75)
br label %77
77: ; preds = %71
br label %78
78: ; preds = %77
br label %50
79: ; preds = %68
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [33 x i8] c"usage: %s <filename><fifoname>\0A:\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [37 x i8] c"open fifo %s for reading successed.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [14 x i8] c"fail to read \00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"peer closed fifo.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stdout
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %7)
%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 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = tail call i32 (i8*, i32, ...) @open(i8* %21, i32 %26, i32 420)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %19
%33 = load i32, i32* @stdout
%34 = load i8*, i8** %1
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %37
37: ; preds = %44, %32
%38 = call i64 @read(i32 %14, i8* %36, i32 1024)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %53
br label %37
45: ; preds = %40
%46 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%47 = tail call i32 @exit(i32 -1)
unreachable
48: ; preds = %37
%49 = icmp eq i64 %38, 0
br i1 %49, label %50, label %53
50: ; preds = %48
%51 = load i32, i32* @stderr
%52 = tail call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
53: ; preds = %48
%54 = call i32 @write(i32 %27, i8* %36, i64 %38)
br label %44
}