#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 worker {
  int dummy;
};
struct evbuffer {
  int dummy;
};
struct distribution {
  int dummy;
};
struct conn {
  int dummy;
};

/* Variables and functions */
scalar_t__ distribution_get_metadata(struct distribution const *,
                                     int /*<<< orphan*/ const **, size_t *);
int /*<<< orphan*/ evbuffer_drain(struct evbuffer *, int);
int /*<<< orphan*/ evbuffer_remove(struct evbuffer *, int /*<<< orphan*/ *,
                                   int);
struct worker *find_worker(int /*<<< orphan*/ const *const);
int /*<<< orphan*/ send_data(struct conn *, int /*<<< orphan*/ const *, int,
                             size_t, int /*<<< orphan*/ const *);
int /*<<< orphan*/ send_err(struct conn *, int, int /*<<< orphan*/ const *,
                            char *, int);
struct distribution *worker_get_distribution(struct worker *);
int handle_get_metadata_command(struct conn *conn, const uint8_t *req_id,
                                uint32_t flags, struct evbuffer *input,
                                uint32_t cmd_len) {
  if (cmd_len < 32) {
    send_err(conn, 2, req_id, "GET_METADATA too short", 1);
    return 1;
  }
  uint8_t getmeta_header[32];
  evbuffer_remove(input, getmeta_header, 32);
  const uint8_t *const dist_id = getmeta_header;
  evbuffer_drain(input, cmd_len - 32);
  struct worker *w = find_worker(dist_id);
  if (!w) {
    send_err(conn, 6, req_id, "No such distribution", 0);
    return 0;
  }
  const struct distribution *dist = worker_get_distribution(w);
  const uint8_t *metadata;
  size_t metadata_len;
  if (distribution_get_metadata(dist, &metadata, &metadata_len) < 0) {
    send_err(conn, 9, req_id, "Distribution had no metadata", 0);
    return 0;
  }
  send_data(conn, req_id, 0x2003, metadata_len, metadata);
  return 0;
}


x86 assembly (clang, -O0):
<handle_get_metadata_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x20,-0x2c(%rbp)
jge    11c3 <handle_get_metadata_command+0x53>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x2,%esi
lea    0xe54(%rip),%rcx
mov    $0x1,%r8d
call   1060 <send_err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c4 <handle_get_metadata_command+0x154>
mov    -0x28(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
mov    $0x20,%edx
call   1040 <evbuffer_remove@plt>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
sub    $0x20,%esi
call   1090 <evbuffer_drain@plt>
mov    -0xb8(%rbp),%rdi
call   1070 <find_worker@plt>
mov    %rax,-0xc0(%rbp)
cmpq   $0x0,-0xc0(%rbp)
jne    123e <handle_get_metadata_command+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x6,%esi
lea    0xded(%rip),%rcx
xor    %r8d,%r8d
call   1060 <send_err@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <handle_get_metadata_command+0x154>
mov    -0xc0(%rbp),%rdi
call   1030 <worker_get_distribution@plt>
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
lea    -0xd8(%rbp),%rdx
call   1050 <distribution_get_metadata@plt>
cmp    $0x0,%rax
jge    129d <handle_get_metadata_command+0x12d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x9,%esi
lea    0xda3(%rip),%rcx
xor    %r8d,%r8d
call   1060 <send_err@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <handle_get_metadata_command+0x154>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xd8(%rbp),%rcx
mov    -0xd0(%rbp),%r8
mov    $0x2003,%edx
call   1080 <send_data@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_get_metadata_command>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
cmp    $0x1f,%r8d
jg     11b2 <handle_get_metadata_command+0x42>
lea    0xe6e(%rip),%rcx
mov    $0x1,%ebx
mov    %r15,%rdi
mov    $0x2,%esi
mov    %r14,%rdx
mov    $0x1,%r8d
call   1060 <send_err@plt>
jmp    124c <handle_get_metadata_command+0xdc>
mov    %r8d,%ebp
mov    %rcx,%rbx
lea    0x10(%rsp),%r12
mov    %rcx,%rdi
mov    %r12,%rsi
mov    $0x20,%edx
call   1040 <evbuffer_remove@plt>
add    $0xffffffe0,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <evbuffer_drain@plt>
mov    %r12,%rdi
call   1070 <find_worker@plt>
test   %rax,%rax
je     121f <handle_get_metadata_command+0xaf>
mov    %rax,%rdi
call   1030 <worker_get_distribution@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1050 <distribution_get_metadata@plt>
test   %rax,%rax
js     1230 <handle_get_metadata_command+0xc0>
mov    (%rsp),%rcx
mov    0x8(%rsp),%r8
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x2003,%edx
call   1080 <send_data@plt>
jmp    124a <handle_get_metadata_command+0xda>
lea    0xdf1(%rip),%rcx
mov    %r15,%rdi
mov    $0x6,%esi
jmp    123f <handle_get_metadata_command+0xcf>
lea    0xdf5(%rip),%rcx
mov    %r15,%rdi
mov    $0x9,%esi
mov    %r14,%rdx
xor    %r8d,%r8d
call   1060 <send_err@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.conn = type { i32 }
%struct.evbuffer = type { i32 }
%struct.worker = type { i32 }
%struct.distribution = type { i32 }
@.str = [23 x i8] c"GET_METADATA too short\00"
@.str.1 = [21 x i8] c"No such distribution\00"
@.str.2 = [29 x i8] c"Distribution had no metadata\00"
define i32 @handle_get_metadata_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 [32 x i32]
%13 = alloca i32*
%14 = alloca %struct.worker*
%15 = alloca %struct.distribution*
%16 = alloca i32*
%17 = alloca i64
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
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 32
br i1 %19, label %20, label %24
20: ; preds = %5
%21 = load %struct.conn*, %struct.conn** %7
%22 = load i32*, i32** %8
%23 = call i32 @send_err(%struct.conn* %21, i32 2, i32* %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 1, i32* %6
br label %57
24: ; preds = %5
%25 = load %struct.evbuffer*, %struct.evbuffer** %10
%26 = getelementptr inbounds [32 x i32], [32 x i32]* %12, i64 0, i64 0
%27 = call i32 @evbuffer_remove(%struct.evbuffer* %25, i32* %26, i32 32)
%28 = getelementptr inbounds [32 x i32], [32 x i32]* %12, i64 0, i64 0
store i32* %28, i32** %13
%29 = load %struct.evbuffer*, %struct.evbuffer** %10
%30 = load i32, i32* %11
%31 = sub nsw i32 %30, 32
%32 = call i32 @evbuffer_drain(%struct.evbuffer* %29, i32 %31)
%33 = load i32*, i32** %13
%34 = call %struct.worker* @find_worker(i32* %33)
store %struct.worker* %34, %struct.worker** %14
%35 = load %struct.worker*, %struct.worker** %14
%36 = icmp ne %struct.worker* %35, null
br i1 %36, label %41, label %37
37: ; preds = %24
%38 = load %struct.conn*, %struct.conn** %7
%39 = load i32*, i32** %8
%40 = call i32 @send_err(%struct.conn* %38, i32 6, i32* %39, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 0, i32* %6
br label %57
41: ; preds = %24
%42 = load %struct.worker*, %struct.worker** %14
%43 = call %struct.distribution* @worker_get_distribution(%struct.worker* %42)
store %struct.distribution* %43, %struct.distribution** %15
%44 = load %struct.distribution*, %struct.distribution** %15
%45 = call i64 @distribution_get_metadata(%struct.distribution* %44, i32** %16, i64* %17)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = load %struct.conn*, %struct.conn** %7
%49 = load i32*, i32** %8
%50 = call i32 @send_err(%struct.conn* %48, i32 9, i32* %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i32 0, i32* %6
br label %57
51: ; preds = %41
%52 = load %struct.conn*, %struct.conn** %7
%53 = load i32*, i32** %8
%54 = load i64, i64* %17
%55 = load i32*, i32** %16
%56 = call i32 @send_data(%struct.conn* %52, i32* %53, i32 8195, i64 %54, i32* %55)
store i32 0, i32* %6
br label %57
57: ; preds = %51, %47, %37, %20
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
%struct.conn = type { i32 }
%struct.evbuffer = type { i32 }
%struct.worker = type { i32 }
%struct.distribution = type { i32 }
@.str = [23 x i8] c"GET_METADATA too short\00"
@.str.1 = [21 x i8] c"No such distribution\00"
@.str.2 = [29 x i8] c"Distribution had no metadata\00"
define i32 @handle_get_metadata_command(%struct.conn* %0, i32* %1, i32 %2, %struct.evbuffer* %3, i32 %4) {
%6 = alloca [32 x i32]
%7 = alloca i32*
%8 = alloca i64
%9 = icmp slt i32 %4, 32
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @send_err(%struct.conn* %0, i32 2, i32* %1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 1)
br label %36
12: ; preds = %5
%13 = bitcast [32 x i32]* %6 to i8*
%14 = getelementptr inbounds [32 x i32], [32 x i32]* %6, i64 0, i64 0
%15 = call i32 @evbuffer_remove(%struct.evbuffer* %3, i32* %14, i32 32)
%16 = add nsw i32 %4, -32
%17 = call i32 @evbuffer_drain(%struct.evbuffer* %3, i32 %16)
%18 = call %struct.worker* @find_worker(i32* %14)
%19 = icmp eq %struct.worker* %18, null
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @send_err(%struct.conn* %0, i32 6, i32* %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %35
22: ; preds = %12
%23 = call %struct.distribution* @worker_get_distribution(%struct.worker* %18)
%24 = bitcast i32** %7 to i8*
%25 = bitcast i64* %8 to i8*
%26 = call i64 @distribution_get_metadata(%struct.distribution* %23, i32** %7, i64* %8)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @send_err(%struct.conn* %0, i32 9, i32* %1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %34
30: ; preds = %22
%31 = load i64, i64* %8
%32 = load i32*, i32** %7
%33 = call i32 @send_data(%struct.conn* %0, i32* %1, i32 8195, i64 %31, i32* %32)
br label %34
34: ; preds = %30, %28
br label %35
35: ; preds = %34, %20
br label %36
36: ; preds = %35, %10
%37 = phi i32 [ 1, %10 ], [ 0, %35 ]
ret i32 %37
}