#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 */
struct evbuffer *bufferevent_get_input(struct bufferevent *);
int evbuffer_get_length(struct evbuffer *);
int /*<<< orphan*/ evbuffer_remove(struct evbuffer *, char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
void readcb(struct bufferevent *bev, void *ctx) {
  struct evbuffer *input = bufferevent_get_input(bev);
  int len = evbuffer_get_length(input);
  if (len > 0) {
    char *buf = malloc(len + 1);
    memset(buf, 0, len + 1);
    evbuffer_remove(input, buf, len);
    printf("%s", buf);
  }
}


x86 assembly (clang, -O0):
<readcb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <bufferevent_get_input@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <evbuffer_get_length@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11d3 <readcb+0x73>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <evbuffer_remove@plt>
mov    -0x28(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcb>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1060 <bufferevent_get_input@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <evbuffer_get_length@plt>
test   %eax,%eax
jle    11be <readcb+0x5e>
mov    %eax,%ebx
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <evbuffer_remove@plt>
lea    0xe54(%rip),%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [3 x i8] c"%s\00"
define void @readcb(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = tail call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %0)
%4 = tail call i32 @evbuffer_get_length(%struct.evbuffer* %3)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = add nuw nsw i32 %4, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @memset(i8* %8, i32 0, i32 %7)
%10 = tail call i32 @evbuffer_remove(%struct.evbuffer* %3, i8* %8, i32 %4)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %12
12: ; preds = %6, %2
ret void
}