#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 /*<<< orphan*/ valuesBuffer;
typedef int uint8_t;
typedef int /*<<< orphan*/ typesBuffer;
typedef int /*<<< orphan*/ label;

/* Variables and functions */
int bufToInt16(int *);
int bufToInt32(int *);
int bufToUInt32(int *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getLabel(int **, char *, int);
int getOrdinal(int **);
int /*<<< orphan*/ printf(char *, ...);
int readTag(int, char *, int *, int);
int /*<<< orphan*/ skipStructEntry(int **);
int /*<<< orphan*/ stderr;
void printInventory(int entId) {
  uint8_t valuesBuffer[100000];
  uint8_t typesBuffer[100000];
  int valLen = readTag(entId, "values", valuesBuffer, sizeof(valuesBuffer));
  int typeLen = readTag(entId, "types", typesBuffer, sizeof(typesBuffer));
  if (valLen < 1 || typeLen < 1) {
    printf("Error finding all tags needed to print inventory\n");
  }
  char label[256];
  int typesOrdinal = typesBuffer[0];
  if (typesOrdinal != 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);
  uint8_t *typesPnt = typesBuffer + 5;
  uint8_t *valuesPnt = valuesBuffer;
  for (int i = 0; i < numTypes; i++) {
    int type = bufToInt16(typesPnt);
    typesPnt += 2;
    uint8_t *pnt = valuesPnt;
    int valueOrdinal = getOrdinal(&pnt);
    getLabel(&pnt, label, sizeof(label));
    if (valueOrdinal == 3) {
      int amount = bufToInt32(pnt);
      printf("%d %d\n", type, amount);
    }
    skipStructEntry(&valuesPnt);
  }
}


x86 assembly (clang, -O0):
<printInventory>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc3650,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x61a90(%rbp),%rdx
lea    0xe41(%rip),%rsi
mov    $0x61a80,%ecx
call   1080 <readTag@plt>
mov    %eax,-0xc3514(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xc3510(%rbp),%rdx
lea    0xe27(%rip),%rsi
mov    $0x61a80,%ecx
call   1080 <readTag@plt>
mov    %eax,-0xc3518(%rbp)
cmpl   $0x1,-0xc3514(%rbp)
jl     120a <printInventory+0x6a>
cmpl   $0x1,-0xc3518(%rbp)
jge    1218 <printInventory+0x78>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc3510(%rbp),%eax
mov    %eax,-0xc3624(%rbp)
cmpl   $0x2,-0xc3624(%rbp)
je     1250 <printInventory+0xb0>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0xc3510(%rbp),%rdi
add    $0x4,%rdi
call   1070 <bufToUInt32@plt>
mov    %eax,-0xc3628(%rbp)
lea    -0xc3510(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0xc3630(%rbp)
lea    -0x61a90(%rbp),%rax
mov    %rax,-0xc3638(%rbp)
movl   $0x0,-0xc363c(%rbp)
mov    -0xc363c(%rbp),%eax
cmp    -0xc3628(%rbp),%eax
jge    1357 <printInventory+0x1b7>
mov    -0xc3630(%rbp),%rdi
call   10c0 <bufToInt16@plt>
mov    %eax,-0xc3640(%rbp)
mov    -0xc3630(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0xc3630(%rbp)
mov    -0xc3638(%rbp),%rax
mov    %rax,-0xc3648(%rbp)
lea    -0xc3648(%rbp),%rdi
call   1040 <getOrdinal@plt>
mov    %eax,-0xc364c(%rbp)
lea    -0xc3620(%rbp),%rsi
lea    -0xc3648(%rbp),%rdi
mov    $0x100,%edx
call   1060 <getLabel@plt>
cmpl   $0x3,-0xc364c(%rbp)
jne    1337 <printInventory+0x197>
mov    -0xc3648(%rbp),%rdi
call   1090 <bufToInt32@plt>
mov    %eax,-0xc3650(%rbp)
mov    -0xc3640(%rbp),%esi
mov    -0xc3650(%rbp),%edx
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xc3638(%rbp),%rdi
call   10b0 <skipStructEntry@plt>
mov    -0xc363c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc363c(%rbp)
jmp    1290 <printInventory+0xf0>
add    $0xc3650,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printInventory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc3618,%rsp
mov    %edi,%ebp
lea    0xe36(%rip),%rsi
lea    0x61b90(%rsp),%r14
mov    %r14,%rdx
mov    $0x61a80,%ecx
call   1090 <readTag@plt>
mov    %eax,%ebx
lea    0xe1f(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    %ebp,%edi
mov    $0x61a80,%ecx
call   1090 <readTag@plt>
test   %ebx,%ebx
jle    1204 <printInventory+0x54>
test   %eax,%eax
jg     1210 <printInventory+0x60>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x2,0x110(%rsp)
02
jne    12c8 <printInventory+0x118>
lea    0x114(%rsp),%rdi
call   1080 <bufToUInt32@plt>
mov    %r14,0x8(%rsp)
test   %eax,%eax
jle    12b6 <printInventory+0x106>
mov    %eax,%ebp
lea    0x124(%rsp),%rbx
mov    %rsp,%r14
lea    0x8(%rsp),%r13
jmp    1261 <printInventory+0xb1>
nopl   0x0(%rax)
add    $0x8,%rbx
mov    %r13,%rdi
call   10c0 <skipStructEntry@plt>
add    $0xffffffff,%ebp
je     12b6 <printInventory+0x106>
mov    %rbx,%rdi
call   10d0 <bufToInt16@plt>
mov    %eax,%r12d
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %r14,%rdi
call   1050 <getOrdinal@plt>
mov    %eax,%r15d
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1070 <getLabel@plt>
cmp    $0x3,%r15d
jne    1250 <printInventory+0xa0>
mov    (%rsp),%rdi
call   10a0 <bufToInt32@plt>
lea    0xdea(%rip),%rdi
mov    %r12d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1250 <printInventory+0xa0>
add    $0xc3618,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd35(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"values\00"
@.str.1 = [6 x i8] c"types\00"
@.str.2 = [50 x i8] c"Error finding all tags needed to print inventory\0A\00"
@stderr = global i32 0
@.str.3 = [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"
@.str.4 = [7 x i8] c"%d %d\0A\00"
define void @printInventory(i32 %0) {
%2 = alloca i32
%3 = alloca [100000 x i32]
%4 = alloca [100000 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %2
%17 = load i32, i32* %2
%18 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 0
%19 = call i32 @readTag(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %18, i32 400000)
store i32 %19, i32* %5
%20 = load i32, i32* %2
%21 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 0
%22 = call i32 @readTag(i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %21, i32 400000)
store i32 %22, i32* %6
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 1
br i1 %24, label %28, label %25
25: ; preds = %1
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %25, %1
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 0
%32 = load i32, i32* %31
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %30
%40 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 0
%41 = getelementptr inbounds i32, i32* %40, i64 1
%42 = call i32 @bufToUInt32(i32* %41)
store i32 %42, i32* %9
%43 = getelementptr inbounds [100000 x i32], [100000 x i32]* %4, i64 0, i64 0
%44 = getelementptr inbounds i32, i32* %43, i64 5
store i32* %44, i32** %10
%45 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 0
store i32* %45, i32** %11
store i32 0, i32* %12
br label %46
46: ; preds = %69, %39
%47 = load i32, i32* %12
%48 = load i32, i32* %9
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %72
50: ; preds = %46
%51 = load i32*, i32** %10
%52 = call i32 @bufToInt16(i32* %51)
store i32 %52, i32* %13
%53 = load i32*, i32** %10
%54 = getelementptr inbounds i32, i32* %53, i64 2
store i32* %54, i32** %10
%55 = load i32*, i32** %11
store i32* %55, i32** %14
%56 = call i32 @getOrdinal(i32** %14)
store i32 %56, i32* %15
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%58 = call i32 @getLabel(i32** %14, i8* %57, i32 256)
%59 = load i32, i32* %15
%60 = icmp eq i32 %59, 3
br i1 %60, label %61, label %67
61: ; preds = %50
%62 = load i32*, i32** %14
%63 = call i32 @bufToInt32(i32* %62)
store i32 %63, i32* %16
%64 = load i32, i32* %13
%65 = load i32, i32* %16
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %64, i32 %65)
br label %67
67: ; preds = %61, %50
%68 = call i32 @skipStructEntry(i32** %11)
br label %69
69: ; preds = %67
%70 = load i32, i32* %12
%71 = add nsw i32 %70, 1
store i32 %71, i32* %12
br label %46
72: ; preds = %46
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"values\00"
@.str.1 = [6 x i8] c"types\00"
@stderr = global i32 0
@.str.3 = [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"
@.str.4 = [7 x i8] c"%d %d\0A\00"
@str = [49 x i8] c"Error finding all tags needed to print inventory\00"
define void @printInventory(i32 %0) {
%2 = alloca [100000 x i32]
%3 = alloca [100000 x i32]
%4 = alloca [256 x i8]
%5 = alloca i32*
%6 = alloca i32*
%7 = bitcast [100000 x i32]* %2 to i8*
%8 = bitcast [100000 x i32]* %3 to i8*
%9 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 0
%10 = call i32 @readTag(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %9, i32 400000)
%11 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 0
%12 = call i32 @readTag(i32 %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %11, i32 400000)
%13 = icmp slt i32 %10, 1
%14 = icmp slt i32 %12, 1
%15 = select i1 %13, i1 true, i1 %14
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %1, %16
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, 2
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %18
%27 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 1
%28 = call i32 @bufToUInt32(i32* %27)
%29 = bitcast i32** %5 to i8*
store i32* %9, i32** %5
%30 = icmp sgt i32 %28, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 5
%33 = bitcast i32** %6 to i8*
br label %35
34: ; preds = %48, %26
ret void
35: ; preds = %31, %48
%36 = phi i32* [ %32, %31 ], [ %39, %48 ]
%37 = phi i32 [ 0, %31 ], [ %50, %48 ]
%38 = call i32 @bufToInt16(i32* %36)
%39 = getelementptr inbounds i32, i32* %36, i64 2
%40 = load i32*, i32** %5
store i32* %40, i32** %6
%41 = call i32 @getOrdinal(i32** %6)
%42 = call i32 @getLabel(i32** %6, i8* %19, i32 256)
%43 = icmp eq i32 %41, 3
br i1 %43, label %44, label %48
44: ; preds = %35
%45 = load i32*, i32** %6
%46 = call i32 @bufToInt32(i32* %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %46)
br label %48
48: ; preds = %44, %35
%49 = call i32 @skipStructEntry(i32** %5)
%50 = add nuw nsw i32 %37, 1
%51 = icmp eq i32 %50, %28
br i1 %51, label %34, label %35
}