#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ delete_event(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ modify_event(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
void do_read(int epollfd, int fd, char *buf) {
  int nread;
  nread = read(fd, buf, 1024);
  if (nread == -1) {
    perror("read error:");
    close(fd);
    delete_event(epollfd, fd, EPOLLIN);
  } else if (nread == 0) {
    fprintf(stderr, "client close.\n");
    close(fd);
    delete_event(epollfd, fd, EPOLLIN);
  } else {
    printf("read message is : %s", buf);
    modify_event(epollfd, fd, EPOLLOUT);
  }
}


x86 assembly (clang, -O0):
<do_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11cd <do_read+0x5d>
lea    0xe59(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   1030 <delete_event@plt>
jmp    1236 <do_read+0xc6>
cmpl   $0x0,-0x14(%rbp)
jne    120d <do_read+0x9d>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
call   1030 <delete_event@plt>
jmp    1231 <do_read+0xc1>
mov    -0x10(%rbp),%rsi
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
call   1080 <modify_event@plt>
jmp    1236 <do_read+0xc6>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %esi,%edi
mov    %rdx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11ba <do_read+0x4a>
cmp    $0xffffffff,%eax
je     11ed <do_read+0x7d>
lea    0xe80(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %ebx,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <modify_event@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %ebx,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <delete_event@plt>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
jmp    11cf <do_read+0x5f>

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

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