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

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ get_line(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, nwrite;
  char buffer[63 + 1] = {0};
  char *fifo_filename;
  if (argc != 2) {
    fprintf(stderr, "Usage: echo message | %s fifo_filename\n", argv[0]);
    exit(1);
  }
  fifo_filename = argv[1];
  if ((fd = open(fifo_filename, O_WRONLY)) < 0) {
    perror("Could not open pipe");
    fprintf(stderr, "Cannot open %s for write.\n", fifo_filename);
    exit(1);
  }
  while (get_line(buffer, stdin)) {
    if (strlen(buffer) > 63) {
      fprintf(stderr, "Too long: %s\n", argv[1]);
      exit(1);
    }
    *(buffer + strlen(buffer)) = 10;
    if ((nwrite = write(fd, buffer, 63 + 1)) <= 0) {
      if (nwrite == 0)
        errno = EAGAIN;
      perror("Could not write to pipe");
      exit(1);
    }
    memset(buffer, 0, sizeof(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)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
je     11e6 <main+0x56>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    123f <main+0xaf>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rdx
lea    0xe0c(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1244 <main+0xb4>
lea    -0x60(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1040 <get_line@plt>
cmp    $0x0,%rax
je     130d <main+0x17d>
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x3f,%eax
jle    1299 <main+0x109>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdcd(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
cltq
movb   $0xa,-0x60(%rbp,%rax,1)
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     12f8 <main+0x168>
cmpl   $0x0,-0x18(%rbp)
jne    12e2 <main+0x152>
mov    0x2ce1(%rip),%rax
mov    (%rax),%ecx
mov    0x2cf0(%rip),%rax
mov    %ecx,(%rax)
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
jmp    1244 <main+0xb4>
mov    -0x14(%rbp),%edi
call   1070 <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
mov    %rsi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
cmp    $0x2,%edi
jne    12ae <main+0x11e>
mov    0x8(%r14),%rbx
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12c3 <main+0x133>
mov    %eax,%ebp
mov    0x2e06(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdi
call   1040 <get_line@plt>
test   %rax,%rax
je     124d <main+0xbd>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x40,%eax
jge    1261 <main+0xd1>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0xa,(%rsp,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1284 <main+0xf4>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <get_line@plt>
test   %rax,%rax
jne    1200 <main+0x70>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xde2(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jne    1298 <main+0x108>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    0x2d3a(%rip),%rcx
mov    %eax,(%rcx)
lea    0xdc6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd3f(%rip),%rsi
jmp    1275 <main+0xe5>
lea    0xd5e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
mov    %rbx,%rdx
jmp    1275 <main+0xe5>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: echo message | %s fifo_filename\0A\00"
@O_WRONLY = global i32 0
@.str.1 = [20 x i8] c"Could not open pipe\00"
@.str.2 = [27 x i8] c"Cannot open %s for write.\0A\00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Too long: %s\0A\00"
@EAGAIN = global i32 0
@errno = global i32 0
@.str.4 = [24 x i8] c"Could not write to pipe\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 [64 x i8]
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [64 x i8]* %8 to i8*
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %9
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
br label %35
35: ; preds = %69, %34
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%37 = load i32, i32* @stdin
%38 = call i64 @get_line(i8* %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %72
40: ; preds = %35
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = icmp sgt i32 %42, 63
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %40
%52 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 10, i8* %56
%57 = load i32, i32* %6
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%59 = call i32 @write(i32 %57, i8* %58, i32 64)
store i32 %59, i32* %7
%60 = icmp sle i32 %59, 0
br i1 %60, label %61, label %69
61: ; preds = %51
%62 = load i32, i32* %7
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = load i32, i32* @EAGAIN
store i32 %65, i32* @errno
br label %66
66: ; preds = %64, %61
%67 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %51
%70 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%71 = call i32 @memset(i8* %70, i32 0, i32 64)
br label %35
72: ; preds = %35
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: echo message | %s fifo_filename\0A\00"
@O_WRONLY = global i32 0
@.str.1 = [20 x i8] c"Could not open pipe\00"
@.str.2 = [27 x i8] c"Cannot open %s for write.\0A\00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Too long: %s\0A\00"
@EAGAIN = global i32 0
@errno = global i32 0
@.str.4 = [24 x i8] c"Could not write to pipe\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 ([40 x i8], [40 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_WRONLY
%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 = load i32, i32* @stdin
%19 = call i64 @get_line(i8* %4, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %52, label %26
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 ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %17, %47
%27 = call i32 @strlen(i8* %4)
%28 = icmp sgt i32 %27, 63
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %12
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = call i32 @strlen(i8* %4)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %36
store i8 10, i8* %37
%38 = call i32 @write(i32 %15, i8* %4, i32 64)
%39 = icmp slt i32 %38, 1
br i1 %39, label %40, label %47
40: ; preds = %34
%41 = icmp eq i32 %38, 0
br i1 %41, label %42, label %44
42: ; preds = %40
%43 = load i32, i32* @EAGAIN
store i32 %43, i32* @errno
br label %44
44: ; preds = %42, %40
%45 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %34
%48 = call i32 @memset(i8* %4, i32 0, i32 64)
%49 = load i32, i32* @stdin
%50 = call i64 @get_line(i8* %4, i32 %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %26
52: ; preds = %47, %17
%53 = call i32 @close(i32 %15)
ret i32 0
}