#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 */

/* Variables and functions */
int writeVarint(size_t size, unsigned char *buffer, size_t remainingSize) {
  int varintSize = -1;
  if (remainingSize == 0) {
    return -1;
  }
  if (size < 0xfd) {
    varintSize = 1;
    *buffer = size;
  } else if (size <= 0xffff) {
    if (remainingSize < 3) {
      return -1;
    }
    varintSize = 3;
    *(buffer++) = 0xfd;
    *(buffer++) = (size & 0xff);
    *(buffer++) = ((size >> 8) & 0xff);
  } else if (size <= 0xffffffff) {
    if (remainingSize < 5) {
      return -1;
    }
    varintSize = 5;
    *(buffer++) = 0xfe;
    *(buffer++) = (size & 0xff);
    *(buffer++) = ((size >> 8) & 0xff);
    *(buffer++) = ((size >> 16) & 0xff);
    *(buffer++) = ((size >> 24) & 0xff);
  } else {
    return -1;
  }
  return varintSize;
}


x86 assembly (clang, -O0):
<writeVarint>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    112e <writeVarint+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <writeVarint+0x1b5>
cmpq   $0xfd,-0x10(%rbp)
jae    1154 <writeVarint+0x54>
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    12af <writeVarint+0x1af>
cmpq   $0xffff,-0x10(%rbp)
ja     11d5 <writeVarint+0xd5>
cmpq   $0x3,-0x20(%rbp)
jae    1179 <writeVarint+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <writeVarint+0x1b5>
movl   $0x3,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0xfd,(%rax)
mov    -0x10(%rbp),%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    12aa <writeVarint+0x1aa>
mov    $0xffffffff,%eax
cmp    %rax,-0x10(%rbp)
ja     1299 <writeVarint+0x199>
cmpq   $0x5,-0x20(%rbp)
jae    11fb <writeVarint+0xfb>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <writeVarint+0x1b5>
movl   $0x5,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0xfe,(%rax)
mov    -0x10(%rbp),%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
shr    $0x10,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
shr    $0x18,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    12a5 <writeVarint+0x1a5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <writeVarint+0x1b5>
jmp    12aa <writeVarint+0x1aa>
jmp    12af <writeVarint+0x1af>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeVarint>:
push   %rbx
mov    $0xffffffff,%eax
test   %rdx,%rdx
je     1185 <writeVarint+0x85>
mov    %rdi,%rbx
mov    $0x1,%edi
cmp    $0xfd,%rbx
jb     1181 <writeVarint+0x81>
cmp    $0xffff,%rbx
ja     1146 <writeVarint+0x46>
cmp    $0x3,%rdx
jb     1185 <writeVarint+0x85>
lea    0x1(%rsi),%r8
movb   $0xfd,(%rsi)
mov    $0x3,%edi
mov    $0x8,%ecx
mov    $0x2,%eax
mov    %rbx,%rdx
jmp    1178 <writeVarint+0x78>
mov    %rbx,%rcx
shr    $0x20,%rcx
jne    1185 <writeVarint+0x85>
cmp    $0x5,%rdx
jb     1185 <writeVarint+0x85>
movb   $0xfe,(%rsi)
mov    %bl,0x1(%rsi)
lea    0x3(%rsi),%r8
mov    %bh,0x2(%rsi)
mov    %rbx,%rdx
shr    $0x10,%rdx
mov    $0x5,%edi
mov    $0x18,%ecx
mov    $0x4,%eax
add    %rax,%rsi
mov    %dl,(%r8)
shr    %cl,%rbx
mov    %bl,(%rsi)
mov    %edi,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @writeVarint(i64 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i64 %0, i64* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 -1, i32* %8
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 -1, i32* %4
br label %78
12: ; preds = %3
%13 = load i64, i64* %5
%14 = icmp ult i64 %13, 253
br i1 %14, label %15, label %19
15: ; preds = %12
store i32 1, i32* %8
%16 = load i64, i64* %5
%17 = trunc i64 %16 to i8
%18 = load i8*, i8** %6
store i8 %17, i8* %18
br label %76
19: ; preds = %12
%20 = load i64, i64* %5
%21 = icmp ule i64 %20, 65535
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = load i64, i64* %7
%24 = icmp ult i64 %23, 3
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 -1, i32* %4
br label %78
26: ; preds = %22
store i32 3, i32* %8
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %6
store i8 -3, i8* %27
%29 = load i64, i64* %5
%30 = and i64 %29, 255
%31 = trunc i64 %30 to i8
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %6
store i8 %31, i8* %32
%34 = load i64, i64* %5
%35 = lshr i64 %34, 8
%36 = and i64 %35, 255
%37 = trunc i64 %36 to i8
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %6
store i8 %37, i8* %38
br label %75
40: ; preds = %19
%41 = load i64, i64* %5
%42 = icmp ule i64 %41, 4294967295
br i1 %42, label %43, label %73
43: ; preds = %40
%44 = load i64, i64* %7
%45 = icmp ult i64 %44, 5
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %4
br label %78
47: ; preds = %43
store i32 5, i32* %8
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %6
store i8 -2, i8* %48
%50 = load i64, i64* %5
%51 = and i64 %50, 255
%52 = trunc i64 %51 to i8
%53 = load i8*, i8** %6
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %6
store i8 %52, i8* %53
%55 = load i64, i64* %5
%56 = lshr i64 %55, 8
%57 = and i64 %56, 255
%58 = trunc i64 %57 to i8
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %6
store i8 %58, i8* %59
%61 = load i64, i64* %5
%62 = lshr i64 %61, 16
%63 = and i64 %62, 255
%64 = trunc i64 %63 to i8
%65 = load i8*, i8** %6
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %6
store i8 %64, i8* %65
%67 = load i64, i64* %5
%68 = lshr i64 %67, 24
%69 = and i64 %68, 255
%70 = trunc i64 %69 to i8
%71 = load i8*, i8** %6
%72 = getelementptr inbounds i8, i8* %71, i32 1
store i8* %72, i8** %6
store i8 %70, i8* %71
br label %74
73: ; preds = %40
store i32 -1, i32* %4
br label %78
74: ; preds = %47
br label %75
75: ; preds = %74, %26
br label %76
76: ; preds = %75, %15
%77 = load i32, i32* %8
store i32 %77, i32* %4
br label %78
78: ; preds = %76, %73, %46, %25, %11
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
define i32 @writeVarint(i64 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %39, label %5
5: ; preds = %3
%6 = icmp ult i64 %0, 253
br i1 %6, label %34, label %7
7: ; preds = %5
%8 = icmp ult i64 %0, 65536
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = icmp ult i64 %2, 3
br i1 %10, label %39, label %11
11: ; preds = %9
%12 = getelementptr inbounds i8, i8* %1, i64 1
store i8 -3, i8* %1
br label %25
13: ; preds = %7
%14 = icmp ugt i64 %0, 4294967295
%15 = icmp ult i64 %2, 5
%16 = or i1 %14, %15
br i1 %16, label %39, label %17
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %1, i64 1
store i8 -2, i8* %1
%19 = trunc i64 %0 to i8
%20 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %19, i8* %18
%21 = lshr i64 %0, 8
%22 = trunc i64 %21 to i8
%23 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %22, i8* %20
%24 = lshr i64 %0, 16
br label %25
25: ; preds = %17, %11
%26 = phi i64 [ %0, %11 ], [ %24, %17 ]
%27 = phi i64 [ 2, %11 ], [ 4, %17 ]
%28 = phi i8* [ %12, %11 ], [ %23, %17 ]
%29 = phi i64 [ 8, %11 ], [ 24, %17 ]
%30 = phi i32 [ 3, %11 ], [ 5, %17 ]
%31 = trunc i64 %26 to i8
%32 = getelementptr inbounds i8, i8* %1, i64 %27
store i8 %31, i8* %28
%33 = lshr i64 %0, %29
br label %34
34: ; preds = %25, %5
%35 = phi i64 [ %0, %5 ], [ %33, %25 ]
%36 = phi i8* [ %1, %5 ], [ %32, %25 ]
%37 = phi i32 [ 1, %5 ], [ %30, %25 ]
%38 = trunc i64 %35 to i8
store i8 %38, i8* %36
br label %39
39: ; preds = %34, %13, %9, %3
%40 = phi i32 [ -1, %3 ], [ -1, %9 ], [ -1, %13 ], [ %37, %34 ]
ret i32 %40
}