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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
size_t fread(size_t *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
size_t findChunk(uint32_t chunkMark, FILE *file, char *fileName) {
  size_t chunkLength = 0;
  for (size_t ioStatus = 1; ioStatus > 0;) {
    uint32_t chunkHead[2];
    ioStatus = fread(chunkHead, 1, 4 * 2, file);
    if (ioStatus < 4 * 2) {
      printf("%s: Can't find \"%.4s\" chunk.\n", fileName, (char *)&chunkMark);
      break;
    }
    if (chunkHead[0] == chunkMark) {
      chunkLength = chunkHead[1];
      return chunkLength;
    }
    if (fseek(file, chunkHead[1], SEEK_CUR)) {
      printf("%s: Can't read enough of file.\n", fileName);
      break;
    }
  }
  return chunkLength;
}


x86 assembly (clang, -O0):
<findChunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x1,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11ff <findChunk+0xcf>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x8,-0x30(%rbp)
jae    11a0 <findChunk+0x70>
mov    -0x20(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ff <findChunk+0xcf>
mov    -0x40(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c3 <findChunk+0x93>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1207 <findChunk+0xd7>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
je     11fa <findChunk+0xca>
mov    -0x20(%rbp),%rsi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ff <findChunk+0xcf>
jmp    1154 <findChunk+0x24>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findChunk>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rdi,0x18(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x8,%rax
jb     11a7 <findChunk+0x77>
mov    0x2e7b(%rip),%r13
mov    %rsp,%r12
mov    0x8(%rsp),%rsi
cmp    %r15,(%rsp)
je     11d2 <findChunk+0xa2>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
call   1050 <fseek@plt>
test   %rax,%rax
jne    11bf <findChunk+0x8f>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x8,%rax
jae    1170 <findChunk+0x40>
lea    0xe52(%rip),%rdi
lea    0x18(%rsp),%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d0 <findChunk+0xa0>
lea    0xe58(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %esi,%esi
mov    %rsi,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [30 x i8] c"%s: Can't find \22%.4s\22 chunk.\0A\00"
@SEEK_CUR = global i32 0
@.str.1 = [32 x i8] c"%s: Can't read enough of file.\0A\00"
define i64 @findChunk(i64 %0, i32* %1, i8* %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca [2 x i64]
store i64 %0, i64* %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i64 0, i64* %8
store i64 1, i64* %9
br label %11
11: ; preds = %43, %3
%12 = load i64, i64* %9
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %44
14: ; preds = %11
%15 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 0
%16 = load i32*, i32** %6
%17 = call i64 @fread(i64* %15, i32 1, i32 8, i32* %16)
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = icmp ult i64 %18, 8
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i8*, i8** %7
%22 = bitcast i64* %5 to i8*
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %22)
br label %44
24: ; preds = %14
%25 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 0
%26 = load i64, i64* %25
%27 = load i64, i64* %5
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
%31 = load i64, i64* %30
store i64 %31, i64* %8
%32 = load i64, i64* %8
store i64 %32, i64* %4
br label %46
33: ; preds = %24
%34 = load i32*, i32** %6
%35 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
%36 = load i64, i64* %35
%37 = load i32, i32* @SEEK_CUR
%38 = call i64 @fseek(i32* %34, i64 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i8*, i8** %7
%42 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %44
43: ; preds = %33
br label %11
44: ; preds = %40, %20, %11
%45 = load i64, i64* %8
store i64 %45, i64* %4
br label %46
46: ; preds = %44, %29
%47 = load i64, i64* %4
ret i64 %47
}

LLVM IR (-O2):
@.str = [30 x i8] c"%s: Can't find \22%.4s\22 chunk.\0A\00"
@SEEK_CUR = global i32 0
@.str.1 = [32 x i8] c"%s: Can't read enough of file.\0A\00"
define i64 @findChunk(i64 %0, i32* nocapture %1, i8* %2) {
%4 = alloca i64
%5 = alloca [2 x i64]
store i64 %0, i64* %4
%6 = bitcast [2 x i64]* %5 to i8*
%7 = getelementptr inbounds [2 x i64], [2 x i64]* %5, i64 0, i64 0
%8 = call i64 @fread(i64* %7, i32 1, i32 8, i32* %1)
%9 = icmp ult i64 %8, 8
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = getelementptr inbounds [2 x i64], [2 x i64]* %5, i64 0, i64 1
br label %14
12: ; preds = %24, %3
%13 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %2, i64* %4)
br label %27
14: ; preds = %10, %24
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, %0
%17 = load i64, i64* %11
br i1 %16, label %28, label %18
18: ; preds = %14
%19 = load i32, i32* @SEEK_CUR
%20 = tail call i64 @fseek(i32* %1, i64 %17, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %27
24: ; preds = %18
%25 = call i64 @fread(i64* %7, i32 1, i32 8, i32* %1)
%26 = icmp ult i64 %25, 8
br i1 %26, label %12, label %14
27: ; preds = %22, %12
br label %29
28: ; preds = %14
br label %29
29: ; preds = %27, %28
%30 = phi i64 [ %17, %28 ], [ 0, %27 ]
ret i64 %30
}