unsigned char *set_object(int object_id, unsigned char *buffer) {
  *buffer++ = 0x5c;
  if (object_id == 0x7e) {
    *buffer++ = 1;
    *buffer++ = 0x7e;
  } else if (object_id > 0xffff && object_id <= 0xffffff) {
    *buffer++ = 3;
    *buffer++ = (object_id >> 16) & 0xff;
    *buffer++ = (object_id >> 8) & 0xff;
    *buffer++ = object_id & 0xff;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<set_object>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%rax)
cmpl   $0x7e,-0x4(%rbp)
jne    1150 <set_object+0x50>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x1,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x7e,(%rax)
jmp    11d8 <set_object+0xd8>
cmpl   $0xffff,-0x4(%rbp)
jle    11d3 <set_object+0xd3>
cmpl   $0xffffff,-0x4(%rbp)
jg     11d3 <set_object+0xd3>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x3,(%rax)
mov    -0x4(%rbp),%eax
sar    $0x10,%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    -0x4(%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    -0x4(%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    11d8 <set_object+0xd8>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_object>:
mov    %rsi,%rax
movb   $0x5c,(%rsi)
cmp    $0x7e,%edi
jne    1116 <set_object+0x16>
movw   $0x7e01,0x1(%rax)
add    $0x3,%rax
ret
mov    %edi,%ecx
lea    -0x10000(%rcx),%edx
cmp    $0xfeffff,%edx
ja     113d <set_object+0x3d>
movb   $0x3,0x1(%rax)
mov    %ecx,%edx
shr    $0x10,%edx
mov    %dl,0x2(%rax)
mov    %ch,0x3(%rax)
mov    %cl,0x4(%rax)
add    $0x5,%rax
ret
add    $0x1,%rax
ret

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

LLVM IR (-O2):
define i8* @set_object(i32 %0, i8* writeonly %1) {
%3 = getelementptr inbounds i8, i8* %1, i64 1
store i8 92, i8* %1
%4 = icmp eq i32 %0, 126
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %1, i64 2
store i8 1, i8* %3
%7 = getelementptr inbounds i8, i8* %1, i64 3
store i8 126, i8* %6
br label %21
8: ; preds = %2
%9 = add i32 %0, -65536
%10 = icmp ult i32 %9, 16711680
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %1, i64 2
store i8 3, i8* %3
%13 = lshr i32 %0, 16
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %14, i8* %12
%16 = lshr i32 %0, 8
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %17, i8* %15
%19 = trunc i32 %0 to i8
%20 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %19, i8* %18
br label %21
21: ; preds = %8, %11, %5
%22 = phi i8* [ %7, %5 ], [ %20, %11 ], [ %3, %8 ]
ret i8* %22
}