#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 pollfd {
  scalar_t__ revents;
  int fd;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ EIO;
scalar_t__ POLLIN;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ errno;
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, scalar_t__);
int handle_event(struct pollfd *pfd, int sockfd) {
  static char buff[1024];
  if (pfd->revents == 0)
    return 0;
  if (pfd->revents != POLLIN)
    return -1;
  int fd_from = pfd->fd;
  int fd_to = (fd_from == STDIN_FILENO ? sockfd : STDOUT_FILENO);
  ssize_t n;
  if ((n = read(fd_from, buff, sizeof(buff))) < 0)
    return -1;
  ssize_t written;
  if ((written = write(fd_to, buff, n)) != n) {
    if (written >= 0)
      errno = EIO;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1149 <handle_event+0x29>
movl   $0x0,-0x4(%rbp)
jmp    1224 <handle_event+0x104>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e89(%rip),%rcx
cmp    (%rcx),%rax
je     116c <handle_event+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <handle_event+0x104>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%eax
jne    1193 <handle_event+0x73>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    119f <handle_event+0x7f>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    0x2e91(%rip),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11d3 <handle_event+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <handle_event+0x104>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rdx
lea    0x2e5f(%rip),%rsi
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    -0x28(%rbp),%rax
je     121d <handle_event+0xfd>
cmpq   $0x0,-0x30(%rbp)
jl     1211 <handle_event+0xf1>
mov    0x2dca(%rip),%rax
mov    (%rax),%ecx
mov    0x2db9(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <handle_event+0x104>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_event>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%rax
xor    %ebx,%ebx
test   %rax,%rax
je     11b2 <handle_event+0x92>
mov    0x2ea4(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <handle_event+0x8d>
mov    %esi,%r12d
mov    0x8(%rdi),%r15d
mov    0x2e71(%rip),%rax
mov    (%rax),%r13d
mov    0x2e97(%rip),%rax
mov    (%rax),%r14d
lea    0x2edd(%rip),%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %rax,%rax
js     11ad <handle_event+0x8d>
mov    %rax,%rbp
cmp    %r13d,%r15d
cmove  %r12d,%r14d
lea    0x2eba(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
je     11b2 <handle_event+0x92>
test   %rax,%rax
js     11ad <handle_event+0x8d>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2e1d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i64, i32 }
@handle_event.buff = internal global [1024 x i8] zeroinitializer
@POLLIN = global i64 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@EIO = global i32 0
@errno = global i32 0
define i32 @handle_event(%struct.pollfd* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.pollfd*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store %struct.pollfd* %0, %struct.pollfd** %4
store i32 %1, i32* %5
%10 = load %struct.pollfd*, %struct.pollfd** %4
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %52
15: ; preds = %2
%16 = load %struct.pollfd*, %struct.pollfd** %4
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @POLLIN
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %3
br label %52
22: ; preds = %15
%23 = load %struct.pollfd*, %struct.pollfd** %4
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %23, i32 0, i32 1
%25 = load i32, i32* %24
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = load i32, i32* @STDIN_FILENO
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = load i32, i32* %5
br label %33
31: ; preds = %22
%32 = load i32, i32* @STDOUT_FILENO
br label %33
33: ; preds = %31, %29
%34 = phi i32 [ %30, %29 ], [ %32, %31 ]
store i32 %34, i32* %7
%35 = load i32, i32* %6
%36 = call i64 @read(i32 %35, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_event.buff, i64 0, i64 0), i32 1024)
store i64 %36, i64* %8
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 -1, i32* %3
br label %52
39: ; preds = %33
%40 = load i32, i32* %7
%41 = load i64, i64* %8
%42 = call i64 @write(i32 %40, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_event.buff, i64 0, i64 0), i64 %41)
store i64 %42, i64* %9
%43 = load i64, i64* %8
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i64, i64* %9
%47 = icmp sge i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i32, i32* @EIO
store i32 %49, i32* @errno
br label %50
50: ; preds = %48, %45
store i32 -1, i32* %3
br label %52
51: ; preds = %39
store i32 0, i32* %3
br label %52
52: ; preds = %51, %50, %38, %21, %14
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.pollfd = type { i64, i32 }
@handle_event.buff = internal global [1024 x i8] zeroinitializer
@POLLIN = global i64 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@EIO = global i32 0
@errno = global i32 0
define i32 @handle_event(%struct.pollfd* nocapture %0, i32 %1) {
%3 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = icmp eq i64 %4, 0
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = load i64, i64* @POLLIN
%8 = icmp eq i64 %4, %7
br i1 %8, label %9, label %25
9: ; preds = %6
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 1
%11 = load i32, i32* %10
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i64 @read(i32 %11, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_event.buff, i64 0, i64 0), i32 1024)
%15 = icmp slt i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %9
%17 = icmp eq i32 %11, %12
%18 = select i1 %17, i32 %1, i32 %13
%19 = tail call i64 @write(i32 %18, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_event.buff, i64 0, i64 0), i64 %14)
%20 = icmp eq i64 %19, %14
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = icmp sgt i64 %19, -1
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = load i32, i32* @EIO
store i32 %24, i32* @errno
br label %25
25: ; preds = %9, %16, %21, %23, %6, %2
%26 = phi i32 [ 0, %2 ], [ -1, %6 ], [ -1, %9 ], [ -1, %23 ], [ -1, %21 ], [ 0, %16 ]
ret i32 %26
}