#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 */
struct event {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void fifo_read(int fd, short event, void *arg) {
  char buf[255];
  int len;
  struct event *ev = arg;
  int count = 0;
  fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n", fd,
          event, arg);
  if (++count == 20) {
    len = read(fd, buf, sizeof(buf) - 1);
    if (len == -1) {
      perror("read");
      return;
    } else if (len == 0) {
      fprintf(stderr, "Connection closed\n");
      return;
    }
    buf[len] = '\0';
    fprintf(stdout, "Read: %s\n", buf);
  }
  printf("over...\n");
}


x86 assembly (clang, -O0):
<fifo_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x120(%rbp)
movl   $0x0,-0x124(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
movswl -0x6(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
cmp    $0x14,%eax
jne    123b <fifo_read+0xfb>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xfe,%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    11e0 <fifo_read+0xa0>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
jmp    1247 <fifo_read+0x107>
cmpl   $0x0,-0x114(%rbp)
jne    1209 <fifo_read+0xc9>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1247 <fifo_read+0x107>
jmp    120e <fifo_read+0xce>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xe12(%rip),%rdi
call   1030 <printf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fifo_read>:
push   %rax
mov    %rdx,%r8
mov    %esi,%ecx
mov    %edi,%edx
mov    0x2eb1(%rip),%rax
mov    (%rax),%edi
lea    0xec8(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xeec(%rip),%rdi
pop    %rax
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.event = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"fifo_read called with fd: %d, event: %d, arg: %p\0A\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [19 x i8] c"Connection closed\0A\00"
@stdout = global i32 0
@.str.3 = [10 x i8] c"Read: %s\0A\00"
@.str.4 = [9 x i8] c"over...\0A\00"
define void @fifo_read(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i8*
%7 = alloca [255 x i8]
%8 = alloca i32
%9 = alloca %struct.event*
%10 = alloca i32
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%11 = load i8*, i8** %6
%12 = bitcast i8* %11 to %struct.event*
store %struct.event* %12, %struct.event** %9
store i32 0, i32* %10
%13 = load i32, i32* @stderr
%14 = load i32, i32* %4
%15 = load i16, i16* %5
%16 = sext i16 %15 to i32
%17 = load i8*, i8** %6
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %16, i8* %17)
%19 = load i32, i32* %10
%20 = add nsw i32 %19, 1
store i32 %20, i32* %10
%21 = icmp eq i32 %20, 20
br i1 %21, label %22, label %44
22: ; preds = %3
%23 = load i32, i32* %4
%24 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 254)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %46
30: ; preds = %22
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %46
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* @stdout
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %37, %3
%45 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %33, %28
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"fifo_read called with fd: %d, event: %d, arg: %p\0A\00"
@stdout = global i32 0
@str = [8 x i8] c"over...\00"
define void @fifo_read(i32 %0, i16 signext %1, i8* %2) {
%4 = load i32, i32* @stderr
%5 = sext i16 %1 to i32
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %5, i8* %2)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
ret void
}