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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int find_meta_start(FILE *fr) {
  char buf[4];
  char page_segments;
  int page_len;
  int i;
  fread(buf, 1, 4, fr);
  if (strncmp(buf, "OggS", 4) != 0) {
    return -1;
  }
  fseek(fr, 26, SEEK_SET);
  fread(&page_segments, 1, 1, fr);
  page_len = 0;
  for (i = 0; i < page_segments; i++) {
    fread(buf, 1, 1, fr);
    page_len += buf[0];
  }
  fseek(fr, 27 + page_segments + page_len, SEEK_SET);
  fread(buf, 1, 4, fr);
  if (strncmp(buf, "OggS", 4) != 0) {
    return -2;
  }
  return 27 + page_segments + page_len;
}


x86 assembly (clang, -O0):
<find_meta_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <fread@plt>
lea    -0x14(%rbp),%rdi
lea    0xea2(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     117e <find_meta_start+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <find_meta_start+0x12d>
mov    -0x10(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    $0x1a,%esi
call   1050 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    -0x15(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
jge    11f2 <find_meta_start+0xc2>
lea    -0x14(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
movsbl -0x14(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11b7 <find_meta_start+0x87>
mov    -0x10(%rbp),%rdi
movsbl -0x15(%rbp),%esi
add    $0x1b,%esi
add    -0x1c(%rbp),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
lea    -0x14(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <fread@plt>
lea    -0x14(%rbp),%rdi
lea    0xdd0(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1250 <find_meta_start+0x120>
movl   $0xfffffffe,-0x4(%rbp)
jmp    125d <find_meta_start+0x12d>
movsbl -0x15(%rbp),%eax
add    $0x1b,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_meta_start>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
lea    0xea1(%rip),%rsi
mov    %rbp,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1234 <find_meta_start+0x104>
mov    0x2e5c(%rip),%r15
mov    (%r15),%edx
mov    %r13,%rdi
mov    $0x1a,%esi
call   1050 <fseek@plt>
lea    0x3(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
movsbl 0x3(%rsp),%r12d
test   %r12d,%r12d
jle    11e6 <find_meta_start+0xb6>
xor    %ebp,%ebp
lea    0x4(%rsp),%r14
mov    %r12d,%ebx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
movsbl 0x4(%rsp),%eax
add    %eax,%ebp
add    $0xffffffff,%ebx
jne    11c0 <find_meta_start+0x90>
add    $0x1b,%ebp
jmp    11eb <find_meta_start+0xbb>
mov    $0x1b,%ebp
add    %r12d,%ebp
mov    (%r15),%edx
mov    %r13,%rdi
mov    %ebp,%esi
call   1050 <fseek@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
lea    0xde4(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
mov    $0xfffffffe,%eax
cmove  %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"OggS\00"
@SEEK_SET = global i32 0
define i32 @find_meta_start(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [4 x i8]
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%9 = load i32*, i32** %3
%10 = call i32 @fread(i8* %8, i32 1, i32 4, i32* %9)
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%12 = call i64 @strncmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %60
15: ; preds = %1
%16 = load i32*, i32** %3
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %16, i32 26, i32 %17)
%19 = load i32*, i32** %3
%20 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %19)
store i32 0, i32* %6
store i32 0, i32* %7
br label %21
21: ; preds = %35, %15
%22 = load i32, i32* %7
%23 = load i8, i8* %5
%24 = sext i8 %23 to i32
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %38
26: ; preds = %21
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%28 = load i32*, i32** %3
%29 = call i32 @fread(i8* %27, i32 1, i32 1, i32* %28)
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = load i32, i32* %6
%34 = add nsw i32 %33, %32
store i32 %34, i32* %6
br label %35
35: ; preds = %26
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %21
38: ; preds = %21
%39 = load i32*, i32** %3
%40 = load i8, i8* %5
%41 = sext i8 %40 to i32
%42 = add nsw i32 27, %41
%43 = load i32, i32* %6
%44 = add nsw i32 %42, %43
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %39, i32 %44, i32 %45)
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%48 = load i32*, i32** %3
%49 = call i32 @fread(i8* %47, i32 1, i32 4, i32* %48)
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%51 = call i64 @strncmp(i8* %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %38
store i32 -2, i32* %2
br label %60
54: ; preds = %38
%55 = load i8, i8* %5
%56 = sext i8 %55 to i32
%57 = add nsw i32 27, %56
%58 = load i32, i32* %6
%59 = add nsw i32 %57, %58
store i32 %59, i32* %2
br label %60
60: ; preds = %54, %53, %14
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@.str = [5 x i8] c"OggS\00"
@SEEK_SET = global i32 0
define i32 @find_meta_start(i32* nocapture %0) {
%2 = alloca [4 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = call i32 @fread(i8* %4, i32 1, i32 4, i32* %0)
%6 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %35
8: ; preds = %1
%9 = load i32, i32* @SEEK_SET
%10 = call i32 @fseek(i32* %0, i32 26, i32 %9)
%11 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %0)
%12 = load i8, i8* %3
%13 = sext i8 %12 to i32
%14 = icmp sgt i8 %12, 0
br i1 %14, label %15, label %26
15: ; preds = %8, %15
%16 = phi i32 [ %22, %15 ], [ 0, %8 ]
%17 = phi i32 [ %21, %15 ], [ 0, %8 ]
%18 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %0)
%19 = load i8, i8* %4
%20 = sext i8 %19 to i32
%21 = add nsw i32 %17, %20
%22 = add nuw nsw i32 %16, 1
%23 = icmp eq i32 %22, %13
br i1 %23, label %24, label %15
24: ; preds = %15
%25 = add i32 %21, 27
br label %26
26: ; preds = %24, %8
%27 = phi i32 [ 27, %8 ], [ %25, %24 ]
%28 = add i32 %27, %13
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %0, i32 %28, i32 %29)
%31 = call i32 @fread(i8* %4, i32 1, i32 4, i32* %0)
%32 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%33 = icmp eq i64 %32, 0
%34 = select i1 %33, i32 %28, i32 -2
br label %35
35: ; preds = %26, %1
%36 = phi i32 [ -1, %1 ], [ %34, %26 ]
ret i32 %36
}