void BuildPadBufferHeader(char *pOutBuf, int nLen) {
  int len = nLen;
  if (len < 6) {
    *pOutBuf++ = 0xff;
    *pOutBuf++ = 0xff;
    *pOutBuf++ = 0xff;
    *pOutBuf++ = 0xff;
    *pOutBuf++ = 0xff;
    *pOutBuf++ = 0xff;
    return;
  }
  len -= 6;
  *pOutBuf++ = 0x00;
  *pOutBuf++ = 0x00;
  *pOutBuf++ = 0x01;
  *pOutBuf++ = 0xBE;
  *pOutBuf++ = len >> 8;
  *pOutBuf++ = len & 0xff;
}


x86 assembly (clang, -O0):
<BuildPadBufferHeader>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x6,-0x10(%rbp)
jge    118c <BuildPadBufferHeader+0x8c>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xff,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xff,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xff,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xff,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xff,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xff,(%rax)
jmp    1211 <BuildPadBufferHeader+0x111>
mov    -0x10(%rbp),%eax
sub    $0x6,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x1,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0xbe,(%rax)
mov    -0x10(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<BuildPadBufferHeader>:
cmp    $0x5,%esi
jg     1116 <BuildPadBufferHeader+0x16>
movb   $0xff,(%rdi)
mov    $0xff,%dl
mov    $0xff,%cl
mov    $0xff,%r8b
mov    $0xff,%al
mov    $0xff,%sil
jmp    1128 <BuildPadBufferHeader+0x28>
add    $0xfffffffa,%esi
movb   $0x0,(%rdi)
mov    %esi,%eax
shr    $0x8,%eax
mov    $0xbe,%r8b
mov    $0x1,%cl
xor    %edx,%edx
mov    %dl,0x1(%rdi)
mov    %cl,0x2(%rdi)
mov    %r8b,0x3(%rdi)
mov    %al,0x4(%rdi)
mov    %sil,0x5(%rdi)
ret

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

LLVM IR (-O2):
define void @BuildPadBufferHeader(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp slt i32 %1, 6
br i1 %3, label %4, label %5
4: ; preds = %2
store i8 -1, i8* %0
br label %10
5: ; preds = %2
%6 = add nsw i32 %1, -6
store i8 0, i8* %0
%7 = lshr i32 %6, 8
%8 = trunc i32 %7 to i8
%9 = trunc i32 %6 to i8
br label %10
10: ; preds = %5, %4
%11 = phi i8 [ -1, %4 ], [ 0, %5 ]
%12 = phi i8 [ -1, %4 ], [ 1, %5 ]
%13 = phi i8 [ -1, %4 ], [ -66, %5 ]
%14 = phi i8 [ -1, %4 ], [ %8, %5 ]
%15 = phi i8 [ -1, %4 ], [ %9, %5 ]
%16 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %11, i8* %16
%17 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %12, i8* %17
%18 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %13, i8* %18
%19 = getelementptr inbounds i8, i8* %0, i64 4
store i8 %14, i8* %19
%20 = getelementptr inbounds i8, i8* %0, i64 5
store i8 %15, i8* %20
ret void
}