#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;

/* Variables and functions */
int /*<<< orphan*/ inttoarr(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          size_t const);
uint8_t *requestforge(const uint8_t msg_type, const uint8_t *keyid,
                      const uint8_t *data, const size_t data_len,
                      size_t *out_len) {
  uint8_t *request;
  uint32_t paksize;
  size_t offs = 0;
  paksize = 1 + data_len + (keyid ? 32 : 0);
  *out_len = paksize + (sizeof(uint32_t));
  if ((request = malloc(*out_len)) == NULL)
    return NULL;
  inttoarr(paksize, request);
  offs += (sizeof(uint32_t));
  request[offs++] = msg_type;
  if (keyid) {
    memcpy(request + offs, keyid, 32);
    offs += 32;
  }
  if (data)
    memcpy(request + offs, data, data_len);
  return request;
}


x86 assembly (clang, -O0):
<requestforge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x18(%rbp),%rsi
xor    %ecx,%ecx
mov    $0x20,%edx
cmp    $0x0,%rsi
cmovne %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rcx
add    $0x4,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11ac <requestforge+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    1240 <requestforge+0x110>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1050 <inttoarr@plt>
mov    -0x48(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x38(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x48(%rbp)
mov    %edx,(%rax,%rcx,4)
cmpq   $0x0,-0x18(%rbp)
je     1211 <requestforge+0xe1>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x20,%edx
call   1030 <memcpy@plt>
mov    -0x48(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1238 <requestforge+0x108>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<requestforge>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%r13d
xor    %eax,%eax
test   %rsi,%rsi
setne  %al
shl    $0x5,%rax
lea    (%rcx,%rax,1),%rbp
add    $0x1,%rbp
movslq %ebp,%rdi
add    $0x4,%rdi
mov    %rdi,(%r8)
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11af <requestforge+0x7f>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <inttoarr@plt>
mov    %r13d,0x10(%rbx)
test   %r12,%r12
je     11c1 <requestforge+0x91>
lea    0x14(%rbx),%rdi
mov    $0x20,%edx
mov    %r12,%rsi
call   1030 <memcpy@plt>
mov    $0x25,%eax
test   %r14,%r14
je     11af <requestforge+0x7f>
lea    (%rbx,%rax,4),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x5,%eax
test   %r14,%r14
jne    11a0 <requestforge+0x70>
jmp    11af <requestforge+0x7f>

LLVM IR (-O0):
define i32* @requestforge(i32 %0, i32* %1, i32* %2, i64 %3, i64* %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i64 %3, i64* %10
store i64* %4, i64** %11
store i64 0, i64* %14
%15 = load i64, i64* %10
%16 = add i64 1, %15
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 32, i32 0
%21 = sext i32 %20 to i64
%22 = add i64 %16, %21
%23 = trunc i64 %22 to i32
store i32 %23, i32* %13
%24 = load i32, i32* %13
%25 = sext i32 %24 to i64
%26 = add i64 %25, 4
%27 = load i64*, i64** %11
store i64 %26, i64* %27
%28 = load i64*, i64** %11
%29 = load i64, i64* %28
%30 = call i32* @malloc(i64 %29)
store i32* %30, i32** %12
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %5
store i32* null, i32** %6
br label %66
33: ; preds = %5
%34 = load i32, i32* %13
%35 = load i32*, i32** %12
%36 = call i32 @inttoarr(i32 %34, i32* %35)
%37 = load i64, i64* %14
%38 = add i64 %37, 4
store i64 %38, i64* %14
%39 = load i32, i32* %7
%40 = load i32*, i32** %12
%41 = load i64, i64* %14
%42 = add i64 %41, 1
store i64 %42, i64* %14
%43 = getelementptr inbounds i32, i32* %40, i64 %41
store i32 %39, i32* %43
%44 = load i32*, i32** %8
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %54
46: ; preds = %33
%47 = load i32*, i32** %12
%48 = load i64, i64* %14
%49 = getelementptr inbounds i32, i32* %47, i64 %48
%50 = load i32*, i32** %8
%51 = call i32 @memcpy(i32* %49, i32* %50, i64 32)
%52 = load i64, i64* %14
%53 = add i64 %52, 32
store i64 %53, i64* %14
br label %54
54: ; preds = %46, %33
%55 = load i32*, i32** %9
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = load i32*, i32** %12
%59 = load i64, i64* %14
%60 = getelementptr inbounds i32, i32* %58, i64 %59
%61 = load i32*, i32** %9
%62 = load i64, i64* %10
%63 = call i32 @memcpy(i32* %60, i32* %61, i64 %62)
br label %64
64: ; preds = %57, %54
%65 = load i32*, i32** %12
store i32* %65, i32** %6
br label %66
66: ; preds = %64, %32
%67 = load i32*, i32** %6
ret i32* %67
}

LLVM IR (-O2):
define i32* @requestforge(i32 %0, i32* %1, i32* %2, i64 %3, i64* nocapture writeonly %4) {
%6 = add i64 %3, 1
%7 = icmp eq i32* %1, null
%8 = select i1 %7, i64 0, i64 32
%9 = add i64 %6, %8
%10 = shl i64 %9, 32
%11 = ashr exact i64 %10, 32
%12 = add nsw i64 %11, 4
store i64 %12, i64* %4
%13 = tail call i32* @malloc(i64 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %28, label %15
15: ; preds = %5
%16 = trunc i64 %9 to i32
%17 = tail call i32 @inttoarr(i32 %16, i32* %13)
%18 = getelementptr inbounds i32, i32* %13, i64 4
store i32 %0, i32* %18
br i1 %7, label %22, label %19
19: ; preds = %15
%20 = getelementptr inbounds i32, i32* %13, i64 5
%21 = tail call i32 @memcpy(i32* %20, i32* %1, i64 32)
br label %22
22: ; preds = %19, %15
%23 = phi i64 [ 37, %19 ], [ 5, %15 ]
%24 = icmp eq i32* %2, null
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = getelementptr inbounds i32, i32* %13, i64 %23
%27 = tail call i32 @memcpy(i32* %26, i32* %2, i64 %3)
br label %28
28: ; preds = %22, %25, %5
ret i32* %13
}