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

/* Variables and functions */
int bufToInt16(int *);
int bufToUInt32(int *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int readTag(int, char *, int *, int);
int /*<<< orphan*/ stderr;
int findBlockIndex(int entId, int blockId, int previous) {
  uint8_t typesBuffer[100000];
  int bufLen = readTag(entId, "types", typesBuffer, sizeof(typesBuffer));
  if (!bufLen)
    return -1;
  int contentOrdinal = typesBuffer[0];
  if (contentOrdinal != 2) {
    fprintf(
        stderr,
        "Error, something has changed in the fileformat. The Ents are fighting "
        "back. Better to flee like a coward than risk messing with it.\n");
    exit(-1);
  }
  int numTypes = bufToUInt32(typesBuffer + 1);
  int contentPos = 1 + 4;
  for (int i = 0; i < numTypes && contentPos < bufLen; i++, contentPos += 2)
    if (i > previous && bufToInt16(typesBuffer + contentPos) == blockId) {
      return i;
    }
  return -1;
}


x86 assembly (clang, -O0):
<findBlockIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x61ab0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x61a90(%rbp),%rdx
lea    0xe8b(%rip),%rsi
mov    $0x61a80,%ecx
call   1050 <readTag@plt>
mov    %eax,-0x61a94(%rbp)
cmpl   $0x0,-0x61a94(%rbp)
jne    119e <findBlockIndex+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <findBlockIndex+0x161>
mov    -0x61a90(%rbp),%eax
mov    %eax,-0x61a98(%rbp)
cmpl   $0x2,-0x61a98(%rbp)
je     11d6 <findBlockIndex+0x86>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    -0x61a90(%rbp),%rdi
add    $0x4,%rdi
call   1040 <bufToUInt32@plt>
mov    %eax,-0x61a9c(%rbp)
movl   $0x5,-0x61aa0(%rbp)
movl   $0x0,-0x61aa4(%rbp)
mov    -0x61aa4(%rbp),%ecx
xor    %eax,%eax
cmp    -0x61a9c(%rbp),%ecx
mov    %al,-0x61aa5(%rbp)
jge    122f <findBlockIndex+0xdf>
mov    -0x61aa0(%rbp),%eax
cmp    -0x61a94(%rbp),%eax
setl   %al
mov    %al,-0x61aa5(%rbp)
mov    -0x61aa5(%rbp),%al
test   $0x1,%al
jne    1242 <findBlockIndex+0xf2>
jmp    12aa <findBlockIndex+0x15a>
mov    -0x61aa4(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1282 <findBlockIndex+0x132>
lea    -0x61a90(%rbp),%rdi
movslq -0x61aa0(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1070 <bufToInt16@plt>
cmp    -0xc(%rbp),%eax
jne    1282 <findBlockIndex+0x132>
mov    -0x61aa4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <findBlockIndex+0x161>
jmp    1287 <findBlockIndex+0x137>
mov    -0x61aa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61aa4(%rbp)
mov    -0x61aa0(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x61aa0(%rbp)
jmp    1200 <findBlockIndex+0xb0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x61ab0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findBlockIndex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x61a98,%rsp
mov    %edx,%r12d
mov    %esi,0xc(%rsp)
lea    0xe91(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    $0x61a80,%ecx
call   1050 <readTag@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
je     11f2 <findBlockIndex+0xa2>
cmpl   $0x2,0x10(%rsp)
jne    1207 <findBlockIndex+0xb7>
mov    %eax,%ebp
lea    0x14(%rsp),%rdi
call   1040 <bufToUInt32@plt>
test   %eax,%eax
jle    11f2 <findBlockIndex+0xa2>
cmp    $0x6,%ebp
jl     11f2 <findBlockIndex+0xa2>
mov    %eax,%ebx
mov    %ebp,%r13d
lea    0x24(%rsp),%rbp
xor    %r14d,%r14d
mov    $0x7,%r15d
nopw   0x0(%rax,%rax,1)
cmp    %r12d,%r14d
jle    11d3 <findBlockIndex+0x83>
mov    %rbp,%rdi
call   1070 <bufToInt16@plt>
cmp    0xc(%rsp),%eax
je     11f2 <findBlockIndex+0xa2>
add    $0x1,%r14d
cmp    %ebx,%r14d
jge    11ec <findBlockIndex+0x9c>
add    $0x8,%rbp
lea    0x2(%r15),%rax
cmp    %r13,%r15
mov    %rax,%r15
jb     11c0 <findBlockIndex+0x70>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x61a98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"types\00"
@stderr = global i32 0
@.str.1 = [133 x i8] c"Error, something has changed in the fileformat. The Ents are fighting back. Better to flee like a coward than risk messing with it.\0A\00"
define i32 @findBlockIndex(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100000 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = load i32, i32* %5
%15 = getelementptr inbounds [100000 x i32], [100000 x i32]* %8, i64 0, i64 0
%16 = call i32 @readTag(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %15, i32 400000)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %3
store i32 -1, i32* %4
br label %64
20: ; preds = %3
%21 = getelementptr inbounds [100000 x i32], [100000 x i32]* %8, i64 0, i64 0
%22 = load i32, i32* %21
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp ne i32 %23, 2
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %20
%30 = getelementptr inbounds [100000 x i32], [100000 x i32]* %8, i64 0, i64 0
%31 = getelementptr inbounds i32, i32* %30, i64 1
%32 = call i32 @bufToUInt32(i32* %31)
store i32 %32, i32* %11
store i32 5, i32* %12
store i32 0, i32* %13
br label %33
33: ; preds = %58, %29
%34 = load i32, i32* %13
%35 = load i32, i32* %11
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* %12
%39 = load i32, i32* %9
%40 = icmp slt i32 %38, %39
br label %41
41: ; preds = %37, %33
%42 = phi i1 [ false, %33 ], [ %40, %37 ]
br i1 %42, label %43, label %63
43: ; preds = %41
%44 = load i32, i32* %13
%45 = load i32, i32* %7
%46 = icmp sgt i32 %44, %45
br i1 %46, label %47, label %57
47: ; preds = %43
%48 = getelementptr inbounds [100000 x i32], [100000 x i32]* %8, i64 0, i64 0
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = call i32 @bufToInt16(i32* %51)
%53 = load i32, i32* %6
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = load i32, i32* %13
store i32 %56, i32* %4
br label %64
57: ; preds = %47, %43
br label %58
58: ; preds = %57
%59 = load i32, i32* %13
%60 = add nsw i32 %59, 1
store i32 %60, i32* %13
%61 = load i32, i32* %12
%62 = add nsw i32 %61, 2
store i32 %62, i32* %12
br label %33
63: ; preds = %41
store i32 -1, i32* %4
br label %64
64: ; preds = %63, %55, %19
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [6 x i8] c"types\00"
@stderr = global i32 0
@.str.1 = [133 x i8] c"Error, something has changed in the fileformat. The Ents are fighting back. Better to flee like a coward than risk messing with it.\0A\00"
define i32 @findBlockIndex(i32 %0, i32 %1, i32 %2) {
%4 = alloca [100000 x i32]
%5 = bitcast [100000 x i32]* %4 to i8*
%6 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 0
%7 = call i32 @readTag(i32 %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %6, i32 400000)
%8 = icmp eq i32 %7, 0
br i1 %8, label %38, label %9
9: ; preds = %3
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 2
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %9
%17 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 1
%18 = call i32 @bufToUInt32(i32* %17)
%19 = icmp sgt i32 %18, 0
%20 = icmp sgt i32 %7, 5
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %22, label %38
22: ; preds = %16
%23 = zext i32 %7 to i64
br label %24
24: ; preds = %22, %32
%25 = phi i64 [ 5, %22 ], [ %34, %32 ]
%26 = phi i32 [ 0, %22 ], [ %33, %32 ]
%27 = icmp sgt i32 %26, %2
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 %25
%30 = call i32 @bufToInt16(i32* %29)
%31 = icmp eq i32 %30, %1
br i1 %31, label %38, label %32
32: ; preds = %24, %28
%33 = add nuw nsw i32 %26, 1
%34 = add nuw nsw i64 %25, 2
%35 = icmp slt i32 %33, %18
%36 = icmp ult i64 %34, %23
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %24, label %38
38: ; preds = %28, %32, %16, %3
%39 = phi i32 [ -1, %3 ], [ -1, %16 ], [ %26, %28 ], [ -1, %32 ]
ret i32 %39
}