#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 set_length(unsigned char *buffer, size_t length) {
  if (length < 0x80) {
    *buffer++ = length;
    return 1;
  } else if (length < 0xff) {
    *buffer++ = 0x81;
    *buffer++ = length;
    return 2;
  } else {
    *buffer++ = 0x82;
    *buffer++ = (length >> 8) & 0xff;
    *buffer++ = length & 0xff;
    return 3;
  }
}


x86 assembly (clang, -O0):
<set_length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x80,-0x18(%rbp)
jae    113d <set_length+0x3d>
mov    -0x18(%rbp),%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11d7 <set_length+0xd7>
cmpq   $0xff,-0x18(%rbp)
jae    1180 <set_length+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x81,(%rax)
mov    -0x18(%rbp),%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    11d7 <set_length+0xd7>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x82,(%rax)
mov    -0x18(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_length>:
mov    %rsi,%rcx
mov    $0x1,%eax
cmp    $0x80,%rsi
jb     1138 <set_length+0x38>
cmp    $0xfe,%rcx
ja     1129 <set_length+0x29>
movb   $0x81,(%rdi)
add    $0x1,%rdi
mov    $0x2,%eax
mov    %cl,(%rdi)
ret
movb   $0x82,(%rdi)
mov    %ch,0x1(%rdi)
add    $0x2,%rdi
mov    $0x3,%eax
mov    %cl,(%rdi)
ret

LLVM IR (-O0):
define i32 @set_length(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%6 = load i64, i64* %5
%7 = icmp ult i64 %6, 128
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i64, i64* %5
%10 = trunc i64 %9 to i8
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %4
store i8 %10, i8* %11
store i32 1, i32* %3
br label %37
13: ; preds = %2
%14 = load i64, i64* %5
%15 = icmp ult i64 %14, 255
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %4
store i8 -127, i8* %17
%19 = load i64, i64* %5
%20 = trunc i64 %19 to i8
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %4
store i8 %20, i8* %21
store i32 2, i32* %3
br label %37
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
store i8 -126, i8* %24
%26 = load i64, i64* %5
%27 = lshr i64 %26, 8
%28 = and i64 %27, 255
%29 = trunc i64 %28 to i8
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 %29, i8* %30
%32 = load i64, i64* %5
%33 = and i64 %32, 255
%34 = trunc i64 %33 to i8
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %4
store i8 %34, i8* %35
store i32 3, i32* %3
br label %37
37: ; preds = %23, %16, %8
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
define i32 @set_length(i8* nocapture writeonly %0, i64 %1) {
%3 = icmp ult i64 %1, 128
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = icmp ult i64 %1, 255
%6 = getelementptr inbounds i8, i8* %0, i64 1
br i1 %5, label %7, label %8
7: ; preds = %4
store i8 -127, i8* %0
br label %12
8: ; preds = %4
store i8 -126, i8* %0
%9 = lshr i64 %1, 8
%10 = trunc i64 %9 to i8
%11 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %10, i8* %6
br label %12
12: ; preds = %2, %8, %7
%13 = phi i8* [ %11, %8 ], [ %6, %7 ], [ %0, %2 ]
%14 = phi i32 [ 3, %8 ], [ 2, %7 ], [ 1, %2 ]
%15 = trunc i64 %1 to i8
store i8 %15, i8* %13
ret i32 %14
}