#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 ASNDerCalcNumLengthBytes(long);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int ASNDerWriteLength(unsigned char *pbData, long nLength) {
  int nNumBytesRequired = ASNDerCalcNumLengthBytes(nLength);
  int nNumLengthBytes = nNumBytesRequired - 1;
  if (nNumBytesRequired > 1) {
    *pbData = (unsigned char)(0x80 | nNumLengthBytes);
    pbData++;
    memcpy(pbData, ((unsigned char *)&nLength) + (4 - nNumLengthBytes),
           nNumLengthBytes);
  } else {
    *pbData = (unsigned char)nLength;
  }
  return nNumBytesRequired;
}


x86 assembly (clang, -O0):
<ASNDerWriteLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ASNDerCalcNumLengthBytes@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    118d <ASNDerWriteLength+0x6d>
mov    -0x18(%rbp),%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x4,%eax
sub    -0x18(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <memcpy@plt>
jmp    1199 <ASNDerWriteLength+0x79>
mov    -0x10(%rbp),%rax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ASNDerWriteLength>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,0x8(%rsp)
mov    %rsi,%rdi
call   1040 <ASNDerCalcNumLengthBytes@plt>
mov    %eax,%r15d
cmp    $0x2,%eax
jl     116f <ASNDerWriteLength+0x4f>
lea    -0x1(%r15),%edx
mov    %edx,%eax
or     $0x80,%al
mov    %al,(%r14)
add    $0x1,%r14
mov    $0x5,%eax
sub    %r15d,%eax
cltq
lea    (%rsp,%rax,1),%rsi
add    $0x8,%rsi
mov    %r14,%rdi
call   1030 <memcpy@plt>
jmp    1172 <ASNDerWriteLength+0x52>
mov    %bl,(%r14)
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @ASNDerWriteLength(i8* %0, i64 %1) {
%3 = alloca i64
store i64 %1, i64* %3
%4 = tail call i32 @ASNDerCalcNumLengthBytes(i64 %1)
%5 = add nsw i32 %4, -1
%6 = icmp sgt i32 %4, 1
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = trunc i32 %5 to i8
%9 = or i8 %8, -128
store i8 %9, i8* %0
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = bitcast i64* %3 to i8*
%12 = sub nsw i32 5, %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = call i32 @memcpy(i8* %10, i8* %14, i32 %5)
br label %18
16: ; preds = %2
%17 = trunc i64 %1 to i8
store i8 %17, i8* %0
br label %18
18: ; preds = %16, %7
ret i32 %4
}