#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 */

/* Variables and functions */
scalar_t__ calloc(int, int);
unsigned char *encode_integer(unsigned int value) {
  unsigned char *integer_string = NULL;
  if (value > 32767) {
    integer_string = (unsigned char *)calloc(6, sizeof(unsigned char));
    integer_string[0] = 0x02;
    integer_string[1] = 0x04;
    integer_string[2] = (unsigned char)(value >> 24);
    integer_string[3] = (unsigned char)(value >> 16);
    integer_string[4] = (unsigned char)(value >> 8);
    integer_string[5] = (unsigned char)(value);
  } else if (value > 127) {
    integer_string = (unsigned char *)calloc(4, sizeof(unsigned char));
    integer_string[0] = 0x02;
    integer_string[1] = 0x02;
    integer_string[2] = (unsigned char)(value >> 8);
    integer_string[3] = (unsigned char)(value);
  } else {
    integer_string = (unsigned char *)calloc(3, sizeof(unsigned char));
    integer_string[0] = 0x02;
    integer_string[1] = 0x01;
    integer_string[2] = (unsigned char)(value);
  }
  return integer_string;
}


x86 assembly (clang, -O0):
<encode_integer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
cmpl   $0x7fff,-0x4(%rbp)
jbe    1190 <encode_integer+0x80>
mov    $0x6,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x2,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x4,0x1(%rax)
mov    -0x4(%rbp),%eax
shr    $0x18,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x4(%rbp),%eax
shr    $0x10,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x4(%rbp),%eax
shr    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x4(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x5(%rax)
jmp    120f <encode_integer+0xff>
cmpl   $0x7f,-0x4(%rbp)
jbe    11dc <encode_integer+0xcc>
mov    $0x4,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x2,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x2,0x1(%rax)
mov    -0x4(%rbp),%eax
shr    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x4(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
jmp    120a <encode_integer+0xfa>
mov    $0x3,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x2,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x1,0x1(%rax)
mov    -0x4(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    120f <encode_integer+0xff>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_integer>:
push   %rbx
mov    %edi,%ebx
cmp    $0x8000,%edi
jb     1147 <encode_integer+0x37>
mov    $0x6,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
movw   $0x402,(%rax)
mov    %ebx,%ecx
shr    $0x18,%ecx
mov    %cl,0x2(%rax)
mov    %ebx,%ecx
shr    $0x10,%ecx
mov    %cl,0x3(%rax)
mov    %bh,0x4(%rax)
mov    %bl,0x5(%rax)
pop    %rbx
ret
cmp    $0x80,%ebx
jb     116b <encode_integer+0x5b>
mov    $0x4,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
movw   $0x202,(%rax)
mov    %bh,0x2(%rax)
mov    %bl,0x3(%rax)
pop    %rbx
ret
mov    $0x3,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
movw   $0x102,(%rax)
mov    %bl,0x2(%rax)
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @encode_integer(i32 %0) {
%2 = icmp ugt i32 %0, 32767
br i1 %2, label %3, label %18
3: ; preds = %1
%4 = tail call i64 @calloc(i32 6, i32 1)
%5 = inttoptr i64 %4 to i8*
store i8 2, i8* %5
%6 = getelementptr inbounds i8, i8* %5, i64 1
store i8 4, i8* %6
%7 = lshr i32 %0, 24
%8 = trunc i32 %7 to i8
%9 = getelementptr inbounds i8, i8* %5, i64 2
store i8 %8, i8* %9
%10 = lshr i32 %0, 16
%11 = trunc i32 %10 to i8
%12 = getelementptr inbounds i8, i8* %5, i64 3
store i8 %11, i8* %12
%13 = lshr i32 %0, 8
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds i8, i8* %5, i64 4
store i8 %14, i8* %15
%16 = trunc i32 %0 to i8
%17 = getelementptr inbounds i8, i8* %5, i64 5
store i8 %16, i8* %17
br label %35
18: ; preds = %1
%19 = icmp ugt i32 %0, 127
br i1 %19, label %20, label %29
20: ; preds = %18
%21 = tail call i64 @calloc(i32 4, i32 1)
%22 = inttoptr i64 %21 to i8*
store i8 2, i8* %22
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8 2, i8* %23
%24 = lshr i32 %0, 8
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds i8, i8* %22, i64 2
store i8 %25, i8* %26
%27 = trunc i32 %0 to i8
%28 = getelementptr inbounds i8, i8* %22, i64 3
store i8 %27, i8* %28
br label %35
29: ; preds = %18
%30 = tail call i64 @calloc(i32 3, i32 1)
%31 = inttoptr i64 %30 to i8*
store i8 2, i8* %31
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 1, i8* %32
%33 = trunc i32 %0 to i8
%34 = getelementptr inbounds i8, i8* %31, i64 2
store i8 %33, i8* %34
br label %35
35: ; preds = %20, %29, %3
%36 = phi i8* [ %5, %3 ], [ %22, %20 ], [ %31, %29 ]
ret i8* %36
}