#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_ptr {
  int pos;
};
struct evbuffer {
  int dummy;
};
struct bufferevent {
  int dummy;
};
typedef int /*<<< orphan*/ request;

/* Variables and functions */
int /*<<< orphan*/ EVBUFFER_EOL_LF;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bufferevent_free(struct bufferevent *);
struct evbuffer *bufferevent_get_input(struct bufferevent *);
struct evbuffer *bufferevent_get_output(struct bufferevent *);
int evbuffer_add(struct evbuffer *, char *, int);
int evbuffer_get_length(struct evbuffer *);
int evbuffer_remove(struct evbuffer *, char *, int);
struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *, int /*<<< orphan*/ *,
                                        size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, size_t);
void echo_read_cb(struct bufferevent *bev, void *ctx) {
  struct evbuffer *input = bufferevent_get_input(bev);
  struct evbuffer *output = bufferevent_get_output(bev);
  while (1) {
    size_t eol_len = 0;
    struct evbuffer_ptr eol =
        evbuffer_search_eol(input, NULL, &eol_len, EVBUFFER_EOL_LF);
    if (eol.pos < 0) {
      size_t readable = evbuffer_get_length(input);
      if (readable > 10240) {
        printf("input is too long %zd\n", readable);
        bufferevent_free(bev);
      }
      break;
    } else if (eol.pos > 10240) {
      printf("line is too long %zd\n", eol.pos);
      bufferevent_free(bev);
      break;
    } else {
      assert(eol_len == 1);
      char request[10240 + 1];
      assert(eol.pos + 1 <= sizeof(request));
      int req_len = evbuffer_remove(input, request, eol.pos + 1);
      assert(req_len == eol.pos + 1);
      char response[10240 + 1];
      int resp_len = req_len;
      memcpy(response, request, req_len);
      int succeed = evbuffer_add(output, response, resp_len);
      assert(succeed == 0);
      if (evbuffer_get_length(output) > 102400) {
        bufferevent_free(bev);
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<echo_read_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x5060,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <bufferevent_get_input@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <bufferevent_get_output@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
call   10c0 <evbuffer_search_eol@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1237 <echo_read_cb+0x97>
mov    -0x18(%rbp),%rdi
call   10a0 <evbuffer_get_length@plt>
cltq
mov    %rax,-0x38(%rbp)
cmpq   $0x2800,-0x38(%rbp)
jbe    1232 <echo_read_cb+0x92>
mov    -0x38(%rbp),%rsi
lea    0xddc(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <bufferevent_free@plt>
jmp    134c <echo_read_cb+0x1ac>
cmpl   $0x2800,-0x30(%rbp)
jle    1262 <echo_read_cb+0xc2>
movslq -0x30(%rbp),%rsi
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <bufferevent_free@plt>
jmp    134c <echo_read_cb+0x1ac>
cmpq   $0x1,-0x28(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
cltq
cmp    $0x2801,%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
lea    -0x2840(%rbp),%rsi
mov    -0x30(%rbp),%edx
add    $0x1,%edx
call   1030 <evbuffer_remove@plt>
mov    %eax,-0x2844(%rbp)
mov    -0x2844(%rbp),%eax
mov    -0x30(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x2844(%rbp),%eax
mov    %eax,-0x5054(%rbp)
lea    -0x5050(%rbp),%rdi
lea    -0x2840(%rbp),%rsi
mov    -0x2844(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%rdi
lea    -0x5050(%rbp),%rsi
mov    -0x5054(%rbp),%edx
call   1090 <evbuffer_add@plt>
mov    %eax,-0x5058(%rbp)
cmpl   $0x0,-0x5058(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <evbuffer_get_length@plt>
cmp    $0x19000,%eax
jle    133d <echo_read_cb+0x19d>
mov    -0x8(%rbp),%rdi
call   10b0 <bufferevent_free@plt>
jmp    134c <echo_read_cb+0x1ac>
jmp    1342 <echo_read_cb+0x1a2>
jmp    1347 <echo_read_cb+0x1a7>
jmp    11cd <echo_read_cb+0x2d>
add    $0x5060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_read_cb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5028,%rsp
mov    %rdi,%rbp
call   1060 <bufferevent_get_input@plt>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdi
call   1050 <bufferevent_get_output@plt>
mov    %rax,%r15
movq   $0x0,(%rsp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <evbuffer_search_eol@plt>
test   %eax,%eax
js     12b8 <echo_read_cb+0x118>
mov    %eax,%r14d
lea    0x2820(%rsp),%r12
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2801,%r14d
jae    12d3 <echo_read_cb+0x133>
xor    %edi,%edi
cmpq   $0x1,(%rsp)
sete   %dil
call   1070 <assert@plt>
add    $0x1,%r14d
mov    $0x1,%edi
call   1070 <assert@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <evbuffer_remove@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    %r14d,%eax
sete   %dil
call   1070 <assert@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1080 <memcpy@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1090 <evbuffer_add@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   10a0 <evbuffer_get_length@plt>
cmp    $0x19001,%eax
jge    12e2 <echo_read_cb+0x142>
movq   $0x0,(%rsp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rsp,%rdx
call   10c0 <evbuffer_search_eol@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1210 <echo_read_cb+0x70>
mov    %rbx,%rdi
call   10a0 <evbuffer_get_length@plt>
cmp    $0x2801,%eax
jb     12ec <echo_read_cb+0x14c>
movslq %eax,%rsi
lea    0xd2f(%rip),%rdi
jmp    12dd <echo_read_cb+0x13d>
mov    %r14d,%esi
lea    0xd3a(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rsp),%rdi
call   10b0 <bufferevent_free@plt>
add    $0x5028,%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 }
%struct.evbuffer_ptr = type { i32 }
@EVBUFFER_EOL_LF = global i32 0
@.str = [23 x i8] c"input is too long %zd\0A\00"
@.str.1 = [22 x i8] c"line is too long %zd\0A\00"
define void @echo_read_cb(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca %struct.evbuffer*
%7 = alloca i64
%8 = alloca %struct.evbuffer_ptr
%9 = alloca i64
%10 = alloca [10241 x i8]
%11 = alloca i32
%12 = alloca [10241 x i8]
%13 = alloca i32
%14 = alloca i32
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%15 = load %struct.bufferevent*, %struct.bufferevent** %3
%16 = call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %15)
store %struct.evbuffer* %16, %struct.evbuffer** %5
%17 = load %struct.bufferevent*, %struct.bufferevent** %3
%18 = call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %17)
store %struct.evbuffer* %18, %struct.evbuffer** %6
br label %19
19: ; preds = %2, %96
store i64 0, i64* %7
%20 = load %struct.evbuffer*, %struct.evbuffer** %5
%21 = load i32, i32* @EVBUFFER_EOL_LF
%22 = call i32 @evbuffer_search_eol(%struct.evbuffer* %20, i32* null, i64* %7, i32 %21)
%23 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %19
%28 = load %struct.evbuffer*, %struct.evbuffer** %5
%29 = call i32 @evbuffer_get_length(%struct.evbuffer* %28)
%30 = sext i32 %29 to i64
store i64 %30, i64* %9
%31 = load i64, i64* %9
%32 = icmp ugt i64 %31, 10240
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i64, i64* %9
%35 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %34)
%36 = load %struct.bufferevent*, %struct.bufferevent** %3
%37 = call i32 @bufferevent_free(%struct.bufferevent* %36)
br label %38
38: ; preds = %33, %27
br label %97
39: ; preds = %19
%40 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
%41 = load i32, i32* %40
%42 = icmp sgt i32 %41, 10240
br i1 %42, label %43, label %50
43: ; preds = %39
%44 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
%45 = load i32, i32* %44
%46 = sext i32 %45 to i64
%47 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 %46)
%48 = load %struct.bufferevent*, %struct.bufferevent** %3
%49 = call i32 @bufferevent_free(%struct.bufferevent* %48)
br label %97
50: ; preds = %39
%51 = load i64, i64* %7
%52 = icmp eq i64 %51, 1
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = icmp ule i64 %58, 10241
%60 = zext i1 %59 to i32
%61 = call i32 @assert(i32 %60)
%62 = load %struct.evbuffer*, %struct.evbuffer** %5
%63 = getelementptr inbounds [10241 x i8], [10241 x i8]* %10, i64 0, i64 0
%64 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
%65 = load i32, i32* %64
%66 = add nsw i32 %65, 1
%67 = call i32 @evbuffer_remove(%struct.evbuffer* %62, i8* %63, i32 %66)
store i32 %67, i32* %11
%68 = load i32, i32* %11
%69 = getelementptr inbounds %struct.evbuffer_ptr, %struct.evbuffer_ptr* %8, i32 0, i32 0
%70 = load i32, i32* %69
%71 = add nsw i32 %70, 1
%72 = icmp eq i32 %68, %71
%73 = zext i1 %72 to i32
%74 = call i32 @assert(i32 %73)
%75 = load i32, i32* %11
store i32 %75, i32* %13
%76 = getelementptr inbounds [10241 x i8], [10241 x i8]* %12, i64 0, i64 0
%77 = getelementptr inbounds [10241 x i8], [10241 x i8]* %10, i64 0, i64 0
%78 = load i32, i32* %11
%79 = call i32 @memcpy(i8* %76, i8* %77, i32 %78)
%80 = load %struct.evbuffer*, %struct.evbuffer** %6
%81 = getelementptr inbounds [10241 x i8], [10241 x i8]* %12, i64 0, i64 0
%82 = load i32, i32* %13
%83 = call i32 @evbuffer_add(%struct.evbuffer* %80, i8* %81, i32 %82)
store i32 %83, i32* %14
%84 = load i32, i32* %14
%85 = icmp eq i32 %84, 0
%86 = zext i1 %85 to i32
%87 = call i32 @assert(i32 %86)
%88 = load %struct.evbuffer*, %struct.evbuffer** %6
%89 = call i32 @evbuffer_get_length(%struct.evbuffer* %88)
%90 = icmp sgt i32 %89, 102400
br i1 %90, label %91, label %94
91: ; preds = %50
%92 = load %struct.bufferevent*, %struct.bufferevent** %3
%93 = call i32 @bufferevent_free(%struct.bufferevent* %92)
br label %97
94: ; preds = %50
br label %95
95: ; preds = %94
br label %96
96: ; preds = %95
br label %19
97: ; preds = %91, %43, %38
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@EVBUFFER_EOL_LF = global i32 0
@.str = [23 x i8] c"input is too long %zd\0A\00"
@.str.1 = [22 x i8] c"line is too long %zd\0A\00"
define void @echo_read_cb(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = alloca i64
%4 = alloca [10241 x i8]
%5 = alloca [10241 x i8]
%6 = tail call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %0)
%7 = tail call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %0)
%8 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%9 = load i32, i32* @EVBUFFER_EOL_LF
%10 = call i32 @evbuffer_search_eol(%struct.evbuffer* %6, i32* null, i64* %3, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = getelementptr inbounds [10241 x i8], [10241 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [10241 x i8], [10241 x i8]* %5, i64 0, i64 0
br label %21
15: ; preds = %50, %2
%16 = call i32 @evbuffer_get_length(%struct.evbuffer* %6)
%17 = icmp ugt i32 %16, 10240
br i1 %17, label %18, label %49
18: ; preds = %15
%19 = sext i32 %16 to i64
%20 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %19)
br label %47
21: ; preds = %12, %50
%22 = phi i32 [ %10, %12 ], [ %52, %50 ]
%23 = icmp ugt i32 %22, 10240
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = zext i32 %22 to i64
%26 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 %25)
br label %47
27: ; preds = %21
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, 1
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = add nuw nsw i32 %22, 1
%33 = call i32 @assert(i32 1)
%34 = call i32 @evbuffer_remove(%struct.evbuffer* %6, i8* %13, i32 %32)
%35 = icmp eq i32 %34, %32
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = call i32 @memcpy(i8* %14, i8* %13, i32 %34)
%39 = call i32 @evbuffer_add(%struct.evbuffer* %7, i8* %14, i32 %34)
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = call i32 @evbuffer_get_length(%struct.evbuffer* %7)
%44 = icmp sgt i32 %43, 102400
br i1 %44, label %45, label %50
45: ; preds = %27
%46 = call i32 @bufferevent_free(%struct.bufferevent* %0)
br label %54
47: ; preds = %18, %24
%48 = call i32 @bufferevent_free(%struct.bufferevent* %0)
br label %49
49: ; preds = %47, %15
br label %54
50: ; preds = %27
store i64 0, i64* %3
%51 = load i32, i32* @EVBUFFER_EOL_LF
%52 = call i32 @evbuffer_search_eol(%struct.evbuffer* %6, i32* null, i64* %3, i32 %51)
%53 = icmp slt i32 %52, 0
br i1 %53, label %15, label %21
54: ; preds = %45, %49
ret void
}