#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 uint8_t;
typedef int uint32_t;
typedef float int64_t;
typedef double int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int fileno(int /*<<< orphan*/ *);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, ...);
scalar_t__ strncmp(char *, char *, int);
uint32_t ogg_track_length_millis(FILE *fp) {
  uint8_t segments;
  int32_t audio_sample_rate;
  int64_t granule;
  float samples_per_milli;
  uint32_t millis;
  char buf[4];
  int fd;
  fd = fileno(fp);
  if (fd == -1) {
    return -1;
  }
  lseek(fd, 26, SEEK_SET);
  read(fd, &segments, 1);
  lseek(fd, segments + 7 + 5, SEEK_CUR);
  read(fd, &audio_sample_rate, 4);
  lseek(fd, -4, SEEK_END);
  read(fd, buf, 4);
  lseek(fd, -5, SEEK_END);
  while (strncmp("OggS", buf, 4) != 0) {
    buf[3] = buf[2];
    buf[2] = buf[1];
    buf[1] = buf[0];
    read(fd, &buf[0], 1);
    if (lseek(fd, 0, SEEK_CUR) > 1) {
      lseek(fd, -2, SEEK_CUR);
    } else {
      return -2;
    }
  }
  lseek(fd, 6 + 1, SEEK_CUR);
  read(fd, &granule, 8);
  samples_per_milli = audio_sample_rate / 1000.0;
  millis = granule / samples_per_milli;
  return millis;
}


x86 assembly (clang, -O0):
<ogg_track_length_millis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    116e <ogg_track_length_millis+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <ogg_track_length_millis+0x1a9>
mov    -0x34(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    $0x1a,%esi
call   1040 <lseek@plt>
mov    -0x34(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    -0x34(%rbp),%edi
mov    -0x14(%rbp),%esi
add    $0x7,%esi
add    $0x5,%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    -0x34(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffc,%esi
call   1040 <lseek@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    -0x34(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffb,%esi
call   1040 <lseek@plt>
lea    -0x30(%rbp),%rsi
lea    0xdfa(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     128f <ogg_track_length_millis+0x14f>
mov    -0x2e(%rbp),%al
mov    %al,-0x2d(%rbp)
mov    -0x2f(%rbp),%al
mov    %al,-0x2e(%rbp)
mov    -0x30(%rbp),%al
mov    %al,-0x2f(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    -0x34(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x1,%eax
jle    127e <ogg_track_length_millis+0x13e>
mov    -0x34(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1040 <lseek@plt>
jmp    128a <ogg_track_length_millis+0x14a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12e9 <ogg_track_length_millis+0x1a9>
jmp    1203 <ogg_track_length_millis+0xc3>
mov    -0x34(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
mov    $0x7,%esi
call   1040 <lseek@plt>
mov    -0x34(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1050 <read@plt>
movsd  -0x20(%rbp),%xmm0
movsd  0xd3b(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x28(%rbp)
movss  -0x24(%rbp),%xmm0
divss  -0x28(%rbp),%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ogg_track_length_millis>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
call   1060 <fileno@plt>
cmp    $0xffffffff,%eax
je     12c1 <ogg_track_length_millis+0x181>
mov    %eax,%ebx
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1a,%esi
call   1040 <lseek@plt>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <read@plt>
mov    0x14(%rsp),%esi
add    $0xc,%esi
mov    0x2e4e(%rip),%r12
mov    (%r12),%edx
mov    %ebx,%edi
call   1040 <lseek@plt>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
mov    0x2e31(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0xfffffffc,%esi
call   1040 <lseek@plt>
lea    0xc(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0xfffffffb,%esi
call   1040 <lseek@plt>
lea    0xe16(%rip),%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     127b <ogg_track_length_millis+0x13b>
lea    0xc(%rsp),%rbp
mov    $0xfffffffe,%r14d
lea    0xdf2(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
movzbl 0xe(%rsp),%eax
mov    %al,0xf(%rsp)
movzwl 0xc(%rsp),%eax
mov    %ax,0xd(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <read@plt>
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x2,%eax
jl     12c7 <ogg_track_length_millis+0x187>
mov    (%r12),%edx
mov    %ebx,%edi
mov    $0xfffffffe,%esi
call   1040 <lseek@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1220 <ogg_track_length_millis+0xe0>
mov    (%r12),%edx
mov    %ebx,%edi
mov    $0x7,%esi
call   1040 <lseek@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
xor    %eax,%eax
call   1050 <read@plt>
movsd  0x18(%rsp),%xmm0
divsd  0xd54(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  0x10(%rsp),%xmm1
divss  %xmm0,%xmm1
cvttss2si %xmm1,%r14d
jmp    12c7 <ogg_track_length_millis+0x187>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@.str = [5 x i8] c"OggS\00"
define i32 @ogg_track_length_millis(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca double
%6 = alloca float
%7 = alloca float
%8 = alloca i32
%9 = alloca [4 x i8]
%10 = alloca i32
store i32* %0, i32** %3
%11 = load i32*, i32** %3
%12 = call i32 @fileno(i32* %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %80
16: ; preds = %1
%17 = load i32, i32* %10
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @lseek(i32 %17, i32 26, i32 %18)
%20 = load i32, i32* %10
%21 = call i32 (i32, ...) @read(i32 %20, i32* %4, i32 1)
%22 = load i32, i32* %10
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 7
%25 = add nsw i32 %24, 5
%26 = load i32, i32* @SEEK_CUR
%27 = call i32 @lseek(i32 %22, i32 %25, i32 %26)
%28 = load i32, i32* %10
%29 = call i32 (i32, ...) @read(i32 %28, double* %5, i32 4)
%30 = load i32, i32* %10
%31 = load i32, i32* @SEEK_END
%32 = call i32 @lseek(i32 %30, i32 -4, i32 %31)
%33 = load i32, i32* %10
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%35 = call i32 (i32, ...) @read(i32 %33, i8* %34, i32 4)
%36 = load i32, i32* %10
%37 = load i32, i32* @SEEK_END
%38 = call i32 @lseek(i32 %36, i32 -5, i32 %37)
br label %39
39: ; preds = %65, %16
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%41 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %40, i32 4)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %66
43: ; preds = %39
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
%45 = load i8, i8* %44
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %45, i8* %46
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
%48 = load i8, i8* %47
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %48, i8* %49
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%51 = load i8, i8* %50
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %51, i8* %52
%53 = load i32, i32* %10
%54 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%55 = call i32 (i32, ...) @read(i32 %53, i8* %54, i32 1)
%56 = load i32, i32* %10
%57 = load i32, i32* @SEEK_CUR
%58 = call i32 @lseek(i32 %56, i32 0, i32 %57)
%59 = icmp sgt i32 %58, 1
br i1 %59, label %60, label %64
60: ; preds = %43
%61 = load i32, i32* %10
%62 = load i32, i32* @SEEK_CUR
%63 = call i32 @lseek(i32 %61, i32 -2, i32 %62)
br label %65
64: ; preds = %43
store i32 -2, i32* %2
br label %80
65: ; preds = %60
br label %39
66: ; preds = %39
%67 = load i32, i32* %10
%68 = load i32, i32* @SEEK_CUR
%69 = call i32 @lseek(i32 %67, i32 7, i32 %68)
%70 = load i32, i32* %10
%71 = call i32 (i32, ...) @read(i32 %70, float* %6, i32 8)
%72 = load double, double* %5
%73 = fdiv double %72, 1.000000e+03
%74 = fptrunc double %73 to float
store float %74, float* %7
%75 = load float, float* %6
%76 = load float, float* %7
%77 = fdiv float %75, %76
%78 = fptosi float %77 to i32
store i32 %78, i32* %8
%79 = load i32, i32* %8
store i32 %79, i32* %2
br label %80
80: ; preds = %66, %64, %15
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@.str = [5 x i8] c"OggS\00"
define i32 @ogg_track_length_millis(i32* nocapture %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca float
%5 = alloca [4 x i8]
%6 = bitcast i32* %2 to i8*
%7 = bitcast double* %3 to i8*
%8 = bitcast float* %4 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%10 = tail call i32 @fileno(i32* %0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %55, label %12
12: ; preds = %1
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %10, i32 26, i32 %13)
%15 = call i32 (i32, ...) @read(i32 %10, i32* %2, i32 1)
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 12
%18 = load i32, i32* @SEEK_CUR
%19 = call i32 @lseek(i32 %10, i32 %17, i32 %18)
%20 = call i32 (i32, ...) @read(i32 %10, double* %3, i32 4)
%21 = load i32, i32* @SEEK_END
%22 = call i32 @lseek(i32 %10, i32 -4, i32 %21)
%23 = call i32 (i32, ...) @read(i32 %10, i8* %9, i32 4)
%24 = load i32, i32* @SEEK_END
%25 = call i32 @lseek(i32 %10, i32 -5, i32 %24)
%26 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %9, i32 4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %45, label %28
28: ; preds = %12
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
br label %32
32: ; preds = %28, %40
%33 = load i8, i8* %29
store i8 %33, i8* %30
%34 = load i8, i8* %31
store i8 %34, i8* %29
%35 = load i8, i8* %9
store i8 %35, i8* %31
%36 = call i32 (i32, ...) @read(i32 %10, i8* %9, i32 1)
%37 = load i32, i32* @SEEK_CUR
%38 = call i32 @lseek(i32 %10, i32 0, i32 %37)
%39 = icmp sgt i32 %38, 1
br i1 %39, label %40, label %55
40: ; preds = %32
%41 = load i32, i32* @SEEK_CUR
%42 = call i32 @lseek(i32 %10, i32 -2, i32 %41)
%43 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %9, i32 4)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %32
45: ; preds = %40, %12
%46 = load i32, i32* @SEEK_CUR
%47 = call i32 @lseek(i32 %10, i32 7, i32 %46)
%48 = call i32 (i32, ...) @read(i32 %10, float* %4, i32 8)
%49 = load double, double* %3
%50 = fdiv double %49, 1.000000e+03
%51 = fptrunc double %50 to float
%52 = load float, float* %4
%53 = fdiv float %52, %51
%54 = fptosi float %53 to i32
br label %55
55: ; preds = %32, %1, %45
%56 = phi i32 [ %54, %45 ], [ -1, %1 ], [ -2, %32 ]
ret i32 %56
}