#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;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
struct evbuffer *bufferevent_get_input(struct bufferevent *);
size_t evbuffer_get_length(struct evbuffer *);
int /*<<< orphan*/ evbuffer_remove(struct evbuffer *, char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void xbt_tcp_handshake_read(struct bufferevent *be, void *arg) {
  fprintf(stderr, "read\n");
  struct evbuffer *input = bufferevent_get_input(be);
  size_t buflen = evbuffer_get_length(input);
  if (buflen <= 0)
    return;
  char buf[buflen];
  evbuffer_remove(input, buf, sizeof(buf));
  fprintf(stderr, "%s\n", buf);
}


x86 assembly (clang, -O0):
<xbt_tcp_handshake_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <bufferevent_get_input@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <evbuffer_get_length@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     1191 <xbt_tcp_handshake_read+0x51>
jmp    11e5 <xbt_tcp_handshake_read+0xa5>
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    %eax,%edx
call   1030 <evbuffer_remove@plt>
mov    -0x38(%rbp),%rdx
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xbt_tcp_handshake_read>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    0x2e8b(%rip),%r15
mov    (%r15),%edi
lea    0xea1(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <bufferevent_get_input@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <evbuffer_get_length@plt>
test   %rax,%rax
je     11b9 <xbt_tcp_handshake_read+0x79>
mov    %rsp,%r12
mov    %rsp,%rbx
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <evbuffer_remove@plt>
mov    (%r15),%edi
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@stderr = global i32 0
@.str = [6 x i8] c"read\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @xbt_tcp_handshake_read(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = load %struct.bufferevent*, %struct.bufferevent** %3
%12 = call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %11)
store %struct.evbuffer* %12, %struct.evbuffer** %5
%13 = load %struct.evbuffer*, %struct.evbuffer** %5
%14 = call i64 @evbuffer_get_length(%struct.evbuffer* %13)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp ule i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
br label %28
18: ; preds = %2
%19 = load i64, i64* %6
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %7
%21 = alloca i8, i64 %19
store i64 %19, i64* %8
%22 = load %struct.evbuffer*, %struct.evbuffer** %5
%23 = trunc i64 %19 to i32
%24 = call i32 @evbuffer_remove(%struct.evbuffer* %22, i8* %21, i32 %23)
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%27 = load i8*, i8** %7
br label %28
28: ; preds = %18, %17
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@stderr = global i32 0
@.str = [6 x i8] c"read\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @xbt_tcp_handshake_read(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = load i32, i32* @stderr
%4 = tail call i32 (i32, i8*, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = tail call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %0)
%6 = tail call i64 @evbuffer_get_length(%struct.evbuffer* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %6
%11 = trunc i64 %6 to i32
%12 = call i32 @evbuffer_remove(%struct.evbuffer* %5, i8* %10, i32 %11)
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %15
15: ; preds = %2, %8
ret void
}