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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ rtrim(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  int fd;
  char buffer[63 + 1];
  char *fifo_filename;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s fifo_filename\n", argv[0]);
    exit(1);
  }
  fifo_filename = argv[1];
  if ((fd = open(fifo_filename, O_RDWR)) < 0) {
    perror("Could not open fifo");
    fprintf(stderr, "Cannot open %s for read/write.\n", fifo_filename);
    exit(1);
  }
  while (1) {
    memset(buffer, 0, 63 + 1);
    if (read(fd, buffer, 63 + 1) < 0) {
      perror("Cannot read from pipe.");
      exit(1);
    }
    rtrim(buffer);
    if (strcmp(buffer, "bye") == 0)
      break;
    printf("%s\n", buffer);
  }
  close(fd);
  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   $0x2,-0x8(%rbp)
je     11e6 <main+0x46>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    123f <main+0x9f>
lea    0xe02(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rdx
lea    0xdfd(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1244 <main+0xa4>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    1285 <main+0xe5>
lea    0xdd7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x60(%rbp),%rdi
call   1080 <rtrim@plt>
lea    -0x60(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12ad <main+0x10d>
jmp    12c2 <main+0x122>
lea    -0x60(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   1030 <printf@plt>
jmp    1244 <main+0xa4>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
cmp    $0x2,%edi
jne    1280 <main+0xe0>
mov    0x8(%rsi),%rbx
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1295 <main+0xf5>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1256 <main+0xb6>
mov    %rsp,%rbx
lea    0xe62(%rip),%r14
lea    0xe5f(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <rtrim@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     126c <main+0xcc>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %rax,%rax
jns    1210 <main+0x70>
lea    0xdf0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd6d(%rip),%rsi
jmp    12b4 <main+0x114>
lea    0xd7d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: %s fifo_filename\0A\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Could not open fifo\00"
@.str.2 = [32 x i8] c"Cannot open %s for read/write.\0A\00"
@.str.3 = [23 x i8] c"Cannot read from pipe.\00"
@.str.4 = [4 x i8] c"bye\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca 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, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 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 1
%21 = load i8*, i8** %20
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %8
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
br label %33
33: ; preds = %32, %50
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%35 = call i32 @memset(i8* %34, i32 0, i32 64)
%36 = load i32, i32* %6
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%38 = call i64 @read(i32 %36, i8* %37, i32 64)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%45 = call i32 @rtrim(i8* %44)
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
br label %53
50: ; preds = %43
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %51)
br label %33
53: ; preds = %49
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: %s fifo_filename\0A\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Could not open fifo\00"
@.str.2 = [32 x i8] c"Cannot open %s for read/write.\0A\00"
@.str.3 = [23 x i8] c"Cannot read from pipe.\00"
@.str.4 = [4 x i8] c"bye\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %11
%18 = call i32 @memset(i8* %4, i32 0, i32 64)
%19 = call i64 @read(i32 %15, i8* %4, i32 64)
%20 = icmp slt i64 %19, 0
br i1 %20, label %26, label %29
21: ; preds = %11
%22 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %33, %17
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17, %33
%30 = call i32 @rtrim(i8* %4)
%31 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%35 = call i32 @memset(i8* %4, i32 0, i32 64)
%36 = call i64 @read(i32 %15, i8* %4, i32 64)
%37 = icmp slt i64 %36, 0
br i1 %37, label %26, label %29
38: ; preds = %29
%39 = call i32 @close(i32 %15)
ret i32 0
}