#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 evbuffer {
  int dummy;
};
struct bufferevent {
  int dummy;
};

/* Variables and functions */
short BEV_EVENT_EOF;
short BEV_EVENT_ERROR;
int /*<<< orphan*/ bufferevent_free(struct bufferevent *);
struct evbuffer *bufferevent_get_output(struct bufferevent *);
size_t evbuffer_get_length(struct evbuffer *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t);
void echo_event_cb(struct bufferevent *bev, short events, void *ctx) {
  struct evbuffer *output = bufferevent_get_output(bev);
  size_t remain = evbuffer_get_length(output);
  if (events & BEV_EVENT_ERROR) {
    perror("Error from bufferevent");
  }
  if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
    printf("closing, remain %zd\n", remain);
    bufferevent_free(bev);
  }
}


x86 assembly (clang, -O0):
<echo_event_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <bufferevent_get_output@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <evbuffer_get_length@plt>
mov    %rax,-0x28(%rbp)
movswl -0xa(%rbp),%eax
mov    0x2e64(%rip),%rcx
movswl (%rcx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11a6 <echo_event_cb+0x56>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
movswl -0xa(%rbp),%eax
mov    0x2e2f(%rip),%rcx
movswl (%rcx),%ecx
mov    0x2e35(%rip),%rdx
movswl (%rdx),%edx
or     %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11e4 <echo_event_cb+0x94>
mov    -0x28(%rbp),%rsi
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <bufferevent_free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_event_cb>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <bufferevent_get_output@plt>
mov    %rax,%rdi
call   1050 <evbuffer_get_length@plt>
mov    %rax,%r14
mov    0x2e7d(%rip),%r15
movzwl (%r15),%eax
test   %bp,%ax
jne    11b7 <echo_event_cb+0x67>
mov    0x2e5d(%rip),%rcx
or     (%rcx),%ax
test   %bp,%ax
je     11ac <echo_event_cb+0x5c>
lea    0xe85(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <bufferevent_free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
movzwl (%r15),%eax
jmp    117c <echo_event_cb+0x2c>

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@BEV_EVENT_ERROR = global i16 0
@.str = [23 x i8] c"Error from bufferevent\00"
@BEV_EVENT_EOF = global i16 0
@.str.1 = [21 x i8] c"closing, remain %zd\0A\00"
define void @echo_event_cb(%struct.bufferevent* %0, i16 signext %1, i8* %2) {
%4 = alloca %struct.bufferevent*
%5 = alloca i16
%6 = alloca i8*
%7 = alloca %struct.evbuffer*
%8 = alloca i64
store %struct.bufferevent* %0, %struct.bufferevent** %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%9 = load %struct.bufferevent*, %struct.bufferevent** %4
%10 = call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %9)
store %struct.evbuffer* %10, %struct.evbuffer** %7
%11 = load %struct.evbuffer*, %struct.evbuffer** %7
%12 = call i64 @evbuffer_get_length(%struct.evbuffer* %11)
store i64 %12, i64* %8
%13 = load i16, i16* %5
%14 = sext i16 %13 to i32
%15 = load i16, i16* @BEV_EVENT_ERROR
%16 = sext i16 %15 to i32
%17 = and i32 %14, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %3
%22 = load i16, i16* %5
%23 = sext i16 %22 to i32
%24 = load i16, i16* @BEV_EVENT_EOF
%25 = sext i16 %24 to i32
%26 = load i16, i16* @BEV_EVENT_ERROR
%27 = sext i16 %26 to i32
%28 = or i32 %25, %27
%29 = and i32 %23, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %21
%32 = load i64, i64* %8
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %32)
%34 = load %struct.bufferevent*, %struct.bufferevent** %4
%35 = call i32 @bufferevent_free(%struct.bufferevent* %34)
br label %36
36: ; preds = %31, %21
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@BEV_EVENT_ERROR = global i16 0
@.str = [23 x i8] c"Error from bufferevent\00"
@BEV_EVENT_EOF = global i16 0
@.str.1 = [21 x i8] c"closing, remain %zd\0A\00"
define void @echo_event_cb(%struct.bufferevent* %0, i16 signext %1, i8* nocapture readnone %2) {
%4 = tail call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %0)
%5 = tail call i64 @evbuffer_get_length(%struct.evbuffer* %4)
%6 = load i16, i16* @BEV_EVENT_ERROR
%7 = and i16 %6, %1
%8 = icmp eq i16 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%11 = load i16, i16* @BEV_EVENT_ERROR
br label %12
12: ; preds = %9, %3
%13 = phi i16 [ %11, %9 ], [ %6, %3 ]
%14 = load i16, i16* @BEV_EVENT_EOF
%15 = or i16 %13, %14
%16 = and i16 %15, %1
%17 = icmp eq i16 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %5)
%20 = tail call i32 @bufferevent_free(%struct.bufferevent* %0)
br label %21
21: ; preds = %18, %12
ret void
}