#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*/ EVBUFFER_EOL_LF;
struct evbuffer *bufferevent_get_input(struct bufferevent *);
struct evbuffer *bufferevent_get_output(struct bufferevent *);
char *dispatch_command(char, char *, size_t const);
int /*<<< orphan*/ evbuffer_add(struct evbuffer *, char *, int);
char *evbuffer_readln(struct evbuffer *, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int strlen(char *);
void readcb(struct bufferevent *bev, void *ctx) {
  struct evbuffer *input, *output;
  char *line;
  size_t n;
  input = bufferevent_get_input(bev);
  output = bufferevent_get_output(bev);
  while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
    if (strlen(line) > 0) {
      char command = line[0];
      char *data = &line[1];
      const size_t length = strlen(data);
      printf("Command = %c\n", command);
      printf("Len = %lu\n", length);
      printf("Res = %s\n", data);
      char *result = dispatch_command(command, data, length);
      evbuffer_add(output, result, strlen(result));
    } else {
      puts("Some error");
    }
    evbuffer_add(output, "\n", 1);
    free(line);
  }
}


x86 assembly (clang, -O0):
<readcb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <bufferevent_get_input@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <bufferevent_get_output@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rsi
call   10b0 <evbuffer_readln@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12bd <readcb+0x12d>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jle    128e <readcb+0xfe>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,-0x48(%rbp)
movsbl -0x31(%rbp),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x48(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x31(%rbp),%al
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
movsbl %al,%edi
call   10a0 <dispatch_command@plt>
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1090 <evbuffer_add@plt>
jmp    129a <readcb+0x10a>
lea    0xd8e(%rip),%rdi
call   1040 <puts@plt>
mov    -0x20(%rbp),%rdi
lea    0xd67(%rip),%rsi
mov    $0x1,%edx
call   1090 <evbuffer_add@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11ba <readcb+0x2a>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1080 <bufferevent_get_input@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <bufferevent_get_output@plt>
mov    %rax,%r13
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rsi
mov    %r14,%rdi
call   10b0 <evbuffer_readln@plt>
test   %rax,%rax
je     129e <readcb+0x10e>
mov    %rax,%rbx
jmp    1224 <readcb+0x94>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe3c(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
lea    0xe16(%rip),%rsi
mov    $0x1,%edx
call   1090 <evbuffer_add@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rsp,%rsi
call   10b0 <evbuffer_readln@plt>
mov    %rax,%rbx
test   %rax,%rax
je     129e <readcb+0x10e>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11e0 <readcb+0x50>
movsbl (%rbx),%ebp
lea    0x1(%rbx),%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%r12
lea    0xdb7(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdb5(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdaf(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10a0 <dispatch_command@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1090 <evbuffer_add@plt>
jmp    11ec <readcb+0x5c>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@EVBUFFER_EOL_LF = global i32 0
@.str = [14 x i8] c"Command = %c\0A\00"
@.str.1 = [11 x i8] c"Len = %lu\0A\00"
@.str.2 = [10 x i8] c"Res = %s\0A\00"
@.str.3 = [11 x i8] c"Some error\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @readcb(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca %struct.evbuffer*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%13 = load %struct.bufferevent*, %struct.bufferevent** %3
%14 = call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %13)
store %struct.evbuffer* %14, %struct.evbuffer** %5
%15 = load %struct.bufferevent*, %struct.bufferevent** %3
%16 = call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %15)
store %struct.evbuffer* %16, %struct.evbuffer** %6
br label %17
17: ; preds = %53, %2
%18 = load %struct.evbuffer*, %struct.evbuffer** %5
%19 = load i32, i32* @EVBUFFER_EOL_LF
%20 = call i8* @evbuffer_readln(%struct.evbuffer* %18, i64* %8, i32 %19)
store i8* %20, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %58
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = call i32 @strlen(i8* %23)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %51
26: ; preds = %22
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
store i8 %29, i8* %9
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8* %31, i8** %10
%32 = load i8*, i8** %10
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
store i64 %34, i64* %11
%35 = load i8, i8* %9
%36 = sext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i64, i64* %11
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %38)
%40 = load i8*, i8** %10
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i8, i8* %9
%43 = load i8*, i8** %10
%44 = load i64, i64* %11
%45 = call i8* @dispatch_command(i8 signext %42, i8* %43, i64 %44)
store i8* %45, i8** %12
%46 = load %struct.evbuffer*, %struct.evbuffer** %6
%47 = load i8*, i8** %12
%48 = load i8*, i8** %12
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @evbuffer_add(%struct.evbuffer* %46, i8* %47, i32 %49)
br label %53
51: ; preds = %22
%52 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %26
%54 = load %struct.evbuffer*, %struct.evbuffer** %6
%55 = call i32 @evbuffer_add(%struct.evbuffer* %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%56 = load i8*, i8** %7
%57 = call i32 @free(i8* %56)
br label %17
58: ; preds = %17
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@EVBUFFER_EOL_LF = global i32 0
@.str = [14 x i8] c"Command = %c\0A\00"
@.str.1 = [11 x i8] c"Len = %lu\0A\00"
@.str.2 = [10 x i8] c"Res = %s\0A\00"
@.str.3 = [11 x i8] c"Some error\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @readcb(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %0)
%6 = tail call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %0)
%7 = load i32, i32* @EVBUFFER_EOL_LF
%8 = call i8* @evbuffer_readln(%struct.evbuffer* %5, i64* %3, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %34, label %10
10: ; preds = %2, %28
%11 = phi i8* [ %32, %28 ], [ %8, %2 ]
%12 = call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load i8, i8* %11
%16 = getelementptr inbounds i8, i8* %11, i64 1
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = sext i8 %15 to i32
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %18)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%23 = call i8* @dispatch_command(i8 signext %15, i8* %16, i64 %18)
%24 = call i32 @strlen(i8* %23)
%25 = call i32 @evbuffer_add(%struct.evbuffer* %6, i8* %23, i32 %24)
br label %28
26: ; preds = %10
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %14
%29 = call i32 @evbuffer_add(%struct.evbuffer* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%30 = call i32 @free(i8* %11)
%31 = load i32, i32* @EVBUFFER_EOL_LF
%32 = call i8* @evbuffer_readln(%struct.evbuffer* %5, i64* %3, i32 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %10
34: ; preds = %28, %2
ret void
}