#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 uint8_t;
typedef int uint64_t;
typedef int uint32_t;

/* Variables and functions */
char *kvtool_ev_values_size_for_event(void *ev, uint64_t ev_size,
                                      uint64_t event_code_of_interest,
                                      uint64_t *values_size) {
  long result = 0;
  uint32_t event_code, value_size;
  uint8_t *p = ev;
  uint8_t *p_bound = p + ev_size;
  while (p < p_bound) {
    event_code = p[0];
    if (event_code > 127) {
      return "TODO: support varints > 127";
    }
    p++;
    if (p >= p_bound) {
      return "unexpected EOF after event code";
    }
    value_size = p[0];
    if (value_size > 127) {
      return "TODO: support varints > 127";
    }
    p++;
    p += value_size;
    if (p > p_bound) {
      return "unexpected EOF after value size";
    }
    if (event_code == event_code_of_interest) {
      result += value_size;
    }
  }
  *values_size = result;
  return NULL;
}


x86 assembly (clang, -O0):
<kvtool_ev_values_size_for_event>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    120f <kvtool_ev_values_size_for_event+0x10f>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x7f,-0x2c(%rbp)
jle    1166 <kvtool_ev_values_size_for_event+0x66>
lea    0xea3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1223 <kvtool_ev_values_size_for_event+0x123>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jb     1190 <kvtool_ev_values_size_for_event+0x90>
lea    0xe95(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1223 <kvtool_ev_values_size_for_event+0x123>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x7f,-0x30(%rbp)
jle    11b3 <kvtool_ev_values_size_for_event+0xb3>
lea    0xe56(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1223 <kvtool_ev_values_size_for_event+0x123>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jbe    11f2 <kvtool_ev_values_size_for_event+0xf2>
lea    0xe53(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1223 <kvtool_ev_values_size_for_event+0x123>
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    120a <kvtool_ev_values_size_for_event+0x10a>
movslq -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1135 <kvtool_ev_values_size_for_event+0x35>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvtool_ev_values_size_for_event>:
push   %r14
push   %rbx
movslq %esi,%rax
lea    (%rdi,%rax,4),%r8
xor    %r14d,%r14d
cmp    %rdi,%r8
jbe    1164 <kvtool_ev_values_size_for_event+0x64>
xor    %r9d,%r9d
lea    0xee4(%rip),%rax
lea    0xef9(%rip),%r10
lea    0xf12(%rip),%r11
xor    %r14d,%r14d
nopl   (%rax)
mov    (%rdi),%ebx
cmp    $0x7f,%ebx
jg     1169 <kvtool_ev_values_size_for_event+0x69>
lea    0x4(%rdi),%rsi
cmp    %r8,%rsi
jae    116d <kvtool_ev_values_size_for_event+0x6d>
movslq (%rsi),%rsi
cmp    $0x7f,%rsi
jg     1169 <kvtool_ev_values_size_for_event+0x69>
lea    (%rdi,%rsi,4),%rdi
add    $0x8,%rdi
cmp    %r8,%rdi
ja     1172 <kvtool_ev_values_size_for_event+0x72>
cmp    %edx,%ebx
cmovne %r9,%rsi
add    %rsi,%r14
cmp    %r8,%rdi
jb     1130 <kvtool_ev_values_size_for_event+0x30>
mov    %r14d,(%rcx)
xor    %eax,%eax
pop    %rbx
pop    %r14
ret
mov    %r10,%rax
jmp    1169 <kvtool_ev_values_size_for_event+0x69>
mov    %r11,%rax
jmp    1169 <kvtool_ev_values_size_for_event+0x69>

LLVM IR (-O0):
@.str = [28 x i8] c"TODO: support varints > 127\00"
@.str.1 = [32 x i8] c"unexpected EOF after event code\00"
@.str.2 = [32 x i8] c"unexpected EOF after value size\00"
define i8* @kvtool_ev_values_size_for_event(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i64 0, i64* %10
%15 = load i8*, i8** %6
%16 = bitcast i8* %15 to i32*
store i32* %16, i32** %13
%17 = load i32*, i32** %13
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
store i32* %20, i32** %14
br label %21
21: ; preds = %66, %4
%22 = load i32*, i32** %13
%23 = load i32*, i32** %14
%24 = icmp ult i32* %22, %23
br i1 %24, label %25, label %67
25: ; preds = %21
%26 = load i32*, i32** %13
%27 = getelementptr inbounds i32, i32* %26, i64 0
%28 = load i32, i32* %27
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = icmp sgt i32 %29, 127
br i1 %30, label %31, label %32
31: ; preds = %25
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8** %5
br label %71
32: ; preds = %25
%33 = load i32*, i32** %13
%34 = getelementptr inbounds i32, i32* %33, i32 1
store i32* %34, i32** %13
%35 = load i32*, i32** %13
%36 = load i32*, i32** %14
%37 = icmp uge i32* %35, %36
br i1 %37, label %38, label %39
38: ; preds = %32
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8** %5
br label %71
39: ; preds = %32
%40 = load i32*, i32** %13
%41 = getelementptr inbounds i32, i32* %40, i64 0
%42 = load i32, i32* %41
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = icmp sgt i32 %43, 127
br i1 %44, label %45, label %46
45: ; preds = %39
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8** %5
br label %71
46: ; preds = %39
%47 = load i32*, i32** %13
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %13
%49 = load i32, i32* %12
%50 = load i32*, i32** %13
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i32, i32* %50, i64 %51
store i32* %52, i32** %13
%53 = load i32*, i32** %13
%54 = load i32*, i32** %14
%55 = icmp ugt i32* %53, %54
br i1 %55, label %56, label %57
56: ; preds = %46
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8** %5
br label %71
57: ; preds = %46
%58 = load i32, i32* %11
%59 = load i32, i32* %8
%60 = icmp eq i32 %58, %59
br i1 %60, label %61, label %66
61: ; preds = %57
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = load i64, i64* %10
%65 = add nsw i64 %64, %63
store i64 %65, i64* %10
br label %66
66: ; preds = %61, %57
br label %21
67: ; preds = %21
%68 = load i64, i64* %10
%69 = trunc i64 %68 to i32
%70 = load i32*, i32** %9
store i32 %69, i32* %70
store i8* null, i8** %5
br label %71
71: ; preds = %67, %56, %45, %38, %31
%72 = load i8*, i8** %5
ret i8* %72
}

LLVM IR (-O2):
@.str = [28 x i8] c"TODO: support varints > 127\00"
@.str.1 = [32 x i8] c"unexpected EOF after event code\00"
@.str.2 = [32 x i8] c"unexpected EOF after value size\00"
define i8* @kvtool_ev_values_size_for_event(i8* %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = bitcast i8* %0 to i32*
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds i32, i32* %5, i64 %6
%8 = icmp ugt i32* %7, %5
br i1 %8, label %9, label %32
9: ; preds = %4, %25
%10 = phi i32* [ %23, %25 ], [ %5, %4 ]
%11 = phi i64 [ %28, %25 ], [ 0, %4 ]
%12 = load i32, i32* %10
%13 = icmp sgt i32 %12, 127
br i1 %13, label %34, label %14
14: ; preds = %9
%15 = getelementptr inbounds i32, i32* %10, i64 1
%16 = icmp ult i32* %15, %7
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = load i32, i32* %15
%19 = icmp sgt i32 %18, 127
br i1 %19, label %34, label %20
20: ; preds = %17
%21 = getelementptr inbounds i32, i32* %10, i64 2
%22 = sext i32 %18 to i64
%23 = getelementptr inbounds i32, i32* %21, i64 %22
%24 = icmp ugt i32* %23, %7
br i1 %24, label %34, label %25
25: ; preds = %20
%26 = icmp eq i32 %12, %2
%27 = select i1 %26, i64 %22, i64 0
%28 = add nsw i64 %27, %11
%29 = icmp ult i32* %23, %7
br i1 %29, label %9, label %30
30: ; preds = %25
%31 = trunc i64 %28 to i32
br label %32
32: ; preds = %30, %4
%33 = phi i32 [ 0, %4 ], [ %31, %30 ]
store i32 %33, i32* %3
br label %34
34: ; preds = %20, %17, %14, %9, %32
%35 = phi i8* [ null, %32 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %17 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %9 ]
ret i8* %35
}