#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 /*<<< orphan*/ v_short;
typedef int /*<<< orphan*/ v_byte;
typedef int /*<<< orphan*/ v;

/* Variables and functions */
int /*<<< orphan*/ write(int, ...);
int write_long(int fd, unsigned long v) {
  unsigned char v_byte;
  unsigned short v_short;
  if (v < 0xF8) {
    v_byte = v;
    write(fd, &v_byte, sizeof(v_byte));
  } else if (v < 0xFFFF) {
    v_byte = 0xFE;
    write(fd, &v_byte, sizeof(v_byte));
    v_short = v;
    write(fd, &v_short, sizeof(v_short));
  } else {
    v_byte = 0xFF;
    write(fd, &v_byte, sizeof(v_byte));
    write(fd, &v, sizeof(v));
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0xf8,-0x10(%rbp)
jae    114c <write_long+0x3c>
mov    -0x10(%rbp),%rax
mov    %al,-0x11(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
jmp    11c0 <write_long+0xb0>
cmpq   $0xffff,-0x10(%rbp)
jae    1191 <write_long+0x81>
movb   $0xfe,-0x11(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x10(%rbp),%rax
mov    %ax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <write@plt>
jmp    11bb <write_long+0xab>
movb   $0xff,-0x11(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1030 <write@plt>
jmp    11c0 <write_long+0xb0>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_long>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,0x10(%rsp)
cmp    $0xf7,%rsi
ja     1139 <write_long+0x29>
mov    %bl,0xd(%rsp)
lea    0xd(%rsp),%rsi
mov    $0x1,%edx
jmp    118d <write_long+0x7d>
cmp    $0xfffe,%rbx
ja     116b <write_long+0x5b>
movb   $0xfe,0xd(%rsp)
lea    0xd(%rsp),%rsi
mov    $0x1,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <write@plt>
mov    %bx,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
jmp    118d <write_long+0x7d>
movb   $0xff,0xd(%rsp)
lea    0xd(%rsp),%rsi
mov    $0x1,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <write@plt>
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <write@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_long(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8
%6 = alloca i16
store i32 %0, i32* %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = icmp ult i64 %7, 248
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i64, i64* %4
%11 = trunc i64 %10 to i8
store i8 %11, i8* %5
%12 = load i32, i32* %3
%13 = call i32 (i32, ...) @write(i32 %12, i8* %5, i64 1)
br label %30
14: ; preds = %2
%15 = load i64, i64* %4
%16 = icmp ult i64 %15, 65535
br i1 %16, label %17, label %24
17: ; preds = %14
store i8 -2, i8* %5
%18 = load i32, i32* %3
%19 = call i32 (i32, ...) @write(i32 %18, i8* %5, i64 1)
%20 = load i64, i64* %4
%21 = trunc i64 %20 to i16
store i16 %21, i16* %6
%22 = load i32, i32* %3
%23 = call i32 (i32, ...) @write(i32 %22, i16* %6, i64 2)
br label %29
24: ; preds = %14
store i8 -1, i8* %5
%25 = load i32, i32* %3
%26 = call i32 (i32, ...) @write(i32 %25, i8* %5, i64 1)
%27 = load i32, i32* %3
%28 = call i32 (i32, ...) @write(i32 %27, i64* %4, i64 8)
br label %29
29: ; preds = %24, %17
br label %30
30: ; preds = %29, %9
ret i32 0
}

LLVM IR (-O2):
define i32 @write_long(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8
%5 = alloca i16
store i64 %1, i64* %3
%6 = bitcast i16* %5 to i8*
%7 = icmp ult i64 %1, 248
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = trunc i64 %1 to i8
store i8 %9, i8* %4
%10 = call i32 (i32, ...) @write(i32 %0, i8* %4, i64 1)
br label %20
11: ; preds = %2
%12 = icmp ult i64 %1, 65535
br i1 %12, label %13, label %17
13: ; preds = %11
store i8 -2, i8* %4
%14 = call i32 (i32, ...) @write(i32 %0, i8* %4, i64 1)
%15 = trunc i64 %1 to i16
store i16 %15, i16* %5
%16 = call i32 (i32, ...) @write(i32 %0, i16* %5, i64 2)
br label %20
17: ; preds = %11
store i8 -1, i8* %4
%18 = call i32 (i32, ...) @write(i32 %0, i8* %4, i64 1)
%19 = call i32 (i32, ...) @write(i32 %0, i64* %3, i64 8)
br label %20
20: ; preds = %13, %17, %8
ret i32 0
}