int encode_snmp_sequence_header(unsigned char *buffer, int length, int type) {
  if (length > 0xFFFF) {
    return -1;
  }
  *buffer++ = type;
  if (length > 0xFF) {
    *buffer++ = 0x82;
    *buffer++ = (length >> 8) & 0xFF;
    *buffer++ = length & 0xFF;
  } else if (length > 0x7F) {
    *buffer++ = 0x81;
    *buffer++ = length & 0xFF;
  } else {
    *buffer++ = length & 0x7F;
  }
  return 0;
}


x86 assembly (clang, -O0):
<encode_snmp_sequence_header>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0xffff,-0x14(%rbp)
jle    1127 <encode_snmp_sequence_header+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <encode_snmp_sequence_header+0xfb>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
cmpl   $0xff,-0x14(%rbp)
jle    119a <encode_snmp_sequence_header+0x9a>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x82,(%rax)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11f4 <encode_snmp_sequence_header+0xf4>
cmpl   $0x7f,-0x14(%rbp)
jle    11d6 <encode_snmp_sequence_header+0xd6>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x81,(%rax)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11ef <encode_snmp_sequence_header+0xef>
mov    -0x14(%rbp),%eax
and    $0x7f,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11f4 <encode_snmp_sequence_header+0xf4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_snmp_sequence_header>:
mov    $0xffffffff,%eax
cmp    $0xffff,%esi
jg     113e <encode_snmp_sequence_header+0x3e>
mov    %esi,%ecx
mov    %dl,(%rdi)
cmp    $0x100,%esi
jl     1125 <encode_snmp_sequence_header+0x25>
movb   $0x82,0x1(%rdi)
mov    %ch,0x2(%rdi)
mov    %cl,0x3(%rdi)
jmp    113c <encode_snmp_sequence_header+0x3c>
cmp    $0x80,%ecx
jl     1136 <encode_snmp_sequence_header+0x36>
movb   $0x81,0x1(%rdi)
mov    %cl,0x2(%rdi)
jmp    113c <encode_snmp_sequence_header+0x3c>
and    $0x7f,%cl
mov    %cl,0x1(%rdi)
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @encode_snmp_sequence_header(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32, i32* %6
%9 = icmp sgt i32 %8, 65535
br i1 %9, label %10, label %11
10: ; preds = %3
store i32 -1, i32* %4
br label %51
11: ; preds = %3
%12 = load i32, i32* %7
%13 = trunc i32 %12 to i8
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %5
store i8 %13, i8* %14
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 255
br i1 %17, label %18, label %32
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %5
store i8 -126, i8* %19
%21 = load i32, i32* %6
%22 = ashr i32 %21, 8
%23 = and i32 %22, 255
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %5
store i8 %24, i8* %25
%27 = load i32, i32* %6
%28 = and i32 %27, 255
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
store i8 %29, i8* %30
br label %50
32: ; preds = %11
%33 = load i32, i32* %6
%34 = icmp sgt i32 %33, 127
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
store i8 -127, i8* %36
%38 = load i32, i32* %6
%39 = and i32 %38, 255
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
store i8 %40, i8* %41
br label %49
43: ; preds = %32
%44 = load i32, i32* %6
%45 = and i32 %44, 127
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %5
store i8 %46, i8* %47
br label %49
49: ; preds = %43, %35
br label %50
50: ; preds = %49, %18
store i32 0, i32* %4
br label %51
51: ; preds = %50, %10
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
define i32 @encode_snmp_sequence_header(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, 65535
br i1 %4, label %23, label %5
5: ; preds = %3
%6 = trunc i32 %2 to i8
%7 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %6, i8* %0
%8 = icmp sgt i32 %1, 255
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %0, i64 2
store i8 -126, i8* %7
%11 = lshr i32 %1, 8
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %12, i8* %10
%14 = trunc i32 %1 to i8
store i8 %14, i8* %13
br label %23
15: ; preds = %5
%16 = icmp sgt i32 %1, 127
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = getelementptr inbounds i8, i8* %0, i64 2
store i8 -127, i8* %7
%19 = trunc i32 %1 to i8
store i8 %19, i8* %18
br label %23
20: ; preds = %15
%21 = trunc i32 %1 to i8
%22 = and i8 %21, 127
store i8 %22, i8* %7
br label %23
23: ; preds = %9, %20, %17, %3
%24 = phi i32 [ -1, %3 ], [ 0, %17 ], [ 0, %20 ], [ 0, %9 ]
ret i32 %24
}