#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, ...);
scalar_t__ strncmp(char *, char *, int);
uint32_t ogg_track_length_millis(char *filename) {
  uint8_t segments;
  int32_t audio_sample_rate;
  int64_t granule;
  float samples_per_milli;
  uint32_t millis;
  char buf[4];
  int fd;
  fd = open(filename, O_RDONLY);
  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;
  close(fd);
  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
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1187 <ogg_track_length_millis+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <ogg_track_length_millis+0x1ba>
mov    -0x34(%rbp),%edi
mov    0x2e37(%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   1060 <read@plt>
mov    -0x34(%rbp),%edi
mov    -0x14(%rbp),%esi
add    $0x7,%esi
add    $0x5,%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1060 <read@plt>
mov    -0x34(%rbp),%edi
mov    0x2e01(%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   1060 <read@plt>
mov    -0x34(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffb,%esi
call   1040 <lseek@plt>
lea    -0x30(%rbp),%rsi
lea    0xde1(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12a8 <ogg_track_length_millis+0x158>
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   1060 <read@plt>
mov    -0x34(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x1,%eax
jle    1297 <ogg_track_length_millis+0x147>
mov    -0x34(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1040 <lseek@plt>
jmp    12a3 <ogg_track_length_millis+0x153>
movl   $0xfffffffe,-0x4(%rbp)
jmp    130a <ogg_track_length_millis+0x1ba>
jmp    121c <ogg_track_length_millis+0xcc>
mov    -0x34(%rbp),%edi
mov    0x2d2e(%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   1060 <read@plt>
movsd  -0x20(%rbp),%xmm0
movsd  0xd22(%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    -0x34(%rbp),%edi
call   1050 <close@plt>
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
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12d8 <ogg_track_length_millis+0x188>
mov    %eax,%ebx
mov    0x2e4c(%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   1060 <read@plt>
mov    0x14(%rsp),%esi
add    $0xc,%esi
mov    0x2e35(%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   1060 <read@plt>
mov    0x2e18(%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   1060 <read@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0xfffffffb,%esi
call   1040 <lseek@plt>
lea    0xdfd(%rip),%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     128b <ogg_track_length_millis+0x13b>
lea    0xc(%rsp),%rbp
mov    $0xfffffffe,%r14d
lea    0xdd9(%rip),%r15
nop
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   1060 <read@plt>
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x2,%eax
jl     12de <ogg_track_length_millis+0x18e>
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    1230 <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   1060 <read@plt>
movsd  0x18(%rsp),%xmm0
divsd  0xd44(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  0x10(%rsp),%xmm1
divss  %xmm0,%xmm1
cvttss2si %xmm1,%r14d
mov    %ebx,%edi
call   1050 <close@plt>
jmp    12de <ogg_track_length_millis+0x18e>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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