#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 redisliteGetVarint(unsigned char const *, long long *);
int redisliteGetVarint32(const unsigned char *p, int *v) {
  int a, b;
  a = *p;
  if (!(a & 0x80)) {
    *v = a;
    return 1;
  }
  p++;
  b = *p;
  if (!(b & 0x80)) {
    a &= 0x7f;
    a = a << 7;
    *v = a | b;
    return 2;
  }
  p++;
  a = a << 14;
  a |= *p;
  if (!(a & 0x80)) {
    a &= (0x7f << 14) | (0x7f);
    b &= 0x7f;
    b = b << 7;
    *v = a | b;
    return 3;
  }
  {
    long long v64;
    int n;
    p -= 2;
    n = redisliteGetVarint(p, &v64);
    if ((v64 & ((((long long)1) << 32) - 1)) != v64) {
      *v = 0xffffffff;
    } else {
      *v = (int)v64;
    }
    return n;
  }
}


x86 assembly (clang, -O0):
<redisliteGetVarint32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1150 <redisliteGetVarint32+0x40>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    125e <redisliteGetVarint32+0x14e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11a1 <redisliteGetVarint32+0x91>
mov    -0x1c(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x7,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
or     -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    125e <redisliteGetVarint32+0x14e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0xe,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1209 <redisliteGetVarint32+0xf9>
mov    -0x1c(%rbp),%eax
and    $0x1fc07f,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
shl    $0x7,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
or     -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x3,-0x4(%rbp)
jmp    125e <redisliteGetVarint32+0x14e>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <redisliteGetVarint@plt>
mov    %eax,-0x2c(%rbp)
movabs $0xffffffff,%rax
and    -0x28(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     124c <redisliteGetVarint32+0x13c>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    1258 <redisliteGetVarint32+0x148>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redisliteGetVarint32>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movzbl (%rdi),%eax
test   $0x80,%al
jne    1128 <redisliteGetVarint32+0x18>
mov    %eax,(%rbx)
mov    $0x1,%eax
jmp    1185 <redisliteGetVarint32+0x75>
movzbl 0x1(%rdi),%ecx
test   $0x80,%cl
jne    1142 <redisliteGetVarint32+0x32>
and    $0x7f,%eax
shl    $0x7,%eax
or     %ecx,%eax
mov    %eax,(%rbx)
mov    $0x2,%eax
jmp    1185 <redisliteGetVarint32+0x75>
movzbl 0x2(%rdi),%edx
test   $0x80,%dl
jne    1166 <redisliteGetVarint32+0x56>
shl    $0xe,%eax
or     %edx,%eax
and    $0x1fc07f,%eax
and    $0x7f,%ecx
shl    $0x7,%ecx
or     %eax,%ecx
mov    %ecx,(%rbx)
mov    $0x3,%eax
jmp    1185 <redisliteGetVarint32+0x75>
lea    0x8(%rsp),%rsi
call   1030 <redisliteGetVarint@plt>
mov    0x8(%rsp),%rcx
mov    %rcx,%rdx
shr    $0x20,%rdx
neg    %rdx
sbb    %edx,%edx
or     %ecx,%edx
mov    %edx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @redisliteGetVarint32(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i64
%4 = load i8, i8* %0
%5 = zext i8 %4 to i32
%6 = and i32 %5, 128
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %2
store i32 %5, i32* %1
br label %39
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = and i32 %12, 128
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = shl nuw nsw i32 %5, 7
%17 = and i32 %16, 16256
%18 = or i32 %17, %12
store i32 %18, i32* %1
br label %39
19: ; preds = %9
%20 = getelementptr inbounds i8, i8* %0, i64 2
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = and i32 %22, 128
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %19
%26 = shl nuw nsw i32 %5, 14
%27 = or i32 %26, %22
%28 = and i32 %27, 2080895
%29 = shl nuw nsw i32 %12, 7
%30 = and i32 %29, 16256
%31 = or i32 %28, %30
store i32 %31, i32* %1
br label %39
32: ; preds = %19
%33 = bitcast i64* %3 to i8*
%34 = call i32 @redisliteGetVarint(i8* %0, i64* %3)
%35 = load i64, i64* %3
%36 = icmp ult i64 %35, 4294967296
%37 = trunc i64 %35 to i32
%38 = select i1 %36, i32 %37, i32 -1
store i32 %38, i32* %1
br label %39
39: ; preds = %32, %25, %15, %8
%40 = phi i32 [ %34, %32 ], [ 3, %25 ], [ 2, %15 ], [ 1, %8 ]
ret i32 %40
}