#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int uint32_t;
struct evbuffer {
  int dummy;
};
struct conn {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ evbuffer_drain(struct evbuffer *, int);
int /*<<< orphan*/ evbuffer_remove(struct evbuffer *, int /*<<< orphan*/ *,
                                   int);
int get_uint32(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ send_err(struct conn *, int, int /*<<< orphan*/ const *,
                            char *, int);
int /*<<< orphan*/ start_request(int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ const *, struct conn *, int,
                                 int, int /*<<< orphan*/ *);
int handle_get_command(struct conn *conn, const uint8_t *req_id, uint32_t flags,
                       struct evbuffer *input, uint32_t cmd_len) {
  if (cmd_len < 40) {
    send_err(conn, 2, req_id, "GET too short", 1);
    return 1;
  }
  uint8_t get_header[40];
  evbuffer_remove(input, get_header, 40);
  uint32_t n_bits = get_uint32(get_header + 32);
  uint32_t block_size = get_uint32(get_header + 36);
  uint32_t bf_len = (n_bits + 7) / 8;
  if (bf_len > cmd_len - 40) {
    send_err(conn, 3, req_id, "No room for bitfield", 1);
    return 1;
  }
  uint8_t *bits = malloc(bf_len);
  if (!bits) {
    send_err(conn, 4, req_id, "Internal error", 1);
    return 1;
  }
  evbuffer_remove(input, bits, bf_len);
  evbuffer_drain(input, cmd_len - 40 - bf_len);
  start_request(get_header, req_id, conn, n_bits, block_size, bits);
  return 0;
}


x86 assembly (clang, -O0):
<handle_get_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x28,-0x2c(%rbp)
jge    11b3 <handle_get_command+0x53>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x2,%esi
lea    0xe64(%rip),%rcx
mov    $0x1,%r8d
call   1060 <send_err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <handle_get_command+0x195>
mov    -0x28(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
mov    $0x28,%edx
call   1040 <evbuffer_remove@plt>
lea    -0xd0(%rbp),%rdi
add    $0x80,%rdi
call   1050 <get_uint32@plt>
mov    %eax,-0xd4(%rbp)
lea    -0xd0(%rbp),%rdi
add    $0x90,%rdi
call   1050 <get_uint32@plt>
mov    %eax,-0xd8(%rbp)
mov    -0xd4(%rbp),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0xdc(%rbp)
mov    -0xdc(%rbp),%eax
mov    -0x2c(%rbp),%ecx
sub    $0x28,%ecx
cmp    %ecx,%eax
jle    1250 <handle_get_command+0xf0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3,%esi
lea    0xdd5(%rip),%rcx
mov    $0x1,%r8d
call   1060 <send_err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <handle_get_command+0x195>
mov    -0xdc(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0xe8(%rbp)
cmpq   $0x0,-0xe8(%rbp)
jne    129b <handle_get_command+0x13b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x4,%esi
lea    0xd9f(%rip),%rcx
mov    $0x1,%r8d
call   1060 <send_err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <handle_get_command+0x195>
mov    -0x28(%rbp),%rdi
mov    -0xe8(%rbp),%rsi
mov    -0xdc(%rbp),%edx
call   1040 <evbuffer_remove@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
sub    $0x28,%esi
sub    -0xdc(%rbp),%esi
call   1080 <evbuffer_drain@plt>
lea    -0xd0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0xd4(%rbp),%ecx
mov    -0xd8(%rbp),%r8d
mov    -0xe8(%rbp),%r9
call   1030 <start_request@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_get_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
cmp    $0x27,%r8d
jg     1196 <handle_get_command+0x36>
lea    0xe7c(%rip),%rcx
mov    $0x1,%ebx
mov    %r15,%rdi
mov    $0x2,%esi
jmp    1257 <handle_get_command+0xf7>
mov    %r8d,%ebp
mov    %rcx,%r12
lea    0x10(%rsp),%rsi
mov    %rcx,%rdi
mov    $0x28,%edx
call   1040 <evbuffer_remove@plt>
lea    0x90(%rsp),%rdi
call   1050 <get_uint32@plt>
mov    %eax,%ebx
lea    0xa0(%rsp),%rdi
call   1050 <get_uint32@plt>
lea    0x7(%rbx),%ecx
lea    0xe(%rbx),%r13d
test   %ecx,%ecx
cmovns %ecx,%r13d
sar    $0x3,%r13d
add    $0xffffffd8,%ebp
sub    %r13d,%ebp
jge    11f9 <handle_get_command+0x99>
lea    0xe24(%rip),%rcx
mov    $0x1,%ebx
mov    %r15,%rdi
mov    $0x3,%esi
jmp    1257 <handle_get_command+0xf7>
mov    %eax,0xc(%rsp)
mov    %r13d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1243 <handle_get_command+0xe3>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r13d,%edx
mov    %rax,%r13
call   1040 <evbuffer_remove@plt>
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <evbuffer_drain@plt>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
mov    0xc(%rsp),%r8d
mov    %r13,%r9
call   1030 <start_request@plt>
xor    %ebx,%ebx
jmp    1265 <handle_get_command+0x105>
lea    0xdd9(%rip),%rcx
mov    $0x1,%ebx
mov    %r15,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
mov    $0x1,%r8d
call   1060 <send_err@plt>
mov    %ebx,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.conn = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [14 x i8] c"GET too short\00"
@.str.1 = [21 x i8] c"No room for bitfield\00"
@.str.2 = [15 x i8] c"Internal error\00"
define i32 @handle_get_command(%struct.conn* %0, i32* %1, i32 %2, %struct.evbuffer* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.conn*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.evbuffer*
%11 = alloca i32
%12 = alloca [40 x i32]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
store %struct.conn* %0, %struct.conn** %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store %struct.evbuffer* %3, %struct.evbuffer** %10
store i32 %4, i32* %11
%17 = load i32, i32* %11
%18 = icmp slt i32 %17, 40
br i1 %18, label %19, label %23
19: ; preds = %5
%20 = load %struct.conn*, %struct.conn** %7
%21 = load i32*, i32** %8
%22 = call i32 @send_err(%struct.conn* %20, i32 2, i32* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 1, i32* %6
br label %71
23: ; preds = %5
%24 = load %struct.evbuffer*, %struct.evbuffer** %10
%25 = getelementptr inbounds [40 x i32], [40 x i32]* %12, i64 0, i64 0
%26 = call i32 @evbuffer_remove(%struct.evbuffer* %24, i32* %25, i32 40)
%27 = getelementptr inbounds [40 x i32], [40 x i32]* %12, i64 0, i64 0
%28 = getelementptr inbounds i32, i32* %27, i64 32
%29 = call i32 @get_uint32(i32* %28)
store i32 %29, i32* %13
%30 = getelementptr inbounds [40 x i32], [40 x i32]* %12, i64 0, i64 0
%31 = getelementptr inbounds i32, i32* %30, i64 36
%32 = call i32 @get_uint32(i32* %31)
store i32 %32, i32* %14
%33 = load i32, i32* %13
%34 = add nsw i32 %33, 7
%35 = sdiv i32 %34, 8
store i32 %35, i32* %15
%36 = load i32, i32* %15
%37 = load i32, i32* %11
%38 = sub nsw i32 %37, 40
%39 = icmp sgt i32 %36, %38
br i1 %39, label %40, label %44
40: ; preds = %23
%41 = load %struct.conn*, %struct.conn** %7
%42 = load i32*, i32** %8
%43 = call i32 @send_err(%struct.conn* %41, i32 3, i32* %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 1, i32* %6
br label %71
44: ; preds = %23
%45 = load i32, i32* %15
%46 = call i32* @malloc(i32 %45)
store i32* %46, i32** %16
%47 = load i32*, i32** %16
%48 = icmp ne i32* %47, null
br i1 %48, label %53, label %49
49: ; preds = %44
%50 = load %struct.conn*, %struct.conn** %7
%51 = load i32*, i32** %8
%52 = call i32 @send_err(%struct.conn* %50, i32 4, i32* %51, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 1, i32* %6
br label %71
53: ; preds = %44
%54 = load %struct.evbuffer*, %struct.evbuffer** %10
%55 = load i32*, i32** %16
%56 = load i32, i32* %15
%57 = call i32 @evbuffer_remove(%struct.evbuffer* %54, i32* %55, i32 %56)
%58 = load %struct.evbuffer*, %struct.evbuffer** %10
%59 = load i32, i32* %11
%60 = sub nsw i32 %59, 40
%61 = load i32, i32* %15
%62 = sub nsw i32 %60, %61
%63 = call i32 @evbuffer_drain(%struct.evbuffer* %58, i32 %62)
%64 = getelementptr inbounds [40 x i32], [40 x i32]* %12, i64 0, i64 0
%65 = load i32*, i32** %8
%66 = load %struct.conn*, %struct.conn** %7
%67 = load i32, i32* %13
%68 = load i32, i32* %14
%69 = load i32*, i32** %16
%70 = call i32 @start_request(i32* %64, i32* %65, %struct.conn* %66, i32 %67, i32 %68, i32* %69)
store i32 0, i32* %6
br label %71
71: ; preds = %53, %49, %40, %19
%72 = load i32, i32* %6
ret i32 %72
}

LLVM IR (-O2):
%struct.conn = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [14 x i8] c"GET too short\00"
@.str.1 = [21 x i8] c"No room for bitfield\00"
@.str.2 = [15 x i8] c"Internal error\00"
define i32 @handle_get_command(%struct.conn* %0, i32* %1, i32 %2, %struct.evbuffer* %3, i32 %4) {
%6 = alloca [40 x i32]
%7 = icmp slt i32 %4, 40
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @send_err(%struct.conn* %0, i32 2, i32* %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 1)
br label %36
10: ; preds = %5
%11 = bitcast [40 x i32]* %6 to i8*
%12 = getelementptr inbounds [40 x i32], [40 x i32]* %6, i64 0, i64 0
%13 = call i32 @evbuffer_remove(%struct.evbuffer* %3, i32* %12, i32 40)
%14 = getelementptr inbounds [40 x i32], [40 x i32]* %6, i64 0, i64 32
%15 = call i32 @get_uint32(i32* %14)
%16 = getelementptr inbounds [40 x i32], [40 x i32]* %6, i64 0, i64 36
%17 = call i32 @get_uint32(i32* %16)
%18 = add nsw i32 %15, 7
%19 = sdiv i32 %18, 8
%20 = add nsw i32 %4, -40
%21 = icmp slt i32 %20, %19
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @send_err(%struct.conn* %0, i32 3, i32* %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %34
24: ; preds = %10
%25 = call i32* @malloc(i32 %19)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @send_err(%struct.conn* %0, i32 4, i32* %1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %34
29: ; preds = %24
%30 = call i32 @evbuffer_remove(%struct.evbuffer* %3, i32* %25, i32 %19)
%31 = sub nsw i32 %20, %19
%32 = call i32 @evbuffer_drain(%struct.evbuffer* %3, i32 %31)
%33 = call i32 @start_request(i32* %12, i32* %1, %struct.conn* %0, i32 %15, i32 %17, i32* %25)
br label %34
34: ; preds = %27, %29, %22
%35 = phi i32 [ 1, %22 ], [ 0, %29 ], [ 1, %27 ]
br label %36
36: ; preds = %34, %8
%37 = phi i32 [ 1, %8 ], [ %35, %34 ]
ret i32 %37
}