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

/* Variables and functions */
int /*<<< orphan*/ strncmp(char const *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int /*<<< orphan*/ strtoull(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ utils_reverse_hex(char *, int);
int utils_varint_to_uint64(const char *vi, uint64_t *i) {
  char v[20] = {0};
  int len;
  if (!vi) {
    len = 0;
  } else if (!strncmp(vi, "ff", 2)) {
    len = 16;
  } else if (!strncmp(vi, "fe", 2)) {
    len = 8;
  } else if (!strncmp(vi, "fd", 2)) {
    len = 4;
  } else {
    len = 2;
  }
  if (len == 0) {
  } else if (len > 2) {
    strncpy(v, vi + 2, len);
    utils_reverse_hex(v, len);
  } else {
    strncpy(v, vi, len);
  }
  *i = strtoull(v, NULL, 16);
  return len;
}


x86 assembly (clang, -O0):
<utils_varint_to_uint64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1187 <utils_varint_to_uint64+0x37>
movl   $0x0,-0x34(%rbp)
jmp    121b <utils_varint_to_uint64+0xcb>
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    11b1 <utils_varint_to_uint64+0x61>
movl   $0x10,-0x34(%rbp)
jmp    1216 <utils_varint_to_uint64+0xc6>
mov    -0x8(%rbp),%rdi
lea    0xe47(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    11db <utils_varint_to_uint64+0x8b>
movl   $0x8,-0x34(%rbp)
jmp    1211 <utils_varint_to_uint64+0xc1>
mov    -0x8(%rbp),%rdi
lea    0xe20(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    1205 <utils_varint_to_uint64+0xb5>
movl   $0x4,-0x34(%rbp)
jmp    120c <utils_varint_to_uint64+0xbc>
movl   $0x2,-0x34(%rbp)
jmp    1211 <utils_varint_to_uint64+0xc1>
jmp    1216 <utils_varint_to_uint64+0xc6>
jmp    121b <utils_varint_to_uint64+0xcb>
cmpl   $0x0,-0x34(%rbp)
jne    122a <utils_varint_to_uint64+0xda>
jmp    126e <utils_varint_to_uint64+0x11e>
cmpl   $0x2,-0x34(%rbp)
jle    1259 <utils_varint_to_uint64+0x109>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x2,%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <utils_reverse_hex@plt>
jmp    1269 <utils_varint_to_uint64+0x119>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
jmp    126e <utils_varint_to_uint64+0x11e>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_varint_to_uint64>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
test   %rdi,%rdi
je     11c0 <utils_varint_to_uint64+0x80>
mov    %rdi,%rbx
lea    0xe95(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     11c4 <utils_varint_to_uint64+0x84>
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     11cb <utils_varint_to_uint64+0x8b>
lea    0xe6e(%rip),%rsi
mov    $0x2,%ebp
mov    %rbx,%rdi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     11d2 <utils_varint_to_uint64+0x92>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
jmp    11f5 <utils_varint_to_uint64+0xb5>
xor    %ebp,%ebp
jmp    11f5 <utils_varint_to_uint64+0xb5>
mov    $0x10,%ebp
jmp    11d7 <utils_varint_to_uint64+0x97>
mov    $0x8,%ebp
jmp    11d7 <utils_varint_to_uint64+0x97>
mov    $0x4,%ebp
add    $0x2,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <utils_reverse_hex@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoull@plt>
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ff\00"
@.str.1 = [3 x i8] c"fe\00"
@.str.2 = [3 x i8] c"fd\00"
define i32 @utils_varint_to_uint64(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [20 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%7 = bitcast [20 x i8]* %5 to i8*
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 0, i32* %6
br label %30
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 @strncmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
store i32 16, i32* %6
br label %29
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 @strncmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %16
store i32 8, i32* %6
br label %28
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = call i32 @strncmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %21
store i32 4, i32* %6
br label %27
26: ; preds = %21
store i32 2, i32* %6
br label %27
27: ; preds = %26, %25
br label %28
28: ; preds = %27, %20
br label %29
29: ; preds = %28, %15
br label %30
30: ; preds = %29, %10
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %52
34: ; preds = %30
%35 = load i32, i32* %6
%36 = icmp sgt i32 %35, 2
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i64 2
%41 = load i32, i32* %6
%42 = call i32 @strncpy(i8* %38, i8* %40, i32 %41)
%43 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %6
%45 = call i32 @utils_reverse_hex(i8* %43, i32 %44)
br label %51
46: ; preds = %34
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%48 = load i8*, i8** %3
%49 = load i32, i32* %6
%50 = call i32 @strncpy(i8* %47, i8* %48, i32 %49)
br label %51
51: ; preds = %46, %37
br label %52
52: ; preds = %51, %33
%53 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%54 = call i32 @strtoull(i8* %53, i32* null, i32 16)
%55 = load i32*, i32** %4
store i32 %54, i32* %55
%56 = load i32, i32* %6
ret i32 %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"ff\00"
@.str.1 = [3 x i8] c"fe\00"
@.str.2 = [3 x i8] c"fd\00"
define i32 @utils_varint_to_uint64(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %6, %9, %12
%16 = phi i32 [ 4, %12 ], [ 16, %6 ], [ 8, %9 ]
%17 = getelementptr inbounds i8, i8* %0, i64 2
%18 = call i32 @strncpy(i8* %4, i8* %17, i32 %16)
%19 = call i32 @utils_reverse_hex(i8* %4, i32 %16)
br label %22
20: ; preds = %12
%21 = call i32 @strncpy(i8* %4, i8* %0, i32 2)
br label %22
22: ; preds = %2, %15, %20
%23 = phi i32 [ %16, %15 ], [ 2, %20 ], [ 0, %2 ]
%24 = call i32 @strtoull(i8* nocapture %4, i32* null, i32 16)
store i32 %24, i32* %1
ret i32 %23
}