#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 */
int /*<<< orphan*/ bufferevent_free(struct bufferevent *);
struct evbuffer *bufferevent_get_output(struct bufferevent *);
size_t evbuffer_get_length(struct evbuffer *);
int /*<<< orphan*/ printf(char *, size_t);
void echo_write_complete_cb(struct bufferevent *bev, void *ctx) {
  struct evbuffer *output = bufferevent_get_output(bev);
  size_t remain = evbuffer_get_length(output);
  printf("write_complete_cb, remain %zd\n", remain);
  if (remain == 0)
    bufferevent_free(bev);
}


x86 assembly (clang, -O0):
<echo_write_complete_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <bufferevent_get_output@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <evbuffer_get_length@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe8b(%rip),%rdi
call   1030 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
jne    118e <echo_write_complete_cb+0x4e>
mov    -0x8(%rbp),%rdi
call   1060 <bufferevent_free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_write_complete_cb>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <bufferevent_get_output@plt>
mov    %rax,%rdi
call   1050 <evbuffer_get_length@plt>
mov    %rax,%rbx
lea    0xea2(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
test   %rbx,%rbx
je     1173 <echo_write_complete_cb+0x33>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <bufferevent_free@plt>

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [31 x i8] c"write_complete_cb, remain %zd\0A\00"
define void @echo_write_complete_cb(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca i64
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%7 = load %struct.bufferevent*, %struct.bufferevent** %3
%8 = call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %7)
store %struct.evbuffer* %8, %struct.evbuffer** %5
%9 = load %struct.evbuffer*, %struct.evbuffer** %5
%10 = call i64 @evbuffer_get_length(%struct.evbuffer* %9)
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = load i64, i64* %6
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load %struct.bufferevent*, %struct.bufferevent** %3
%17 = call i32 @bufferevent_free(%struct.bufferevent* %16)
br label %18
18: ; preds = %15, %2
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [31 x i8] c"write_complete_cb, remain %zd\0A\00"
define void @echo_write_complete_cb(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = tail call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %0)
%4 = tail call i64 @evbuffer_get_length(%struct.evbuffer* %3)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %4)
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @bufferevent_free(%struct.bufferevent* %0)
br label %9
9: ; preds = %7, %2
ret void
}