#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*/ event_add(struct event *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(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;
  event_add(ev, NULL);
  fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n", fd,
          event, arg);
  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);
}


x86 assembly (clang, -O0):
<fifo_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <event_add@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
movswl -0x6(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xfe,%edx
call   1030 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    11ce <fifo_read+0x8e>
lea    0xe6e(%rip),%rdi
call   1060 <perror@plt>
jmp    1229 <fifo_read+0xe9>
cmpl   $0x0,-0x114(%rbp)
jne    11f7 <fifo_read+0xb7>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1229 <fifo_read+0xe9>
jmp    11fc <fifo_read+0xbc>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fifo_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rdx,%rdi
xor    %esi,%esi
call   1040 <event_add@plt>
mov    0x2e7a(%rip),%r15
mov    (%r15),%edi
lea    0xe90(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xfe,%edx
call   1030 <read@plt>
test   %eax,%eax
je     11c5 <fifo_read+0x85>
cmp    $0xffffffff,%eax
je     11e3 <fifo_read+0xa3>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%edi
lea    0xe68(%rip),%rsi
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>
lea    0xe48(%rip),%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <perror@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"
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*
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = bitcast i8* %10 to %struct.event*
store %struct.event* %11, %struct.event** %9
%12 = load %struct.event*, %struct.event** %9
%13 = call i32 @event_add(%struct.event* %12, i32* null)
%14 = load i32, i32* @stderr
%15 = load i32, i32* %4
%16 = load i16, i16* %5
%17 = sext i16 %16 to i32
%18 = load i8*, i8** %6
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17, i8* %18)
%20 = load i32, i32* %4
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 254)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %41
27: ; preds = %3
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %41
33: ; preds = %27
br label %34
34: ; preds = %33
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* @stdout
%39 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %34, %30, %25
ret void
}

LLVM IR (-O2):
%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"
define void @fifo_read(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%6 = bitcast i8* %2 to %struct.event*
%7 = tail call i32 @event_add(%struct.event* %6, i32* null)
%8 = load i32, i32* @stderr
%9 = sext i16 %1 to i32
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %9, i8* %2)
%11 = call i32 @read(i32 %0, i8* %5, i32 254)
switch i32 %11, label %17 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %22
17: ; preds = %3
%18 = sext i32 %11 to i64
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* @stdout
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %22
22: ; preds = %17, %14, %12
ret void
}