#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 */
typedef int uint8_t;

/* Variables and functions */
int fill_header(uint8_t *buf, int len, int id) {
  int sz;
  if (len < 128) {
    buf[0] = len;
    ++buf;
    sz = 1;
  } else {
    buf[0] = ((len & 0x7f00) >> 8) | 0x80;
    buf[1] = len & 0xff;
    buf += 2;
    sz = 2;
  }
  buf[0] = (id & 0xff00) >> 8;
  buf[1] = id & 0xff;
  return sz + 2;
}


x86 assembly (clang, -O0):
<fill_header>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x80,-0xc(%rbp)
jge    113c <fill_header+0x3c>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
jmp    1177 <fill_header+0x77>
mov    -0xc(%rbp),%ecx
and    $0x7f00,%ecx
sar    $0x8,%ecx
or     $0x80,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%ecx
and    $0xff,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
movl   $0x2,-0x14(%rbp)
mov    -0x10(%rbp),%ecx
and    $0xff00,%ecx
sar    $0x8,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
and    $0xff,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_header>:
cmp    $0x7f,%esi
jg     1113 <fill_header+0x13>
mov    %esi,(%rdi)
mov    $0x3,%eax
mov    $0x1,%ecx
jmp    1133 <fill_header+0x33>
mov    %esi,%eax
shr    $0x8,%eax
or     $0x80,%eax
movzbl %al,%eax
mov    %eax,(%rdi)
movzbl %sil,%eax
mov    %eax,0x4(%rdi)
mov    $0x4,%eax
mov    $0x2,%ecx
movzbl %dh,%esi
mov    %esi,(%rdi,%rcx,4)
movzbl %dl,%edx
mov    %edx,0x4(%rdi,%rcx,4)
ret

LLVM IR (-O0):
define i32 @fill_header(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 128
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load i32, i32* %5
%12 = load i32*, i32** %4
%13 = getelementptr inbounds i32, i32* %12, i64 0
store i32 %11, i32* %13
%14 = load i32*, i32** %4
%15 = getelementptr inbounds i32, i32* %14, i32 1
store i32* %15, i32** %4
store i32 1, i32* %7
br label %29
16: ; preds = %3
%17 = load i32, i32* %5
%18 = and i32 %17, 32512
%19 = ashr i32 %18, 8
%20 = or i32 %19, 128
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i64 0
store i32 %20, i32* %22
%23 = load i32, i32* %5
%24 = and i32 %23, 255
%25 = load i32*, i32** %4
%26 = getelementptr inbounds i32, i32* %25, i64 1
store i32 %24, i32* %26
%27 = load i32*, i32** %4
%28 = getelementptr inbounds i32, i32* %27, i64 2
store i32* %28, i32** %4
store i32 2, i32* %7
br label %29
29: ; preds = %16, %10
%30 = load i32, i32* %6
%31 = and i32 %30, 65280
%32 = ashr i32 %31, 8
%33 = load i32*, i32** %4
%34 = getelementptr inbounds i32, i32* %33, i64 0
store i32 %32, i32* %34
%35 = load i32, i32* %6
%36 = and i32 %35, 255
%37 = load i32*, i32** %4
%38 = getelementptr inbounds i32, i32* %37, i64 1
store i32 %36, i32* %38
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 2
ret i32 %40
}

LLVM IR (-O2):
define i32 @fill_header(i32* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %1, 128
br i1 %4, label %5, label %6
5: ; preds = %3
store i32 %1, i32* %0
br label %12
6: ; preds = %3
%7 = lshr i32 %1, 8
%8 = and i32 %7, 127
%9 = or i32 %8, 128
store i32 %9, i32* %0
%10 = and i32 %1, 255
%11 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %10, i32* %11
br label %12
12: ; preds = %6, %5
%13 = phi i64 [ 2, %6 ], [ 1, %5 ]
%14 = phi i32 [ 4, %6 ], [ 3, %5 ]
%15 = getelementptr inbounds i32, i32* %0, i64 %13
%16 = lshr i32 %2, 8
%17 = and i32 %16, 255
store i32 %17, i32* %15
%18 = and i32 %2, 255
%19 = getelementptr inbounds i32, i32* %15, i64 1
store i32 %18, i32* %19
ret i32 %14
}