#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 uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int xdf_write_uint32(int /*<<< orphan*/ *, int);
int xdf_write_uint64(int /*<<< orphan*/ *, int);
int xdf_write_uint8(int /*<<< orphan*/ *, int);
int xdf_write_length(FILE *out, uint64_t len) {
  int nb = 0;
  int ret = 0;
  if (len < 256) {
    nb = 1;
    ret = xdf_write_uint8(out, nb);
    ret |= xdf_write_uint8(out, len);
  } else if (len <= 0xffffffffULL) {
    nb = 4;
    ret = xdf_write_uint8(out, nb);
    ret |= xdf_write_uint32(out, len);
  } else {
    nb = 8;
    ret = xdf_write_uint8(out, nb);
    ret |= xdf_write_uint64(out, len);
  }
  return ret;
}


x86 assembly (clang, -O0):
<xdf_write_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x100,-0xc(%rbp)
jge    1187 <xdf_write_length+0x57>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <xdf_write_uint8@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <xdf_write_uint8@plt>
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11f3 <xdf_write_length+0xc3>
movslq -0xc(%rbp),%rax
mov    $0xffffffff,%ecx
cmp    %rcx,%rax
ja     11c6 <xdf_write_length+0x96>
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <xdf_write_uint8@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <xdf_write_uint32@plt>
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11ee <xdf_write_length+0xbe>
movl   $0x8,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <xdf_write_uint8@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <xdf_write_uint64@plt>
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11f3 <xdf_write_length+0xc3>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdf_write_length>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
cmp    $0xff,%esi
jg     114a <xdf_write_length+0x2a>
mov    $0x1,%esi
call   1030 <xdf_write_uint8@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <xdf_write_uint8@plt>
jmp    1161 <xdf_write_length+0x41>
mov    $0x4,%esi
call   1030 <xdf_write_uint8@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <xdf_write_uint32@plt>
or     %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @xdf_write_length(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 256
br i1 %8, label %9, label %18
9: ; preds = %2
store i32 1, i32* %5
%10 = load i32*, i32** %3
%11 = load i32, i32* %5
%12 = call i32 @xdf_write_uint8(i32* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = call i32 @xdf_write_uint8(i32* %13, i32 %14)
%16 = load i32, i32* %6
%17 = or i32 %16, %15
store i32 %17, i32* %6
br label %41
18: ; preds = %2
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = icmp ule i64 %20, 4294967295
br i1 %21, label %22, label %31
22: ; preds = %18
store i32 4, i32* %5
%23 = load i32*, i32** %3
%24 = load i32, i32* %5
%25 = call i32 @xdf_write_uint8(i32* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32*, i32** %3
%27 = load i32, i32* %4
%28 = call i32 @xdf_write_uint32(i32* %26, i32 %27)
%29 = load i32, i32* %6
%30 = or i32 %29, %28
store i32 %30, i32* %6
br label %40
31: ; preds = %18
store i32 8, i32* %5
%32 = load i32*, i32** %3
%33 = load i32, i32* %5
%34 = call i32 @xdf_write_uint8(i32* %32, i32 %33)
store i32 %34, i32* %6
%35 = load i32*, i32** %3
%36 = load i32, i32* %4
%37 = call i32 @xdf_write_uint64(i32* %35, i32 %36)
%38 = load i32, i32* %6
%39 = or i32 %38, %37
store i32 %39, i32* %6
br label %40
40: ; preds = %31, %22
br label %41
41: ; preds = %40, %9
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
define i32 @xdf_write_length(i32* %0, i32 %1) {
%3 = icmp slt i32 %1, 256
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = tail call i32 @xdf_write_uint8(i32* %0, i32 1)
%6 = tail call i32 @xdf_write_uint8(i32* %0, i32 %1)
%7 = or i32 %6, %5
br label %12
8: ; preds = %2
%9 = tail call i32 @xdf_write_uint8(i32* %0, i32 4)
%10 = tail call i32 @xdf_write_uint32(i32* %0, i32 %1)
%11 = or i32 %10, %9
br label %12
12: ; preds = %8, %4
%13 = phi i32 [ %7, %4 ], [ %11, %8 ]
ret i32 %13
}