#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*/ EPOLLIN;
int /*<<< orphan*/ EPOLLOUT;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ add_event(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ delete_event(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
void do_read(int epollfd, int fd, int sockfd, char *buf) {
  int nread;
  nread = read(fd, buf, 1024);
  if (nread == -1) {
    perror("read error:");
    close(fd);
  } else if (nread == 0) {
    fprintf(stderr, "server close.\n");
    close(fd);
  } else {
    if (fd == STDIN_FILENO)
      add_event(epollfd, sockfd, EPOLLOUT);
    else {
      delete_event(epollfd, sockfd, EPOLLIN);
      add_event(epollfd, STDOUT_FILENO, EPOLLOUT);
    }
  }
}


x86 assembly (clang, -O0):
<do_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ac <do_read+0x4c>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    123b <do_read+0xdb>
cmpl   $0x0,-0x1c(%rbp)
jne    11d8 <do_read+0x78>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    1236 <do_read+0xd6>
mov    -0x8(%rbp),%eax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%eax
jne    1203 <do_read+0xa3>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   1070 <add_event@plt>
jmp    1231 <do_read+0xd1>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
call   1030 <delete_event@plt>
mov    -0x4(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
call   1070 <add_event@plt>
jmp    1236 <do_read+0xd6>
jmp    123b <do_read+0xdb>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
mov    %esi,%edi
mov    %rcx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     119e <do_read+0x3e>
cmp    $0xffffffff,%eax
je     11ee <do_read+0x8e>
mov    0x2e36(%rip),%rax
cmp    %ebp,(%rax)
jne    11be <do_read+0x5e>
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14d,%esi
jmp    11e5 <do_read+0x85>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14d,%esi
call   1030 <delete_event@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <add_event@plt>
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
jmp    11b3 <do_read+0x53>

LLVM IR (-O0):
@.str = [12 x i8] c"read error:\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"server close.\0A\00"
@STDIN_FILENO = global i32 0
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
@STDOUT_FILENO = global i32 0
define void @do_read(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i32, i32* %6
%11 = load i8*, i8** %8
%12 = call i32 @read(i32 %10, i8* %11, i32 1024)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
br label %47
19: ; preds = %4
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
br label %46
27: ; preds = %19
%28 = load i32, i32* %6
%29 = load i32, i32* @STDIN_FILENO
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* %5
%33 = load i32, i32* %7
%34 = load i32, i32* @EPOLLOUT
%35 = call i32 @add_event(i32 %32, i32 %33, i32 %34)
br label %45
36: ; preds = %27
%37 = load i32, i32* %5
%38 = load i32, i32* %7
%39 = load i32, i32* @EPOLLIN
%40 = call i32 @delete_event(i32 %37, i32 %38, i32 %39)
%41 = load i32, i32* %5
%42 = load i32, i32* @STDOUT_FILENO
%43 = load i32, i32* @EPOLLOUT
%44 = call i32 @add_event(i32 %41, i32 %42, i32 %43)
br label %45
45: ; preds = %36, %31
br label %46
46: ; preds = %45, %22
br label %47
47: ; preds = %46, %15
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"read error:\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"server close.\0A\00"
@STDIN_FILENO = global i32 0
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
@STDOUT_FILENO = global i32 0
define void @do_read(i32 %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32 @read(i32 %1, i8* %3, i32 1024)
switch i32 %5, label %13 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %4
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @close(i32 %1)
br label %25
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @close(i32 %1)
br label %25
13: ; preds = %4
%14 = load i32, i32* @STDIN_FILENO
%15 = icmp eq i32 %14, %1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EPOLLOUT
%18 = tail call i32 @add_event(i32 %0, i32 %2, i32 %17)
br label %25
19: ; preds = %13
%20 = load i32, i32* @EPOLLIN
%21 = tail call i32 @delete_event(i32 %0, i32 %2, i32 %20)
%22 = load i32, i32* @STDOUT_FILENO
%23 = load i32, i32* @EPOLLOUT
%24 = tail call i32 @add_event(i32 %0, i32 %22, i32 %23)
br label %25
25: ; preds = %9, %19, %16, %6
ret void
}