#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_RDONLY;
int S_IFIFO;
int /*<<< orphan*/ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
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 writers...\n");
  fd = open("speak_and_tick", O_RDONLY);
  printf("Got a writer\n");
  do {
    if ((num = read(fd, buffer, 300)) == -1)
      perror("read");
    else {
      buffer[num] = '\0';
      printf("tick: read %d bytes: %s\n", num, buffer);
    }
  } while (num > 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x144(%rbp)
movl   $0x0,-0x148(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
lea    0xe6d(%rip),%rdi
xor    %edx,%edx
call   1050 <mknod@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x148(%rbp)
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x148(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x12c,%edx
call   1040 <read@plt>
mov    %eax,-0x144(%rbp)
cmp    $0xffffffff,%eax
jne    1208 <main+0xb8>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
jmp    1232 <main+0xe2>
movslq -0x144(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
mov    -0x144(%rbp),%esi
lea    -0x140(%rbp),%rdx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1237 <main+0xe7>
cmpl   $0x0,-0x144(%rbp)
jg     11d1 <main+0x81>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x138,%rsp
mov    0x2e64(%rip),%rax
mov    $0x1b6,%esi
or     (%rax),%esi
lea    0xe7e(%rip),%rbx
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <mknod@plt>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%r15d
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
lea    0xe52(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x12c,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0xb0>
mov    %eax,%ebp
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jg     11d0 <main+0x70>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
jmp    1200 <main+0xa0>

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 writers...\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"Got a writer\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [25 x i8] c"tick: read %d bytes: %s\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_RDONLY
%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 ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %31, %2
%17 = load i32, i32* %8
%18 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 300)
store i32 %19, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %30
23: ; preds = %16
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %7
%28 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %27, i8* %28)
br label %30
30: ; preds = %23, %21
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %16, label %34
34: ; preds = %31
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"speak_and_tick\00"
@S_IFIFO = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [25 x i8] c"tick: read %d bytes: %s\0A\00"
@str = [23 x i8] c"Waiting for writers...\00"
@str.5 = [13 x i8] c"Got a writer\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_RDONLY
%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 ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
br label %12
12: ; preds = %17, %2
%13 = call i32 @read(i32 %10, i8* %4, i32 300)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %22
17: ; preds = %12
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds [300 x i8], [300 x i8]* %3, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %13, i8* %4)
%21 = icmp sgt i32 %13, 0
br i1 %21, label %12, label %22
22: ; preds = %17, %15
ret i32 0
}