#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_by_length(unsigned int value,
                                        unsigned char length,
                                        unsigned char typ) {
  unsigned char *integer_string = NULL;
  if (length > 2) {
    integer_string = (unsigned char *)calloc(6, sizeof(unsigned char));
    integer_string[0] = typ;
    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 > 1) {
    integer_string = (unsigned char *)calloc(4, sizeof(unsigned char));
    integer_string[0] = typ;
    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] = typ;
    integer_string[1] = 0x01;
    integer_string[2] = (unsigned char)(value);
  }
  return integer_string;
}


x86 assembly (clang, -O0):
<encode_integer_by_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %edi,-0x4(%rbp)
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
movq   $0x0,-0x10(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0x2,%eax
jle    119d <encode_integer_by_length+0x8d>
mov    $0x6,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x6(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%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    1220 <encode_integer_by_length+0x110>
cmpl   $0x1,-0x4(%rbp)
jbe    11eb <encode_integer_by_length+0xdb>
mov    $0x4,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x6(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%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    121b <encode_integer_by_length+0x10b>
mov    $0x3,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x6(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%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    1220 <encode_integer_by_length+0x110>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_integer_by_length>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %edi,%ebx
cmp    $0x3,%sil
jb     114b <encode_integer_by_length+0x3b>
mov    $0x6,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %bpl,(%rax)
movb   $0x4,0x1(%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)
jmp    1187 <encode_integer_by_length+0x77>
cmp    $0x2,%ebx
jb     116e <encode_integer_by_length+0x5e>
mov    $0x4,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %bpl,(%rax)
movb   $0x2,0x1(%rax)
mov    %bh,0x2(%rax)
mov    %bl,0x3(%rax)
jmp    1187 <encode_integer_by_length+0x77>
mov    $0x3,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %bpl,(%rax)
movb   $0x1,0x1(%rax)
mov    %bl,0x2(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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