#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 */
int EINVAL;
int asn1_header_encode(unsigned char **buf, unsigned int cls, unsigned int con,
                       unsigned int tag, unsigned int *len) {
  unsigned char *loc;
  unsigned int r_len;
  if (*len < 2)
    return -EINVAL;
  loc = *buf;
  r_len = *len;
  *loc++ = ((cls & 0x3) << 6) | ((con & 0x1) << 5) | (tag & 0x1F);
  r_len -= 1;
  if (r_len - 1 < (1 << 7)) {
    r_len -= 1;
    *loc++ = (unsigned char)(r_len & 0x7F);
  } else if (r_len - 2 < (1 << 8)) {
    r_len -= 2;
    *loc++ = 0x81;
    *loc++ = (unsigned char)(r_len & 0xFF);
  } else if (r_len - 3 < (1 << 16)) {
    r_len -= 3;
    *loc++ = 0x82;
    *loc++ = (unsigned char)((r_len >> 8) & 0xFF);
    *loc++ = (unsigned char)(r_len & 0xFF);
  } else if (r_len - 4 < (1 << 24)) {
    r_len -= 4;
    *loc++ = 0x83;
    *loc++ = (unsigned char)((r_len >> 16) & 0xFF);
    *loc++ = (unsigned char)((r_len >> 8) & 0xFF);
    *loc++ = (unsigned char)(r_len & 0xFF);
  } else {
    r_len -= 5;
    *loc++ = 0x84;
    *loc++ = (unsigned char)((r_len >> 24) & 0xFF);
    *loc++ = (unsigned char)((r_len >> 16) & 0xFF);
    *loc++ = (unsigned char)((r_len >> 8) & 0xFF);
    *loc++ = (unsigned char)(r_len & 0xFF);
  }
  *buf = loc;
  *len = r_len;
  return 0;
}


x86 assembly (clang, -O0):
<asn1_header_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x2,(%rax)
jae    1135 <asn1_header_encode+0x35>
xor    %eax,%eax
mov    0x2eb5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13b1 <asn1_header_encode+0x2b1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
and    $0x3,%eax
shl    $0x6,%eax
mov    -0x18(%rbp),%ecx
and    $0x1,%ecx
shl    $0x5,%ecx
or     %ecx,%eax
mov    -0x1c(%rbp),%ecx
and    $0x1f,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cmp    $0x80,%eax
jae    11b9 <asn1_header_encode+0xb9>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
and    $0x7f,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    1396 <asn1_header_encode+0x296>
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
cmp    $0x100,%eax
jae    1205 <asn1_header_encode+0x105>
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x81,(%rax)
mov    -0x34(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    1391 <asn1_header_encode+0x291>
mov    -0x34(%rbp),%eax
sub    $0x3,%eax
cmp    $0x10000,%eax
jae    126f <asn1_header_encode+0x16f>
mov    -0x34(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x82,(%rax)
mov    -0x34(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    138c <asn1_header_encode+0x28c>
mov    -0x34(%rbp),%eax
sub    $0x4,%eax
cmp    $0x1000000,%eax
jae    12f7 <asn1_header_encode+0x1f7>
mov    -0x34(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x83,(%rax)
mov    -0x34(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    1387 <asn1_header_encode+0x287>
mov    -0x34(%rbp),%eax
sub    $0x5,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x84,(%rax)
mov    -0x34(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    138c <asn1_header_encode+0x28c>
jmp    1391 <asn1_header_encode+0x291>
jmp    1396 <asn1_header_encode+0x296>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<asn1_header_encode>:
push   %rbx
mov    (%r8),%ebx
cmp    $0x1,%ebx
ja     1116 <asn1_header_encode+0x16>
mov    0x2ed0(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret
mov    (%rdi),%rax
shl    $0x6,%esi
shl    $0x5,%edx
and    $0x20,%edx
or     %esi,%edx
and    $0x1f,%ecx
or     %edx,%ecx
mov    %cl,(%rax)
lea    -0x2(%rbx),%ecx
cmp    $0x80,%ecx
jae    1144 <asn1_header_encode+0x44>
lea    0x1(%rax),%rdx
mov    $0x2,%esi
jmp    11c5 <asn1_header_encode+0xc5>
lea    -0x3(%rbx),%ecx
cmp    $0xff,%ecx
ja     115e <asn1_header_encode+0x5e>
lea    0x2(%rax),%rdx
movb   $0x81,0x1(%rax)
mov    $0x3,%esi
jmp    11c5 <asn1_header_encode+0xc5>
lea    -0x4(%rbx),%ecx
cmp    $0xffff,%ecx
ja     117b <asn1_header_encode+0x7b>
movb   $0x82,0x1(%rax)
lea    0x3(%rax),%rdx
mov    %ch,0x2(%rax)
mov    $0x4,%esi
jmp    11c5 <asn1_header_encode+0xc5>
lea    -0x5(%rbx),%ecx
cmp    $0xffffff,%ecx
ja     11a0 <asn1_header_encode+0xa0>
movb   $0x83,0x1(%rax)
mov    %ecx,%edx
shr    $0x10,%edx
mov    %dl,0x2(%rax)
lea    0x4(%rax),%rdx
mov    %ch,0x3(%rax)
mov    $0x5,%esi
jmp    11c5 <asn1_header_encode+0xc5>
add    $0xfffffffa,%ebx
movb   $0x84,0x1(%rax)
mov    %ebx,%ecx
shr    $0x18,%ecx
mov    %cl,0x2(%rax)
mov    %ebx,%ecx
shr    $0x10,%ecx
mov    %cl,0x3(%rax)
lea    0x5(%rax),%rdx
mov    %bh,0x4(%rax)
mov    $0x6,%esi
mov    %ebx,%ecx
add    %rsi,%rax
mov    %cl,(%rdx)
mov    %rax,(%rdi)
mov    %ecx,(%r8)
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
define i32 @asn1_header_encode(i8** %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
store i8** %0, i8*** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%14 = load i32*, i32** %11
%15 = load i32, i32* %14
%16 = icmp ult i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load i32, i32* @EINVAL
%19 = sub nsw i32 0, %18
store i32 %19, i32* %6
br label %147
20: ; preds = %5
%21 = load i8**, i8*** %7
%22 = load i8*, i8** %21
store i8* %22, i8** %12
%23 = load i32*, i32** %11
%24 = load i32, i32* %23
store i32 %24, i32* %13
%25 = load i32, i32* %8
%26 = and i32 %25, 3
%27 = shl i32 %26, 6
%28 = load i32, i32* %9
%29 = and i32 %28, 1
%30 = shl i32 %29, 5
%31 = or i32 %27, %30
%32 = load i32, i32* %10
%33 = and i32 %32, 31
%34 = or i32 %31, %33
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %12
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %12
store i8 %35, i8* %36
%38 = load i32, i32* %13
%39 = sub i32 %38, 1
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = sub i32 %40, 1
%42 = icmp ult i32 %41, 128
br i1 %42, label %43, label %51
43: ; preds = %20
%44 = load i32, i32* %13
%45 = sub i32 %44, 1
store i32 %45, i32* %13
%46 = load i32, i32* %13
%47 = and i32 %46, 127
%48 = trunc i32 %47 to i8
%49 = load i8*, i8** %12
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %12
store i8 %48, i8* %49
br label %142
51: ; preds = %20
%52 = load i32, i32* %13
%53 = sub i32 %52, 2
%54 = icmp ult i32 %53, 256
br i1 %54, label %55, label %65
55: ; preds = %51
%56 = load i32, i32* %13
%57 = sub i32 %56, 2
store i32 %57, i32* %13
%58 = load i8*, i8** %12
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %12
store i8 -127, i8* %58
%60 = load i32, i32* %13
%61 = and i32 %60, 255
%62 = trunc i32 %61 to i8
%63 = load i8*, i8** %12
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %12
store i8 %62, i8* %63
br label %141
65: ; preds = %51
%66 = load i32, i32* %13
%67 = sub i32 %66, 3
%68 = icmp ult i32 %67, 65536
br i1 %68, label %69, label %85
69: ; preds = %65
%70 = load i32, i32* %13
%71 = sub i32 %70, 3
store i32 %71, i32* %13
%72 = load i8*, i8** %12
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %12
store i8 -126, i8* %72
%74 = load i32, i32* %13
%75 = lshr i32 %74, 8
%76 = and i32 %75, 255
%77 = trunc i32 %76 to i8
%78 = load i8*, i8** %12
%79 = getelementptr inbounds i8, i8* %78, i32 1
store i8* %79, i8** %12
store i8 %77, i8* %78
%80 = load i32, i32* %13
%81 = and i32 %80, 255
%82 = trunc i32 %81 to i8
%83 = load i8*, i8** %12
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %12
store i8 %82, i8* %83
br label %140
85: ; preds = %65
%86 = load i32, i32* %13
%87 = sub i32 %86, 4
%88 = icmp ult i32 %87, 16777216
br i1 %88, label %89, label %111
89: ; preds = %85
%90 = load i32, i32* %13
%91 = sub i32 %90, 4
store i32 %91, i32* %13
%92 = load i8*, i8** %12
%93 = getelementptr inbounds i8, i8* %92, i32 1
store i8* %93, i8** %12
store i8 -125, i8* %92
%94 = load i32, i32* %13
%95 = lshr i32 %94, 16
%96 = and i32 %95, 255
%97 = trunc i32 %96 to i8
%98 = load i8*, i8** %12
%99 = getelementptr inbounds i8, i8* %98, i32 1
store i8* %99, i8** %12
store i8 %97, i8* %98
%100 = load i32, i32* %13
%101 = lshr i32 %100, 8
%102 = and i32 %101, 255
%103 = trunc i32 %102 to i8
%104 = load i8*, i8** %12
%105 = getelementptr inbounds i8, i8* %104, i32 1
store i8* %105, i8** %12
store i8 %103, i8* %104
%106 = load i32, i32* %13
%107 = and i32 %106, 255
%108 = trunc i32 %107 to i8
%109 = load i8*, i8** %12
%110 = getelementptr inbounds i8, i8* %109, i32 1
store i8* %110, i8** %12
store i8 %108, i8* %109
br label %139
111: ; preds = %85
%112 = load i32, i32* %13
%113 = sub i32 %112, 5
store i32 %113, i32* %13
%114 = load i8*, i8** %12
%115 = getelementptr inbounds i8, i8* %114, i32 1
store i8* %115, i8** %12
store i8 -124, i8* %114
%116 = load i32, i32* %13
%117 = lshr i32 %116, 24
%118 = and i32 %117, 255
%119 = trunc i32 %118 to i8
%120 = load i8*, i8** %12
%121 = getelementptr inbounds i8, i8* %120, i32 1
store i8* %121, i8** %12
store i8 %119, i8* %120
%122 = load i32, i32* %13
%123 = lshr i32 %122, 16
%124 = and i32 %123, 255
%125 = trunc i32 %124 to i8
%126 = load i8*, i8** %12
%127 = getelementptr inbounds i8, i8* %126, i32 1
store i8* %127, i8** %12
store i8 %125, i8* %126
%128 = load i32, i32* %13
%129 = lshr i32 %128, 8
%130 = and i32 %129, 255
%131 = trunc i32 %130 to i8
%132 = load i8*, i8** %12
%133 = getelementptr inbounds i8, i8* %132, i32 1
store i8* %133, i8** %12
store i8 %131, i8* %132
%134 = load i32, i32* %13
%135 = and i32 %134, 255
%136 = trunc i32 %135 to i8
%137 = load i8*, i8** %12
%138 = getelementptr inbounds i8, i8* %137, i32 1
store i8* %138, i8** %12
store i8 %136, i8* %137
br label %139
139: ; preds = %111, %89
br label %140
140: ; preds = %139, %69
br label %141
141: ; preds = %140, %55
br label %142
142: ; preds = %141, %43
%143 = load i8*, i8** %12
%144 = load i8**, i8*** %7
store i8* %143, i8** %144
%145 = load i32, i32* %13
%146 = load i32*, i32** %11
store i32 %145, i32* %146
store i32 0, i32* %6
br label %147
147: ; preds = %142, %17
%148 = load i32, i32* %6
ret i32 %148
}

LLVM IR (-O2):
@EINVAL = global i32 0
define i32 @asn1_header_encode(i8** nocapture %0, i32 %1, i32 %2, i32 %3, i32* nocapture %4) {
%6 = load i32, i32* %4
%7 = icmp ult i32 %6, 2
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %65
11: ; preds = %5
%12 = load i8*, i8** %0
%13 = shl i32 %1, 6
%14 = shl i32 %2, 5
%15 = and i32 %14, 32
%16 = or i32 %15, %13
%17 = and i32 %3, 31
%18 = or i32 %16, %17
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %19, i8* %12
%21 = add i32 %6, -2
%22 = icmp ult i32 %21, 128
br i1 %22, label %59, label %23
23: ; preds = %11
%24 = add i32 %6, -3
%25 = icmp ult i32 %24, 256
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = getelementptr inbounds i8, i8* %12, i64 2
store i8 -127, i8* %20
br label %59
28: ; preds = %23
%29 = add i32 %6, -4
%30 = icmp ult i32 %29, 65536
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = getelementptr inbounds i8, i8* %12, i64 2
store i8 -126, i8* %20
%33 = lshr i32 %29, 8
%34 = trunc i32 %33 to i8
%35 = getelementptr inbounds i8, i8* %12, i64 3
store i8 %34, i8* %32
br label %59
36: ; preds = %28
%37 = add i32 %6, -5
%38 = icmp ult i32 %37, 16777216
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = getelementptr inbounds i8, i8* %12, i64 2
store i8 -125, i8* %20
%41 = lshr i32 %37, 16
%42 = trunc i32 %41 to i8
%43 = getelementptr inbounds i8, i8* %12, i64 3
store i8 %42, i8* %40
%44 = lshr i32 %37, 8
%45 = trunc i32 %44 to i8
%46 = getelementptr inbounds i8, i8* %12, i64 4
store i8 %45, i8* %43
br label %59
47: ; preds = %36
%48 = add i32 %6, -6
%49 = getelementptr inbounds i8, i8* %12, i64 2
store i8 -124, i8* %20
%50 = lshr i32 %48, 24
%51 = trunc i32 %50 to i8
%52 = getelementptr inbounds i8, i8* %12, i64 3
store i8 %51, i8* %49
%53 = lshr i32 %48, 16
%54 = trunc i32 %53 to i8
%55 = getelementptr inbounds i8, i8* %12, i64 4
store i8 %54, i8* %52
%56 = lshr i32 %48, 8
%57 = trunc i32 %56 to i8
%58 = getelementptr inbounds i8, i8* %12, i64 5
store i8 %57, i8* %55
br label %59
59: ; preds = %11, %26, %39, %47, %31
%60 = phi i32 [ %24, %26 ], [ %37, %39 ], [ %48, %47 ], [ %29, %31 ], [ %21, %11 ]
%61 = phi i64 [ 3, %26 ], [ 5, %39 ], [ 6, %47 ], [ 4, %31 ], [ 2, %11 ]
%62 = phi i8* [ %27, %26 ], [ %46, %39 ], [ %58, %47 ], [ %35, %31 ], [ %20, %11 ]
%63 = trunc i32 %60 to i8
%64 = getelementptr inbounds i8, i8* %12, i64 %61
store i8 %63, i8* %62
store i8* %64, i8** %0
store i32 %60, i32* %4
br label %65
65: ; preds = %59, %8
%66 = phi i32 [ %10, %8 ], [ 0, %59 ]
ret i32 %66
}