#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 scalar_t__ uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
size_t fread(scalar_t__ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ memcmp(scalar_t__ *, scalar_t__ const *, int);
uint32_t find_chunk(FILE *file, const uint8_t chunk_id[4]) {
  uint8_t buffer[8];
  while (1) {
    size_t chunksize;
    size_t s = fread(buffer, 1, 8, file);
    if (s < 8)
      return 0;
    chunksize = (uint32_t)buffer[4] | ((uint32_t)buffer[5] << 8) |
                ((uint32_t)buffer[6] << 16) | ((uint32_t)buffer[7] << 24);
    if (!memcmp(buffer, chunk_id, 4))
      return chunksize;
    fseek(file, chunksize, SEEK_CUR);
  }
}


x86 assembly (clang, -O0):
<find_chunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1030 <fread@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x8,-0x70(%rbp)
jae    1172 <find_chunk+0x42>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <find_chunk+0xa9>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x30(%rbp),%rcx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x28(%rbp),%rcx
shl    $0x18,%ecx
or     %ecx,%eax
cltq
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    11be <find_chunk+0x8e>
mov    -0x68(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11d9 <find_chunk+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
jmp    1140 <find_chunk+0x10>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_chunk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %r15d,%r15d
cmp    $0x8,%rax
jb     11d0 <find_chunk+0xa0>
mov    %rsp,%r13
mov    0x2e7c(%rip),%r12
nopl   0x0(%rax)
mov    0x28(%rsp),%eax
mov    0x30(%rsp),%ecx
shl    $0x8,%eax
or     0x20(%rsp),%eax
shl    $0x10,%ecx
mov    0x38(%rsp),%ebp
shl    $0x18,%ebp
or     %ecx,%ebp
or     %eax,%ebp
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
test   %eax,%eax
je     11cd <find_chunk+0x9d>
movslq %ebp,%rsi
mov    (%r12),%edx
mov    %rbx,%rdi
call   1050 <fseek@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x7,%rax
ja     1170 <find_chunk+0x40>
jmp    11d0 <find_chunk+0xa0>
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
define i32 @find_chunk(i32* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64*
%6 = alloca [8 x i64]
%7 = alloca i64
%8 = alloca i64
store i32* %0, i32** %4
store i64* %1, i64** %5
br label %9
9: ; preds = %2, %43
%10 = getelementptr inbounds [8 x i64], [8 x i64]* %6, i64 0, i64 0
%11 = load i32*, i32** %4
%12 = call i64 @fread(i64* %10, i32 1, i32 8, i32* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp ult i64 %13, 8
br i1 %14, label %15, label %16
15: ; preds = %9
store i32 0, i32* %3
br label %48
16: ; preds = %9
%17 = getelementptr inbounds [8 x i64], [8 x i64]* %6, i64 0, i64 4
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds [8 x i64], [8 x i64]* %6, i64 0, i64 5
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
%23 = shl i32 %22, 8
%24 = or i32 %19, %23
%25 = getelementptr inbounds [8 x i64], [8 x i64]* %6, i64 0, i64 6
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = shl i32 %27, 16
%29 = or i32 %24, %28
%30 = getelementptr inbounds [8 x i64], [8 x i64]* %6, i64 0, i64 7
%31 = load i64, i64* %30
%32 = trunc i64 %31 to i32
%33 = shl i32 %32, 24
%34 = or i32 %29, %33
%35 = sext i32 %34 to i64
store i64 %35, i64* %7
%36 = getelementptr inbounds [8 x i64], [8 x i64]* %6, i64 0, i64 0
%37 = load i64*, i64** %5
%38 = call i32 @memcmp(i64* %36, i64* %37, i32 4)
%39 = icmp ne i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %16
%41 = load i64, i64* %7
%42 = trunc i64 %41 to i32
store i32 %42, i32* %3
br label %48
43: ; preds = %16
%44 = load i32*, i32** %4
%45 = load i64, i64* %7
%46 = load i32, i32* @SEEK_CUR
%47 = call i32 @fseek(i32* %44, i64 %45, i32 %46)
br label %9
48: ; preds = %40, %15
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
define i32 @find_chunk(i32* nocapture %0, i64* %1) {
%3 = alloca [8 x i64]
%4 = bitcast [8 x i64]* %3 to i8*
%5 = getelementptr inbounds [8 x i64], [8 x i64]* %3, i64 0, i64 4
%6 = getelementptr inbounds [8 x i64], [8 x i64]* %3, i64 0, i64 0
%7 = call i64 @fread(i64* %6, i32 1, i32 8, i32* %0)
%8 = icmp ult i64 %7, 8
br i1 %8, label %36, label %9
9: ; preds = %2
%10 = getelementptr inbounds [8 x i64], [8 x i64]* %3, i64 0, i64 5
%11 = getelementptr inbounds [8 x i64], [8 x i64]* %3, i64 0, i64 6
%12 = getelementptr inbounds [8 x i64], [8 x i64]* %3, i64 0, i64 7
br label %13
13: ; preds = %9, %30
%14 = load i64, i64* %5
%15 = trunc i64 %14 to i32
%16 = load i64, i64* %10
%17 = trunc i64 %16 to i32
%18 = shl i32 %17, 8
%19 = or i32 %18, %15
%20 = load i64, i64* %11
%21 = trunc i64 %20 to i32
%22 = shl i32 %21, 16
%23 = or i32 %19, %22
%24 = load i64, i64* %12
%25 = trunc i64 %24 to i32
%26 = shl i32 %25, 24
%27 = or i32 %23, %26
%28 = call i32 @memcmp(i64* %6, i64* %1, i32 4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %13
%31 = sext i32 %27 to i64
%32 = load i32, i32* @SEEK_CUR
%33 = call i32 @fseek(i32* %0, i64 %31, i32 %32)
%34 = call i64 @fread(i64* %6, i32 1, i32 8, i32* %0)
%35 = icmp ult i64 %34, 8
br i1 %35, label %36, label %13
36: ; preds = %13, %30, %2
%37 = phi i32 [ 0, %2 ], [ 0, %30 ], [ %27, %13 ]
ret i32 %37
}