#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 /*<<< orphan*/ OPENSSL_assert(int);
size_t der_encode_length(size_t len, unsigned char **pp) {
  size_t lenbytes;
  OPENSSL_assert(len < 0x8000);
  if (len > 255)
    lenbytes = 3;
  else if (len > 127)
    lenbytes = 2;
  else
    lenbytes = 1;
  if (pp != NULL) {
    if (lenbytes == 1) {
      *(*pp)++ = (unsigned char)len;
    } else {
      *(*pp)++ = (unsigned char)(lenbytes - 1);
      if (lenbytes == 2) {
        *(*pp)++ = (unsigned char)(0x80 | len);
      } else {
        *(*pp)++ = (unsigned char)(0x80 | (len >> 8));
        *(*pp)++ = (unsigned char)(len);
      }
    }
  }
  return lenbytes;
}


x86 assembly (clang, -O0):
<der_encode_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x8000,-0x8(%rbp)
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <OPENSSL_assert@plt>
cmpq   $0xff,-0x8(%rbp)
jbe    1150 <der_encode_length+0x40>
movq   $0x3,-0x18(%rbp)
jmp    1175 <der_encode_length+0x65>
cmpq   $0x7f,-0x8(%rbp)
jbe    1168 <der_encode_length+0x58>
movq   $0x2,-0x18(%rbp)
jmp    1170 <der_encode_length+0x60>
movq   $0x1,-0x18(%rbp)
jmp    1175 <der_encode_length+0x65>
cmpq   $0x0,-0x10(%rbp)
je     123b <der_encode_length+0x12b>
cmpq   $0x1,-0x18(%rbp)
jne    11a9 <der_encode_length+0x99>
mov    -0x8(%rbp),%rax
mov    %al,%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
jmp    1236 <der_encode_length+0x126>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
cmpq   $0x2,-0x18(%rbp)
jne    11f5 <der_encode_length+0xe5>
mov    -0x8(%rbp),%rax
or     $0x80,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
jmp    1231 <der_encode_length+0x121>
mov    -0x8(%rbp),%rax
shr    $0x8,%rax
or     $0x80,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    %al,%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
jmp    1236 <der_encode_length+0x126>
jmp    123b <der_encode_length+0x12b>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<der_encode_length>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
cmp    $0x8000,%rbx
setb   %dil
call   1030 <OPENSSL_assert@plt>
cmp    $0xff,%rbx
jbe    1170 <der_encode_length+0x60>
mov    $0x3,%eax
test   %r14,%r14
je     11bd <der_encode_length+0xad>
mov    (%r14),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%r14)
movb   $0x2,(%rcx)
mov    %rbx,%rcx
shr    $0x8,%rcx
or     $0x80,%cl
mov    (%r14),%rdx
lea    0x1(%rdx),%rsi
mov    %rsi,(%r14)
mov    %cl,(%rdx)
mov    (%r14),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%r14)
mov    %bl,(%rcx)
jmp    11bd <der_encode_length+0xad>
xor    %eax,%eax
cmp    $0x7f,%rbx
seta   %al
add    $0x1,%rax
test   %r14,%r14
je     11bd <der_encode_length+0xad>
cmp    $0x7f,%rbx
jbe    11ac <der_encode_length+0x9c>
add    $0xff,%al
mov    (%r14),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%r14)
mov    %al,(%rcx)
or     $0x80,%bl
mov    (%r14),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%r14)
mov    %bl,(%rax)
mov    $0x2,%eax
jmp    11bd <der_encode_length+0xad>
mov    (%r14),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%r14)
mov    %bl,(%rax)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @der_encode_length(i64 %0, i8** %1) {
%3 = icmp ult i64 %0, 32768
%4 = zext i1 %3 to i32
%5 = tail call i32 @OPENSSL_assert(i32 %4)
%6 = icmp ugt i64 %0, 255
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = icmp ugt i64 %0, 127
%9 = select i1 %8, i64 2, i64 1
%10 = icmp eq i8** %1, null
br i1 %10, label %38, label %24
11: ; preds = %2
%12 = icmp eq i8** %1, null
br i1 %12, label %38, label %13
13: ; preds = %11
%14 = load i8*, i8** %1
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8* %15, i8** %1
store i8 2, i8* %14
%16 = lshr i64 %0, 8
%17 = trunc i64 %16 to i8
%18 = or i8 %17, -128
%19 = load i8*, i8** %1
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8* %20, i8** %1
store i8 %18, i8* %19
%21 = trunc i64 %0 to i8
%22 = load i8*, i8** %1
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8* %23, i8** %1
store i8 %21, i8* %22
br label %38
24: ; preds = %7
br i1 %8, label %29, label %25
25: ; preds = %24
%26 = trunc i64 %0 to i8
%27 = load i8*, i8** %1
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8* %28, i8** %1
store i8 %26, i8* %27
br label %38
29: ; preds = %24
%30 = trunc i64 %9 to i8
%31 = add nsw i8 %30, -1
%32 = load i8*, i8** %1
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %1
store i8 %31, i8* %32
%34 = trunc i64 %0 to i8
%35 = or i8 %34, -128
%36 = load i8*, i8** %1
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8* %37, i8** %1
store i8 %35, i8* %36
br label %38
38: ; preds = %11, %25, %13, %29, %7
%39 = phi i64 [ 3, %11 ], [ 1, %25 ], [ 3, %13 ], [ 2, %29 ], [ %9, %7 ]
ret i64 %39
}