#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
int htonl(int);
int htons(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int pack_size(uint32_t val, char *dst) {
  if (val <= 0x80) {
    dst[0] = (unsigned char)val;
    return sizeof(char);
  }
  if (val < ((uint16_t)~0)) {
    uint16_t vval = htons((uint16_t)val);
    dst[0] = (0x80 + sizeof(uint16_t));
    memcpy(dst + 1, (const char *)&vval, sizeof(uint16_t));
    return sizeof(uint16_t) + 1;
  }
  val = htonl(val);
  dst[0] = (0x80 + sizeof(uint32_t));
  memcpy(dst + 1, (const char *)&val, sizeof(uint32_t));
  return sizeof(uint32_t) + 1;
}


x86 assembly (clang, -O0):
<pack_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x80,-0x8(%rbp)
jg     1163 <pack_size+0x33>
mov    -0x8(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11d0 <pack_size+0xa0>
cmpl   $0xffffffff,-0x8(%rbp)
jge    11a1 <pack_size+0x71>
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x84,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movl   $0x5,-0x4(%rbp)
jmp    11d0 <pack_size+0xa0>
mov    -0x8(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x84,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movl   $0x5,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack_size>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x80,%edi
jg     113a <pack_size+0x1a>
mov    %dil,(%rbx)
mov    $0x1,%eax
jmp    1161 <pack_size+0x41>
call   1030 <htonl@plt>
mov    %eax,0xc(%rsp)
movb   $0x84,(%rbx)
add    $0x1,%rbx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    $0x5,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @pack_size(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = icmp sle i32 %7, 128
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* %4
%11 = trunc i32 %10 to i8
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 %11, i8* %13
store i32 1, i32* %3
br label %35
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, -1
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i32, i32* %4
%19 = call i32 @htons(i32 %18)
store i32 %19, i32* %6
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 -124, i8* %21
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = bitcast i32* %6 to i8*
%25 = call i32 @memcpy(i8* %23, i8* %24, i32 4)
store i32 5, i32* %3
br label %35
26: ; preds = %14
%27 = load i32, i32* %4
%28 = call i32 @htonl(i32 %27)
store i32 %28, i32* %4
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 -124, i8* %30
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = bitcast i32* %4 to i8*
%34 = call i32 @memcpy(i8* %32, i8* %33, i32 4)
store i32 5, i32* %3
br label %35
35: ; preds = %26, %17, %9
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @pack_size(i32 %0, i8* %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 129
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = trunc i32 %0 to i8
store i8 %6, i8* %1
br label %12
7: ; preds = %2
%8 = tail call i32 @htonl(i32 %0)
store i32 %8, i32* %3
store i8 -124, i8* %1
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = bitcast i32* %3 to i8*
%11 = call i32 @memcpy(i8* %9, i8* %10, i32 4)
br label %12
12: ; preds = %7, %5
%13 = phi i32 [ 1, %5 ], [ 5, %7 ]
ret i32 %13
}