#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(void) {
  char s[300];
  int num, fd;
  mknod("american_maid", S_IFIFO | 0666, 0);
  printf("waiting for writers...\n");
  fd = open("american_maid", O_RDONLY);
  printf("got a writer\n");
  do {
    if ((num = read(fd, s, 300)) == -1)
      perror("read");
    else {
      s[num] = '\0';
      printf("tick: read %d bytes: \"%s\"\n", num, s);
    }
  } while (num > 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
lea    0xe88(%rip),%rdi
xor    %edx,%edx
call   1050 <mknod@plt>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x138(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x138(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x12c,%edx
call   1040 <read@plt>
mov    %eax,-0x134(%rbp)
cmp    $0xffffffff,%eax
jne    11ed <main+0x9d>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
jmp    1217 <main+0xc7>
movslq -0x134(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
mov    -0x134(%rbp),%esi
lea    -0x130(%rbp),%rdx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121c <main+0xcc>
cmpl   $0x0,-0x134(%rbp)
jg     11b6 <main+0x66>
xor    %eax,%eax
add    $0x140,%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    0xe9b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%r15d
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
lea    0xe51(%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    0xdf7(%rip),%rdi
call   1080 <perror@plt>
jmp    1200 <main+0xa0>

LLVM IR (-O0):
@.str = [14 x i8] c"american_maid\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 = [27 x i8] c"tick: read %d bytes: \22%s\22\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [300 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @S_IFIFO
%6 = or i32 %5, 438
%7 = call i32 @mknod(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %4
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %27, %0
%13 = load i32, i32* %4
%14 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 300)
store i32 %15, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %26
19: ; preds = %12
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
%23 = load i32, i32* %3
%24 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %23, i8* %24)
br label %26
26: ; preds = %19, %17
br label %27
27: ; preds = %26
%28 = load i32, i32* %3
%29 = icmp sgt i32 %28, 0
br i1 %29, label %12, label %30
30: ; preds = %27
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"american_maid\00"
@S_IFIFO = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [27 x i8] c"tick: read %d bytes: \22%s\22\0A\00"
@str = [23 x i8] c"waiting for writers...\00"
@str.5 = [13 x i8] c"got a writer\00"
define i32 @main() {
%1 = alloca [300 x i8]
%2 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @S_IFIFO
%4 = or i32 %3, 438
%5 = tail call i32 @mknod(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
br label %10
10: ; preds = %15, %0
%11 = call i32 @read(i32 %8, i8* %2, i32 300)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %20
15: ; preds = %10
%16 = sext i32 %11 to i64
%17 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %11, i8* %2)
%19 = icmp sgt i32 %11, 0
br i1 %19, label %10, label %20
20: ; preds = %15, %13
ret i32 0
}