#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 epoll_event {
  int events;
};

/* Variables and functions */
int EPOLLERR;
int EPOLLHUP;
int EPOLLIN;
int /*<<< orphan*/ epoll_unwatch_fd(int, int);
int /*<<< orphan*/ printf(char *, ...);
int su_tcp_recv(int, char *, int);
void handle_tcp(int epfd, int fd, struct epoll_event *evs) {
  char buf[1024];
  int ev = evs->events;
  if (ev & EPOLLERR) {
    printf("epoll tcp err\n");
  }
  if (ev & EPOLLHUP) {
    printf("epoll tcp hup\n");
  }
  if (ev & EPOLLIN) {
    printf("epoll tcp read\n");
  }
  int len = su_tcp_recv(fd, buf, 1024);
  buf[len] = 0;
  if (len == 0) {
    printf("tcp peer closed connection, remove it\n");
    epoll_unwatch_fd(epfd, fd);
  } else {
    printf("read from tcp, len: %d, body: %s\n", len, buf);
  }
}


x86 assembly (clang, -O0):
<handle_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
mov    0x2e7a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1177 <handle_tcp+0x47>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x414(%rbp),%eax
mov    0x2e4c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     119d <handle_tcp+0x6d>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x414(%rbp),%eax
mov    0x2e3e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c3 <handle_tcp+0x93>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <su_tcp_recv@plt>
mov    %eax,-0x418(%rbp)
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
cmpl   $0x0,-0x418(%rbp)
jne    1217 <handle_tcp+0xe7>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <epoll_unwatch_fd@plt>
jmp    1232 <handle_tcp+0x102>
mov    -0x418(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_tcp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
mov    (%rdx),%ebp
mov    0x2e7f(%rip),%rax
test   %ebp,(%rax)
je     1169 <handle_tcp+0x29>
lea    0xf01(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e60(%rip),%rax
test   %ebp,(%rax)
je     1180 <handle_tcp+0x40>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e61(%rip),%rax
test   %ebp,(%rax)
je     1197 <handle_tcp+0x57>
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1060 <su_tcp_recv@plt>
movslq %eax,%rcx
movb   $0x0,(%rsp,%rcx,1)
test   %eax,%eax
je     11c6 <handle_tcp+0x86>
lea    0xe48(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11dc <handle_tcp+0x9c>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1050 <epoll_unwatch_fd@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32 }
@EPOLLERR = global i32 0
@.str = [15 x i8] c"epoll tcp err\0A\00"
@EPOLLHUP = global i32 0
@.str.1 = [15 x i8] c"epoll tcp hup\0A\00"
@EPOLLIN = global i32 0
@.str.2 = [16 x i8] c"epoll tcp read\0A\00"
@.str.3 = [39 x i8] c"tcp peer closed connection, remove it\0A\00"
@.str.4 = [34 x i8] c"read from tcp, len: %d, body: %s\0A\00"
define void @handle_tcp(i32 %0, i32 %1, %struct.epoll_event* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.epoll_event*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.epoll_event* %2, %struct.epoll_event** %6
%10 = load %struct.epoll_event*, %struct.epoll_event** %6
%11 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %10, i32 0, i32 0
%12 = load i32, i32* %11
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* @EPOLLERR
%15 = and i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %3
%20 = load i32, i32* %8
%21 = load i32, i32* @EPOLLHUP
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = load i32, i32* %8
%28 = load i32, i32* @EPOLLIN
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %5
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%36 = call i32 @su_tcp_recv(i32 %34, i8* %35, i32 1024)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %33
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %4
%45 = load i32, i32* %5
%46 = call i32 @epoll_unwatch_fd(i32 %44, i32 %45)
br label %51
47: ; preds = %33
%48 = load i32, i32* %9
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %48, i8* %49)
br label %51
51: ; preds = %47, %42
ret void
}

LLVM IR (-O2):
%struct.epoll_event = type { i32 }
@EPOLLERR = global i32 0
@EPOLLHUP = global i32 0
@EPOLLIN = global i32 0
@.str.4 = [34 x i8] c"read from tcp, len: %d, body: %s\0A\00"
@str = [38 x i8] c"tcp peer closed connection, remove it\00"
@str.5 = [15 x i8] c"epoll tcp read\00"
@str.6 = [14 x i8] c"epoll tcp hup\00"
@str.7 = [14 x i8] c"epoll tcp err\00"
define void @handle_tcp(i32 %0, i32 %1, %struct.epoll_event* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @EPOLLERR
%9 = and i32 %8, %7
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = load i32, i32* @EPOLLHUP
%15 = and i32 %14, %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = load i32, i32* @EPOLLIN
%21 = and i32 %20, %7
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 @su_tcp_recv(i32 %1, i8* %5, i32 1024)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %27
store i8 0, i8* %28
%29 = icmp eq i32 %26, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%32 = call i32 @epoll_unwatch_fd(i32 %0, i32 %1)
br label %35
33: ; preds = %25
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* %5)
br label %35
35: ; preds = %33, %30
ret void
}