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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ wav_get_header(void *, int *, int *, int *, int *,
                                  unsigned int *);
int /*<<< orphan*/ wav_read_close(void *);
void *wav_read_open(char const *);
int wav_duration(const char *fileName) {
  void *wav = wav_read_open(fileName);
  if (0 == wav) {
    return 0;
  }
  int format, channels, sample_rate, bits_per_sample;
  unsigned int data_length;
  wav_get_header(wav, &format, &channels, &sample_rate, &bits_per_sample,
                 &data_length);
  wav_read_close(wav);
  int bytes_per_frame = bits_per_sample / 8 * channels;
  int bytes_per_sec = bytes_per_frame * sample_rate;
  assert(bytes_per_sec > 0);
  if (0 == bytes_per_sec) {
    return 0;
  }
  int sec = 1000 * data_length / bytes_per_sec;
  return sec;
}


x86 assembly (clang, -O0):
<wav_duration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <wav_read_open@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1171 <wav_duration+0x31>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <wav_duration+0xb0>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%r9
call   1040 <wav_get_header@plt>
mov    -0x18(%rbp),%rdi
call   1060 <wav_read_close@plt>
mov    -0x28(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
imul   -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
imul   -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
jne    11db <wav_duration+0x9b>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <wav_duration+0xb0>
imul   $0x3e8,-0x2c(%rbp),%eax
xor    %edx,%edx
divl   -0x34(%rbp)
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wav_duration>:
push   %rbx
sub    $0x20,%rsp
call   1030 <wav_read_open@plt>
test   %rax,%rax
je     11b3 <wav_duration+0x73>
mov    %rax,%rbx
lea    0x1c(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %rax,%rdi
call   1040 <wav_get_header@plt>
mov    %rbx,%rdi
call   1060 <wav_read_close@plt>
mov    0x10(%rsp),%eax
lea    0x7(%rax),%ebx
test   %eax,%eax
cmovns %eax,%ebx
sar    $0x3,%ebx
imul   0x18(%rsp),%ebx
imul   0x14(%rsp),%ebx
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1050 <assert@plt>
test   %ebx,%ebx
je     11b3 <wav_duration+0x73>
imul   $0x3e8,0xc(%rsp),%eax
xor    %edx,%edx
div    %ebx
jmp    11b5 <wav_duration+0x75>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @wav_duration(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
%13 = load i8*, i8** %3
%14 = call i8* @wav_read_open(i8* %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp eq i8* null, %15
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
br label %43
18: ; preds = %1
%19 = load i8*, i8** %4
%20 = call i32 @wav_get_header(i8* %19, i32* %5, i32* %6, i32* %7, i32* %8, i32* %9)
%21 = load i8*, i8** %4
%22 = call i32 @wav_read_close(i8* %21)
%23 = load i32, i32* %8
%24 = sdiv i32 %23, 8
%25 = load i32, i32* %6
%26 = mul nsw i32 %24, %25
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = load i32, i32* %7
%29 = mul nsw i32 %27, %28
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = icmp sgt i32 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %11
%35 = icmp eq i32 0, %34
br i1 %35, label %36, label %37
36: ; preds = %18
store i32 0, i32* %2
br label %43
37: ; preds = %18
%38 = load i32, i32* %9
%39 = mul i32 1000, %38
%40 = load i32, i32* %11
%41 = udiv i32 %39, %40
store i32 %41, i32* %12
%42 = load i32, i32* %12
store i32 %42, i32* %2
br label %43
43: ; preds = %37, %36, %17
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
define i32 @wav_duration(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = tail call i8* @wav_read_open(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %33, label %9
9: ; preds = %1
%10 = bitcast i32* %2 to i8*
%11 = bitcast i32* %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast i32* %6 to i8*
%15 = call i32 @wav_get_header(i8* %7, i32* %2, i32* %3, i32* %4, i32* %5, i32* %6)
%16 = call i32 @wav_read_close(i8* %7)
%17 = load i32, i32* %5
%18 = sdiv i32 %17, 8
%19 = load i32, i32* %3
%20 = mul nsw i32 %18, %19
%21 = load i32, i32* %4
%22 = mul nsw i32 %20, %21
%23 = icmp sgt i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = icmp eq i32 %22, 0
br i1 %26, label %31, label %27
27: ; preds = %9
%28 = load i32, i32* %6
%29 = mul i32 %28, 1000
%30 = udiv i32 %29, %22
br label %31
31: ; preds = %9, %27
%32 = phi i32 [ %30, %27 ], [ 0, %9 ]
br label %33
33: ; preds = %1, %31
%34 = phi i32 [ %32, %31 ], [ 0, %1 ]
ret i32 %34
}