#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 */
scalar_t__ ReadLong(char *);
scalar_t__ strncmp(char *, char *, int);
char *findchunk(char *data, char *string, int length) {
  char *mydata = data + 12;
  while (1) {
    if (strncmp((char *)mydata, string, 4) == 0)
      return mydata + 8;
    mydata += ReadLong(mydata + 4) + 8;
    if ((mydata - data) >= length)
      break;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<findchunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    116c <findchunk+0x4c>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
jmp    11af <findchunk+0x8f>
mov    -0x28(%rbp),%rdi
add    $0x4,%rdi
call   1040 <ReadLong@plt>
add    $0x8,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jl     11a2 <findchunk+0x82>
jmp    11a7 <findchunk+0x87>
jmp    113f <findchunk+0x1f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findchunk>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xc(%rdi),%rbx
movslq %edx,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1174 <findchunk+0x54>
lea    0x4(%rbx),%rdi
call   1040 <ReadLong@plt>
add    %rax,%rbx
add    $0x8,%rbx
mov    %rbx,%rax
sub    %r15,%rax
cmp    %r12,%rax
jl     1140 <findchunk+0x20>
xor    %ebx,%ebx
jmp    1178 <findchunk+0x58>
add    $0x8,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @findchunk(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = getelementptr inbounds i8, i8* %9, i64 12
store i8* %10, i8** %8
br label %11
11: ; preds = %3, %35
%12 = load i8*, i8** %8
%13 = load i8*, i8** %6
%14 = call i64 @strncmp(i8* %12, i8* %13, i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 8
store i8* %18, i8** %4
br label %37
19: ; preds = %11
%20 = load i8*, i8** %8
%21 = getelementptr inbounds i8, i8* %20, i64 4
%22 = call i64 @ReadLong(i8* %21)
%23 = add nsw i64 %22, 8
%24 = load i8*, i8** %8
%25 = getelementptr inbounds i8, i8* %24, i64 %23
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = load i8*, i8** %5
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = icmp sge i64 %30, %32
br i1 %33, label %34, label %35
34: ; preds = %19
br label %36
35: ; preds = %19
br label %11
36: ; preds = %34
store i8* null, i8** %4
br label %37
37: ; preds = %36, %16
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
define i8* @findchunk(i8* %0, i8* %1, i32 %2) {
%4 = getelementptr inbounds i8, i8* %0, i64 12
%5 = ptrtoint i8* %0 to i64
%6 = sext i32 %2 to i64
br label %7
7: ; preds = %13, %3
%8 = phi i8* [ %4, %3 ], [ %17, %13 ]
%9 = tail call i64 @strncmp(i8* %8, i8* %1, i32 4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %8, i64 8
br label %21
13: ; preds = %7
%14 = getelementptr inbounds i8, i8* %8, i64 4
%15 = tail call i64 @ReadLong(i8* %14)
%16 = add nsw i64 %15, 8
%17 = getelementptr inbounds i8, i8* %8, i64 %16
%18 = ptrtoint i8* %17 to i64
%19 = sub i64 %18, %5
%20 = icmp slt i64 %19, %6
br i1 %20, label %7, label %21
21: ; preds = %13, %11
%22 = phi i8* [ %12, %11 ], [ null, %13 ]
ret i8* %22
}