#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 findBlockIndex(int, int, int);
scalar_t__ getValueAsInt(int, int);
int getBlockAmount(int entId, int block) {
  int total = 0;
  int prev = -1;
  for (;;) {
    int index = findBlockIndex(entId, block, prev);
    if (index == -1)
      return total;
    total += getValueAsInt(entId, index);
  }
  return 0;
}


x86 assembly (clang, -O0):
<getBlockAmount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <findBlockIndex@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1160 <getBlockAmount+0x40>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <getValueAsInt@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
jmp    113c <getBlockAmount+0x1c>

x86 assembly (clang, -O2):
<getBlockAmount>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0xffffffff,%edx
call   1040 <findBlockIndex@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     115f <getBlockAmount+0x3f>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %eax,%esi
call   1030 <getValueAsInt@plt>
add    %eax,%ebx
mov    %ebp,%edi
mov    %r14d,%esi
mov    $0xffffffff,%edx
call   1040 <findBlockIndex@plt>
cmp    $0xffffffff,%eax
jne    1140 <getBlockAmount+0x20>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getBlockAmount(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 -1, i32* %6
br label %8
8: ; preds = %17, %2
%9 = load i32, i32* %3
%10 = load i32, i32* %4
%11 = load i32, i32* %6
%12 = call i32 @findBlockIndex(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = load i32, i32* %5
ret i32 %16
17: ; preds = %8
%18 = load i32, i32* %3
%19 = load i32, i32* %7
%20 = call i64 @getValueAsInt(i32 %18, i32 %19)
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = add nsw i64 %22, %20
%24 = trunc i64 %23 to i32
store i32 %24, i32* %5
br label %8
}

LLVM IR (-O2):
define i32 @getBlockAmount(i32 %0, i32 %1) {
%3 = tail call i32 @findBlockIndex(i32 %0, i32 %1, i32 -1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %13, label %5
5: ; preds = %2, %5
%6 = phi i32 [ %11, %5 ], [ %3, %2 ]
%7 = phi i32 [ %10, %5 ], [ 0, %2 ]
%8 = tail call i64 @getValueAsInt(i32 %0, i32 %6)
%9 = trunc i64 %8 to i32
%10 = add i32 %7, %9
%11 = tail call i32 @findBlockIndex(i32 %0, i32 %1, i32 -1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %5
13: ; preds = %5, %2
%14 = phi i32 [ 0, %2 ], [ %10, %5 ]
ret i32 %14
}