#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*/ O_WRONLY;
int S_IFIFO;
int /*<<< orphan*/ feof(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char buffer[300];
  int num = 0, fd = 0;
  mknod("speak_and_tick", S_IFIFO | 0666, 0);
  printf("Waiting for readers...\n");
  fd = open("speak_and_tick", O_WRONLY);
  printf("Got a reader -- type some stuff\n");
  while (fgets(buffer, sizeof(buffer), stdin), !feof(stdin)) {
    if ((num = write(fd, buffer, strlen(buffer))) == -1) {
      perror("write");
    } else {
      printf("speak: wrote %d byte\n", num);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x144(%rbp)
movl   $0x0,-0x148(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
lea    0xe3d(%rip),%rdi
xor    %edx,%edx
call   1080 <mknod@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x148(%rbp)
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    $0x12c,%esi
call   1060 <fgets@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123e <main+0xbe>
jmp    12b1 <main+0x131>
mov    -0x148(%rbp),%eax
mov    %eax,-0x154(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x140(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x154(%rbp),%edi
mov    -0x150(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x144(%rbp)
cmp    $0xffffffff,%eax
jne    1298 <main+0x118>
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ac <main+0x12c>
mov    -0x144(%rbp),%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1201 <main+0x81>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    0x2e32(%rip),%rax
mov    $0x1b6,%esi
or     (%rax),%esi
lea    0xe4c(%rip),%rbx
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <mknod@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    %eax,%r12d
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df7(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rsp,%rdi
mov    $0x12c,%esi
call   1070 <fgets@plt>
mov    0x0(%rbp),%edi
call   1080 <feof@plt>
test   %eax,%eax
je     121f <main+0x8f>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rbx
lea    0xde6(%rip),%r14
lea    0xde5(%rip),%r15
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xe4>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x12c,%esi
call   1070 <fgets@plt>
mov    0x0(%rbp),%edi
call   1080 <feof@plt>
test   %eax,%eax
je     1230 <main+0xa0>
jmp    120d <main+0x7d>
mov    %r14,%rdi
call   10b0 <perror@plt>
jmp    1256 <main+0xc6>

LLVM IR (-O0):
@.str = [15 x i8] c"speak_and_tick\00"
@S_IFIFO = global i32 0
@.str.1 = [24 x i8] c"Waiting for readers...\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [33 x i8] c"Got a reader -- type some stuff\0A\00"
@stdin = global i32 0
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [22 x i8] c"speak: wrote %d byte\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [300 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i32, i32* @S_IFIFO
%10 = or i32 %9, 438
%11 = call i32 @mknod(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10, i32 0)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %36, %2
%17 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* @stdin
%19 = call i64 @fgets(i8* %17, i32 300, i32 %18)
%20 = load i32, i32* @stdin
%21 = call i32 @feof(i32 %20)
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br i1 %23, label %24, label %37
24: ; preds = %16
%25 = load i32, i32* %8
%26 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%27 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @write(i32 %25, i8* %26, i32 %28)
store i32 %29, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %36
33: ; preds = %24
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33, %31
br label %16
37: ; preds = %16
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"speak_and_tick\00"
@S_IFIFO = global i32 0
@O_WRONLY = global i32 0
@stdin = global i32 0
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [22 x i8] c"speak: wrote %d byte\0A\00"
@str = [23 x i8] c"Waiting for readers...\00"
@str.5 = [32 x i8] c"Got a reader -- type some stuff\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [300 x i8]
%4 = getelementptr inbounds [300 x i8], [300 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @S_IFIFO
%6 = or i32 %5, 438
%7 = tail call i32 @mknod(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
%12 = load i32, i32* @stdin
%13 = call i64 @fgets(i8* %4, i32 300, i32 %12)
%14 = load i32, i32* @stdin
%15 = call i32 @feof(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %2, %25
%18 = call i32 @strlen(i8* %4)
%19 = call i32 @write(i32 %10, i8* %4, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %25
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %19)
br label %25
25: ; preds = %23, %21
%26 = load i32, i32* @stdin
%27 = call i64 @fgets(i8* %4, i32 300, i32 %26)
%28 = load i32, i32* @stdin
%29 = call i32 @feof(i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %17, label %31
31: ; preds = %25, %2
ret i32 0
}