#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 */
int /*<<< orphan*/ memcpy(void *, int *, int);
char *kvtool_ev_extract_values(void *ev, uint64_t ev_size,
                               uint64_t event_code_of_interest, void *values,
                               uint64_t values_size) {
  uint32_t event_code, value_size;
  uint8_t *p = ev;
  uint8_t *p_bound = p + ev_size;
  uint32_t values_pos = 0;
  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++;
    if ((p + value_size) > p_bound) {
      return "unexpected EOF after value size";
    }
    if (event_code == event_code_of_interest) {
      memcpy(&(values[values_pos]), p, value_size);
      values_pos += value_size;
    }
    p += value_size;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<kvtool_ev_extract_values>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%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)
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    1245 <kvtool_ev_extract_values+0x135>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x7f,-0x28(%rbp)
jle    117d <kvtool_ev_extract_values+0x6d>
lea    0xe8c(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124d <kvtool_ev_extract_values+0x13d>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jb     11a7 <kvtool_ev_extract_values+0x97>
lea    0xe7e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124d <kvtool_ev_extract_values+0x13d>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x7f,-0x2c(%rbp)
jle    11ca <kvtool_ev_extract_values+0xba>
lea    0xe3f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124d <kvtool_ev_extract_values+0x13d>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
cmp    -0x40(%rbp),%rax
jbe    11ff <kvtool_ev_extract_values+0xef>
lea    0xe46(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124d <kvtool_ev_extract_values+0x13d>
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    122b <kvtool_ev_extract_values+0x11b>
mov    -0x20(%rbp),%rdi
movslq -0x44(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    114c <kvtool_ev_extract_values+0x3c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvtool_ev_extract_values>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%eax
movslq %esi,%rax
lea    (%rdi,%rax,4),%rbp
cmp    %rdi,%rbp
jbe    118a <kvtool_ev_extract_values+0x7a>
mov    %rcx,%r15
mov    %edx,%r12d
mov    %rdi,%rsi
xor    %r13d,%r13d
jmp    1148 <kvtool_ev_extract_values+0x38>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rsi
cmp    %rbp,%r14
jae    118a <kvtool_ev_extract_values+0x7a>
mov    (%rsi),%eax
cmp    $0x7f,%eax
jg     118e <kvtool_ev_extract_values+0x7e>
lea    0x4(%rsi),%rcx
cmp    %rbp,%rcx
jae    1197 <kvtool_ev_extract_values+0x87>
movslq (%rcx),%rbx
cmp    $0x7f,%rbx
jg     118e <kvtool_ev_extract_values+0x7e>
lea    (%rsi,%rbx,4),%r14
add    $0x8,%r14
cmp    %rbp,%r14
ja     11a0 <kvtool_ev_extract_values+0x90>
cmp    %r12d,%eax
jne    1140 <kvtool_ev_extract_values+0x30>
add    $0x8,%rsi
movslq %r13d,%r13
lea    (%r15,%r13,1),%rdi
mov    %ebx,%edx
call   1030 <memcpy@plt>
add    %ebx,%r13d
jmp    1140 <kvtool_ev_extract_values+0x30>
xor    %eax,%eax
jmp    11a7 <kvtool_ev_extract_values+0x97>
lea    0xe6b(%rip),%rax
jmp    11a7 <kvtool_ev_extract_values+0x97>
lea    0xe7e(%rip),%rax
jmp    11a7 <kvtool_ev_extract_values+0x97>
lea    0xe95(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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_extract_values(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%17 = load i8*, i8** %7
%18 = bitcast i8* %17 to i32*
store i32* %18, i32** %14
%19 = load i32*, i32** %14
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
store i32* %22, i32** %15
store i32 0, i32* %16
br label %23
23: ; preds = %73, %5
%24 = load i32*, i32** %14
%25 = load i32*, i32** %15
%26 = icmp ult i32* %24, %25
br i1 %26, label %27, label %78
27: ; preds = %23
%28 = load i32*, i32** %14
%29 = getelementptr inbounds i32, i32* %28, i64 0
%30 = load i32, i32* %29
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = icmp sgt i32 %31, 127
br i1 %32, label %33, label %34
33: ; preds = %27
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8** %6
br label %79
34: ; preds = %27
%35 = load i32*, i32** %14
%36 = getelementptr inbounds i32, i32* %35, i32 1
store i32* %36, i32** %14
%37 = load i32*, i32** %14
%38 = load i32*, i32** %15
%39 = icmp uge i32* %37, %38
br i1 %39, label %40, label %41
40: ; preds = %34
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %79
41: ; preds = %34
%42 = load i32*, i32** %14
%43 = getelementptr inbounds i32, i32* %42, i64 0
%44 = load i32, i32* %43
store i32 %44, i32* %13
%45 = load i32, i32* %13
%46 = icmp sgt i32 %45, 127
br i1 %46, label %47, label %48
47: ; preds = %41
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8** %6
br label %79
48: ; preds = %41
%49 = load i32*, i32** %14
%50 = getelementptr inbounds i32, i32* %49, i32 1
store i32* %50, i32** %14
%51 = load i32*, i32** %14
%52 = load i32, i32* %13
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32*, i32** %15
%56 = icmp ugt i32* %54, %55
br i1 %56, label %57, label %58
57: ; preds = %48
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %79
58: ; preds = %48
%59 = load i32, i32* %12
%60 = load i32, i32* %9
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %73
62: ; preds = %58
%63 = load i8*, i8** %10
%64 = load i32, i32* %16
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32*, i32** %14
%68 = load i32, i32* %13
%69 = call i32 @memcpy(i8* %66, i32* %67, i32 %68)
%70 = load i32, i32* %13
%71 = load i32, i32* %16
%72 = add nsw i32 %71, %70
store i32 %72, i32* %16
br label %73
73: ; preds = %62, %58
%74 = load i32, i32* %13
%75 = load i32*, i32** %14
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i32, i32* %75, i64 %76
store i32* %77, i32** %14
br label %23
78: ; preds = %23
store i8* null, i8** %6
br label %79
79: ; preds = %78, %57, %47, %40, %33
%80 = load i8*, i8** %6
ret i8* %80
}

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_extract_values(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = bitcast i8* %0 to i32*
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds i32, i32* %6, i64 %7
%9 = icmp ugt i32* %8, %6
br i1 %9, label %10, label %36
10: ; preds = %5, %33
%11 = phi i32 [ %34, %33 ], [ 0, %5 ]
%12 = phi i32* [ %24, %33 ], [ %6, %5 ]
%13 = load i32, i32* %12
%14 = icmp sgt i32 %13, 127
br i1 %14, label %36, label %15
15: ; preds = %10
%16 = getelementptr inbounds i32, i32* %12, i64 1
%17 = icmp ult i32* %16, %8
br i1 %17, label %18, label %36
18: ; preds = %15
%19 = load i32, i32* %16
%20 = icmp sgt i32 %19, 127
br i1 %20, label %36, label %21
21: ; preds = %18
%22 = getelementptr inbounds i32, i32* %12, i64 2
%23 = sext i32 %19 to i64
%24 = getelementptr inbounds i32, i32* %22, i64 %23
%25 = icmp ugt i32* %24, %8
br i1 %25, label %36, label %26
26: ; preds = %21
%27 = icmp eq i32 %13, %2
br i1 %27, label %28, label %33
28: ; preds = %26
%29 = sext i32 %11 to i64
%30 = getelementptr inbounds i8, i8* %3, i64 %29
%31 = tail call i32 @memcpy(i8* %30, i32* %22, i32 %19)
%32 = add nsw i32 %19, %11
br label %33
33: ; preds = %28, %26
%34 = phi i32 [ %32, %28 ], [ %11, %26 ]
%35 = icmp ult i32* %24, %8
br i1 %35, label %10, label %36
36: ; preds = %10, %15, %18, %21, %33, %5
%37 = phi i8* [ null, %5 ], [ null, %33 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), %21 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %18 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %15 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %10 ]
ret i8* %37
}