#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*/ acc(float *, float *, long);
long atol(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
size_t fread(float *, int, long, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(float *, float *, long);
int /*<<< orphan*/ norm(float *, long, long);
int /*<<< orphan*/ printf(char *, ...);
long quantise(float *, float *, long, long, float *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ zero(float *, long);
int main(int argc, char *argv[]) {
  long k, m;
  float *vec;
  float *cb;
  float *cent;
  long *n;
  long J;
  long i, j;
  long ind;
  float se;
  float Dn, Dn_1;
  float delta;
  FILE *ftrain;
  FILE *fvq;
  if (argc != 5) {
    printf("usage: %s TrainFile K(dimension) M(codebook size) VQFile\n",
           argv[0]);
    exit(1);
  }
  ftrain = fopen(argv[1], "rb");
  if (ftrain == NULL) {
    printf("Error opening training database file: %s\n", argv[1]);
    exit(1);
  }
  k = atol(argv[2]);
  m = atol(argv[3]);
  printf("dimension K=%ld  number of entries M=%ld\n", k, m);
  vec = (float *)malloc(sizeof(float) * k);
  cb = (float *)malloc(sizeof(float) * k * m);
  cent = (float *)malloc(sizeof(float) * k * m);
  n = (long *)malloc(sizeof(long) * m);
  if (cb == NULL || cb == NULL || cent == NULL || vec == NULL) {
    printf("Error in malloc.\n");
    exit(1);
  }
  J = 0;
  while (fread(vec, sizeof(float), k, ftrain) == (size_t)k)
    J++;
  printf("J=%ld entries in training set\n", J);
  rewind(ftrain);
  fread(cb, sizeof(float), k * m, ftrain);
  Dn = 1E32;
  j = 1;
  do {
    Dn_1 = Dn;
    for (i = 0; i < m; i++) {
      zero(&cent[i * k], k);
      n[i] = 0;
    }
    se = 0.0;
    rewind(ftrain);
    for (i = 0; i < J; i++) {
      fread(vec, sizeof(float), k, ftrain);
      ind = quantise(cb, vec, k, m, &se);
      n[ind]++;
      acc(&cent[ind * k], vec, k);
    }
    Dn = se / J;
    delta = (Dn_1 - Dn) / Dn;
    printf("\r  Iteration %ld, Dn = %f, Delta = %e\n", j, Dn, delta);
    j++;
    if (delta > 0.01)
      for (i = 0; i < m; i++) {
        if (n[i] != 0) {
          norm(&cent[i * k], k, n[i]);
          memcpy(&cb[i * k], &cent[i * k], k * sizeof(float));
        }
      }
  } while (delta > 0.01);
  fvq = fopen(argv[4], "wt");
  if (fvq == NULL) {
    printf("Error opening VQ file: %s\n", argv[4]);
    exit(1);
  }
  fprintf(fvq, "%ld %ld\n", k, m);
  for (j = 0; j < m; j++) {
    for (i = 0; i < k; i++)
      fprintf(fvq, "%f  ", cb[j * k + i]);
    fprintf(fvq, "\n");
  }
  fclose(fvq);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     1222 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe19(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    1265 <main+0x85>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10e0 <atol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
imul   -0x20(%rbp),%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
imul   -0x20(%rbp),%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     131f <main+0x13f>
cmpq   $0x0,-0x30(%rbp)
je     131f <main+0x13f>
cmpq   $0x0,-0x38(%rbp)
je     131f <main+0x13f>
cmpq   $0x0,-0x28(%rbp)
jne    1337 <main+0x157>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x78(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
jne    1370 <main+0x190>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    133f <main+0x15f>
mov    -0x48(%rbp),%rsi
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x78(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
imul   -0x20(%rbp),%rdx
mov    -0x78(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
movss  0xc52(%rip),%xmm0
movss  %xmm0,-0x68(%rbp)
movq   $0x1,-0x58(%rbp)
movss  -0x68(%rbp),%xmm0
movss  %xmm0,-0x6c(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1419 <main+0x239>
mov    -0x38(%rbp),%rdi
mov    -0x50(%rbp),%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   10d0 <zero@plt>
mov    -0x40(%rbp),%rax
mov    -0x50(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    13cd <main+0x1ed>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x64(%rbp)
mov    -0x78(%rbp),%rdi
call   1070 <rewind@plt>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x48(%rbp),%rax
jge    14b9 <main+0x2d9>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x78(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x64(%rbp),%r8
call   1080 <quantise@plt>
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x60(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
add    $0x1,%rdx
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%rdi
mov    -0x60(%rbp),%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <acc@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1432 <main+0x252>
movss  -0x64(%rbp),%xmm0
cvtsi2ssq -0x48(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x68(%rbp)
movss  -0x6c(%rbp),%xmm0
subss  -0x68(%rbp),%xmm0
divss  -0x68(%rbp),%xmm0
movss  %xmm0,-0x70(%rbp)
mov    -0x58(%rbp),%rsi
movss  -0x68(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x70(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
lea    0xbd4(%rip),%rdi
mov    $0x2,%al
call   1060 <printf@plt>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
movss  -0x70(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xae6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    15ce <main+0x3ee>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    15c9 <main+0x3e9>
mov    -0x40(%rbp),%rax
mov    -0x50(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     15b3 <main+0x3d3>
mov    -0x38(%rbp),%rdi
mov    -0x50(%rbp),%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
call   10a0 <norm@plt>
mov    -0x30(%rbp),%rdi
mov    -0x50(%rbp),%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x50(%rbp),%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
call   10b0 <memcpy@plt>
jmp    15b8 <main+0x3d8>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1534 <main+0x354>
jmp    15ce <main+0x3ee>
jmp    15d3 <main+0x3f3>
movss  -0x70(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xa24(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     13bb <main+0x1db>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xafc(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    1631 <main+0x451>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
lea    0xadc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xad3(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    16d7 <main+0x4f7>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    16b4 <main+0x4d4>
mov    -0x80(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x58(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    -0x50(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xa84(%rip),%rsi
mov    $0x1,%al
call   1090 <fprintf@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1669 <main+0x489>
mov    -0x80(%rbp),%rdi
lea    0x9f2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    1653 <main+0x473>
mov    -0x80(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
cmp    $0x5,%edi
jne    15bb <main+0x3bb>
mov    0x8(%rbx),%rdi
lea    0xe25(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     15c7 <main+0x3c7>
mov    0x10(%rbx),%rdi
call   1100 <atol@plt>
mov    %rax,%r13
mov    %rbx,0x50(%rsp)
mov    0x18(%rbx),%rdi
call   1100 <atol@plt>
mov    %rax,%r12
lea    0xe1b(%rip),%rdi
mov    %r13,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x0(,%r13,4),%rbx
mov    %ebx,%edi
call   10e0 <malloc@plt>
mov    %rax,%r14
mov    %r12d,%ebp
mov    %rbx,0x8(%rsp)
imul   %ebx,%ebp
mov    %ebp,%edi
call   10e0 <malloc@plt>
mov    %rax,(%rsp)
mov    %ebp,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbx
lea    0x0(,%r12,8),%edi
call   10e0 <malloc@plt>
mov    (%rsp),%rcx
test   %rcx,%rcx
je     15d4 <main+0x3d4>
test   %rbx,%rbx
je     15d4 <main+0x3d4>
test   %r14,%r14
je     15d4 <main+0x3d4>
mov    %rax,%r15
mov    %rbx,0x28(%rsp)
mov    $0xffffffffffffffff,%rbp
mov    0x20(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x4,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
add    $0x1,%rbp
cmp    %r13,%rax
je     12e0 <main+0xe0>
mov    %r14,0x60(%rsp)
lea    0xd99(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1080 <rewind@plt>
mov    %r12,%rdx
imul   %r13,%rdx
mov    (%rsp),%rdi
mov    $0x4,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbp,0x38(%rsp)
cvtsi2ss %rbp,%xmm0
movss  %xmm0,0x34(%rsp)
mov    $0x1,%eax
mov    %rax,0x40(%rsp)
movss  0xcac(%rip),%xmm0
mov    0x8(%rsp),%r14
mov    %r15,0x48(%rsp)
jmp    136c <main+0x16c>
movapd %xmm1,%xmm0
mov    %rbx,%r12
mov    0x8(%rsp),%r14
movss  %xmm0,0x18(%rsp)
test   %r12,%r12
jle    139f <main+0x19f>
mov    0x28(%rsp),%rbp
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %r13,%rsi
call   10f0 <zero@plt>
movq   $0x0,(%r15,%rbx,8)
add    $0x1,%rbx
add    %r14,%rbp
cmp    %rbx,%r12
jne    1380 <main+0x180>
movl   $0x0,0x14(%rsp)
mov    0x20(%rsp),%rdi
call   1080 <rewind@plt>
cmpq   $0x0,0x38(%rsp)
mov    0x60(%rsp),%r14
mov    (%rsp),%rbx
je     141e <main+0x21e>
mov    0x38(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x4,%esi
mov    %r13,%rdx
mov    0x20(%rsp),%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
lea    0x14(%rsp),%r8
call   1090 <quantise@plt>
addq   $0x1,(%r15,%rax,8)
imul   %r13,%rax
mov    0x28(%rsp),%rcx
lea    (%rcx,%rax,4),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1060 <acc@plt>
add    $0xffffffffffffffff,%rbp
jne    13d0 <main+0x1d0>
movss  0x14(%rsp),%xmm0
divss  0x34(%rsp),%xmm0
movss  0x18(%rsp),%xmm1
subss  %xmm0,%xmm1
divss  %xmm0,%xmm1
movss  %xmm0,0x10(%rsp)
cvtss2sd %xmm0,%xmm0
cvtss2sd %xmm1,%xmm1
movsd  %xmm1,0x18(%rsp)
lea    0xc6d(%rip),%rdi
mov    0x40(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x2,%al
call   1070 <printf@plt>
movsd  0x18(%rsp),%xmm0
ucomisd 0xb98(%rip),%xmm0
jbe    14f7 <main+0x2f7>
add    $0x1,%rbx
mov    %rbx,0x40(%rsp)
movss  0x10(%rsp),%xmm1
movapd %xmm1,%xmm0
test   %r12,%r12
mov    0x48(%rsp),%r15
mov    0x8(%rsp),%r14
jle    136c <main+0x16c>
mov    %r12,%rbx
mov    0x28(%rsp),%r12
mov    (%rsp),%r14
xor    %ebp,%ebp
jmp    14c8 <main+0x2c8>
nopl   0x0(%rax)
add    $0x1,%rbp
add    %r15,%r14
add    %r15,%r12
cmp    %rbp,%rbx
mov    0x48(%rsp),%r15
je     1360 <main+0x160>
mov    (%r15,%rbp,8),%rdx
test   %rdx,%rdx
mov    0x8(%rsp),%r15
je     14b0 <main+0x2b0>
mov    %r12,%rdi
mov    %r13,%rsi
call   10c0 <norm@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   10d0 <memcpy@plt>
movss  0x10(%rsp),%xmm1
jmp    14b0 <main+0x2b0>
mov    0x50(%rsp),%r14
mov    0x20(%r14),%rdi
lea    0xbe0(%rip),%rsi
call   1110 <fopen@plt>
test   %rax,%rax
je     15ea <main+0x3ea>
mov    %rax,%rbp
mov    %r12,%rbx
lea    0xbe3(%rip),%rsi
mov    %rax,%rdi
mov    %r13,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
test   %r12,%r12
jle    15a2 <main+0x3a2>
xor    %r12d,%r12d
lea    0xbcd(%rip),%r15
mov    %rbx,0x58(%rsp)
jmp    1574 <main+0x374>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   10a0 <fputc@plt>
add    $0x1,%r12
add    0x8(%rsp),%r14
mov    %r14,(%rsp)
mov    0x58(%rsp),%rbx
cmp    %rbx,%r12
je     15a2 <main+0x3a2>
test   %r13,%r13
mov    (%rsp),%r14
jle    1550 <main+0x350>
xor    %ebx,%ebx
nop
movss  (%r14,%rbx,4),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x1,%al
call   10b0 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1580 <main+0x380>
jmp    1550 <main+0x350>
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xa4b(%rip),%rdi
jmp    15f5 <main+0x3f5>
mov    0x8(%rbx),%rsi
lea    0xa7b(%rip),%rdi
jmp    15f5 <main+0x3f5>
lea    0xb38(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    0x20(%r14),%rsi
lea    0xaf5(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [58 x i8] c"usage: %s TrainFile K(dimension) M(codebook size) VQFile\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [42 x i8] c"Error opening training database file: %s\0A\00"
@.str.3 = [42 x i8] c"dimension K=%ld number of entries M=%ld\0A\00"
@.str.4 = [18 x i8] c"Error in malloc.\0A\00"
@.str.5 = [31 x i8] c"J=%ld entries in training set\0A\00"
@.str.6 = [39 x i8] c"\0D Iteration %ld, Dn = %f, Delta = %e\0A\00"
@.str.7 = [3 x i8] c"wt\00"
@.str.8 = [27 x i8] c"Error opening VQ file: %s\0A\00"
@.str.9 = [9 x i8] c"%ld %ld\0A\00"
@.str.10 = [5 x i8] c"%f \00"
@.str.11 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca float*
%9 = alloca float*
%10 = alloca float*
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca float
%17 = alloca float
%18 = alloca float
%19 = alloca float
%20 = alloca i32*
%21 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 5
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %2
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %20
%35 = load i32*, i32** %20
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %43
37: ; preds = %30
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %30
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i64 @atol(i8* %46)
store i64 %47, i64* %6
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 3
%50 = load i8*, i8** %49
%51 = call i64 @atol(i8* %50)
store i64 %51, i64* %7
%52 = load i64, i64* %6
%53 = load i64, i64* %7
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %52, i64 %53)
%55 = load i64, i64* %6
%56 = mul i64 4, %55
%57 = trunc i64 %56 to i32
%58 = call i64 @malloc(i32 %57)
%59 = inttoptr i64 %58 to float*
store float* %59, float** %8
%60 = load i64, i64* %6
%61 = mul i64 4, %60
%62 = load i64, i64* %7
%63 = mul i64 %61, %62
%64 = trunc i64 %63 to i32
%65 = call i64 @malloc(i32 %64)
%66 = inttoptr i64 %65 to float*
store float* %66, float** %9
%67 = load i64, i64* %6
%68 = mul i64 4, %67
%69 = load i64, i64* %7
%70 = mul i64 %68, %69
%71 = trunc i64 %70 to i32
%72 = call i64 @malloc(i32 %71)
%73 = inttoptr i64 %72 to float*
store float* %73, float** %10
%74 = load i64, i64* %7
%75 = mul i64 8, %74
%76 = trunc i64 %75 to i32
%77 = call i64 @malloc(i32 %76)
%78 = inttoptr i64 %77 to i64*
store i64* %78, i64** %11
%79 = load float*, float** %9
%80 = icmp eq float* %79, null
br i1 %80, label %90, label %81
81: ; preds = %43
%82 = load float*, float** %9
%83 = icmp eq float* %82, null
br i1 %83, label %90, label %84
84: ; preds = %81
%85 = load float*, float** %10
%86 = icmp eq float* %85, null
br i1 %86, label %90, label %87
87: ; preds = %84
%88 = load float*, float** %8
%89 = icmp eq float* %88, null
br i1 %89, label %90, label %93
90: ; preds = %87, %84, %81, %43
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %87
store i64 0, i64* %12
br label %94
94: ; preds = %101, %93
%95 = load float*, float** %8
%96 = load i64, i64* %6
%97 = load i32*, i32** %20
%98 = call i64 @fread(float* %95, i32 4, i64 %96, i32* %97)
%99 = load i64, i64* %6
%100 = icmp eq i64 %98, %99
br i1 %100, label %101, label %104
101: ; preds = %94
%102 = load i64, i64* %12
%103 = add nsw i64 %102, 1
store i64 %103, i64* %12
br label %94
104: ; preds = %94
%105 = load i64, i64* %12
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %105)
%107 = load i32*, i32** %20
%108 = call i32 @rewind(i32* %107)
%109 = load float*, float** %9
%110 = load i64, i64* %6
%111 = load i64, i64* %7
%112 = mul nsw i64 %110, %111
%113 = load i32*, i32** %20
%114 = call i64 @fread(float* %109, i32 4, i64 %112, i32* %113)
store float 0x4693B8B5C0000000, float* %17
store i64 1, i64* %14
br label %115
115: ; preds = %231, %104
%116 = load float, float* %17
store float %116, float* %18
store i64 0, i64* %13
br label %117
117: ; preds = %132, %115
%118 = load i64, i64* %13
%119 = load i64, i64* %7
%120 = icmp slt i64 %118, %119
br i1 %120, label %121, label %135
121: ; preds = %117
%122 = load float*, float** %10
%123 = load i64, i64* %13
%124 = load i64, i64* %6
%125 = mul nsw i64 %123, %124
%126 = getelementptr inbounds float, float* %122, i64 %125
%127 = load i64, i64* %6
%128 = call i32 @zero(float* %126, i64 %127)
%129 = load i64*, i64** %11
%130 = load i64, i64* %13
%131 = getelementptr inbounds i64, i64* %129, i64 %130
store i64 0, i64* %131
br label %132
132: ; preds = %121
%133 = load i64, i64* %13
%134 = add nsw i64 %133, 1
store i64 %134, i64* %13
br label %117
135: ; preds = %117
store float 0.000000e+00, float* %16
%136 = load i32*, i32** %20
%137 = call i32 @rewind(i32* %136)
store i64 0, i64* %13
br label %138
138: ; preds = %165, %135
%139 = load i64, i64* %13
%140 = load i64, i64* %12
%141 = icmp slt i64 %139, %140
br i1 %141, label %142, label %168
142: ; preds = %138
%143 = load float*, float** %8
%144 = load i64, i64* %6
%145 = load i32*, i32** %20
%146 = call i64 @fread(float* %143, i32 4, i64 %144, i32* %145)
%147 = load float*, float** %9
%148 = load float*, float** %8
%149 = load i64, i64* %6
%150 = load i64, i64* %7
%151 = call i64 @quantise(float* %147, float* %148, i64 %149, i64 %150, float* %16)
store i64 %151, i64* %15
%152 = load i64*, i64** %11
%153 = load i64, i64* %15
%154 = getelementptr inbounds i64, i64* %152, i64 %153
%155 = load i64, i64* %154
%156 = add nsw i64 %155, 1
store i64 %156, i64* %154
%157 = load float*, float** %10
%158 = load i64, i64* %15
%159 = load i64, i64* %6
%160 = mul nsw i64 %158, %159
%161 = getelementptr inbounds float, float* %157, i64 %160
%162 = load float*, float** %8
%163 = load i64, i64* %6
%164 = call i32 @acc(float* %161, float* %162, i64 %163)
br label %165
165: ; preds = %142
%166 = load i64, i64* %13
%167 = add nsw i64 %166, 1
store i64 %167, i64* %13
br label %138
168: ; preds = %138
%169 = load float, float* %16
%170 = load i64, i64* %12
%171 = sitofp i64 %170 to float
%172 = fdiv float %169, %171
store float %172, float* %17
%173 = load float, float* %18
%174 = load float, float* %17
%175 = fsub float %173, %174
%176 = load float, float* %17
%177 = fdiv float %175, %176
store float %177, float* %19
%178 = load i64, i64* %14
%179 = load float, float* %17
%180 = fpext float %179 to double
%181 = load float, float* %19
%182 = fpext float %181 to double
%183 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i64 %178, double %180, double %182)
%184 = load i64, i64* %14
%185 = add nsw i64 %184, 1
store i64 %185, i64* %14
%186 = load float, float* %19
%187 = fpext float %186 to double
%188 = fcmp ogt double %187, 1.000000e-02
br i1 %188, label %189, label %230
189: ; preds = %168
store i64 0, i64* %13
br label %190
190: ; preds = %226, %189
%191 = load i64, i64* %13
%192 = load i64, i64* %7
%193 = icmp slt i64 %191, %192
br i1 %193, label %194, label %229
194: ; preds = %190
%195 = load i64*, i64** %11
%196 = load i64, i64* %13
%197 = getelementptr inbounds i64, i64* %195, i64 %196
%198 = load i64, i64* %197
%199 = icmp ne i64 %198, 0
br i1 %199, label %200, label %225
200: ; preds = %194
%201 = load float*, float** %10
%202 = load i64, i64* %13
%203 = load i64, i64* %6
%204 = mul nsw i64 %202, %203
%205 = getelementptr inbounds float, float* %201, i64 %204
%206 = load i64, i64* %6
%207 = load i64*, i64** %11
%208 = load i64, i64* %13
%209 = getelementptr inbounds i64, i64* %207, i64 %208
%210 = load i64, i64* %209
%211 = call i32 @norm(float* %205, i64 %206, i64 %210)
%212 = load float*, float** %9
%213 = load i64, i64* %13
%214 = load i64, i64* %6
%215 = mul nsw i64 %213, %214
%216 = getelementptr inbounds float, float* %212, i64 %215
%217 = load float*, float** %10
%218 = load i64, i64* %13
%219 = load i64, i64* %6
%220 = mul nsw i64 %218, %219
%221 = getelementptr inbounds float, float* %217, i64 %220
%222 = load i64, i64* %6
%223 = mul i64 %222, 4
%224 = call i32 @memcpy(float* %216, float* %221, i64 %223)
br label %225
225: ; preds = %200, %194
br label %226
226: ; preds = %225
%227 = load i64, i64* %13
%228 = add nsw i64 %227, 1
store i64 %228, i64* %13
br label %190
229: ; preds = %190
br label %230
230: ; preds = %229, %168
br label %231
231: ; preds = %230
%232 = load float, float* %19
%233 = fpext float %232 to double
%234 = fcmp ogt double %233, 1.000000e-02
br i1 %234, label %115, label %235
235: ; preds = %231
%236 = load i8**, i8*** %5
%237 = getelementptr inbounds i8*, i8** %236, i64 4
%238 = load i8*, i8** %237
%239 = call i32* @fopen(i8* %238, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %239, i32** %21
%240 = load i32*, i32** %21
%241 = icmp eq i32* %240, null
br i1 %241, label %242, label %248
242: ; preds = %235
%243 = load i8**, i8*** %5
%244 = getelementptr inbounds i8*, i8** %243, i64 4
%245 = load i8*, i8** %244
%246 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %245)
%247 = call i32 @exit(i32 1)
unreachable
248: ; preds = %235
%249 = load i32*, i32** %21
%250 = load i64, i64* %6
%251 = load i64, i64* %7
%252 = call i32 (i32*, i8*, ...) @fprintf(i32* %249, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i64 %250, i64 %251)
store i64 0, i64* %14
br label %253
253: ; preds = %280, %248
%254 = load i64, i64* %14
%255 = load i64, i64* %7
%256 = icmp slt i64 %254, %255
br i1 %256, label %257, label %283
257: ; preds = %253
store i64 0, i64* %13
br label %258
258: ; preds = %274, %257
%259 = load i64, i64* %13
%260 = load i64, i64* %6
%261 = icmp slt i64 %259, %260
br i1 %261, label %262, label %277
262: ; preds = %258
%263 = load i32*, i32** %21
%264 = load float*, float** %9
%265 = load i64, i64* %14
%266 = load i64, i64* %6
%267 = mul nsw i64 %265, %266
%268 = load i64, i64* %13
%269 = add nsw i64 %267, %268
%270 = getelementptr inbounds float, float* %264, i64 %269
%271 = load float, float* %270
%272 = fpext float %271 to double
%273 = call i32 (i32*, i8*, ...) @fprintf(i32* %263, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), double %272)
br label %274
274: ; preds = %262
%275 = load i64, i64* %13
%276 = add nsw i64 %275, 1
store i64 %276, i64* %13
br label %258
277: ; preds = %258
%278 = load i32*, i32** %21
%279 = call i32 (i32*, i8*, ...) @fprintf(i32* %278, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
br label %280
280: ; preds = %277
%281 = load i64, i64* %14
%282 = add nsw i64 %281, 1
store i64 %282, i64* %14
br label %253
283: ; preds = %253
%284 = load i32*, i32** %21
%285 = call i32 @fclose(i32* %284)
ret i32 0
}

LLVM IR (-O2):
@.str = [58 x i8] c"usage: %s TrainFile K(dimension) M(codebook size) VQFile\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [42 x i8] c"Error opening training database file: %s\0A\00"
@.str.3 = [42 x i8] c"dimension K=%ld number of entries M=%ld\0A\00"
@.str.5 = [31 x i8] c"J=%ld entries in training set\0A\00"
@.str.6 = [39 x i8] c"\0D Iteration %ld, Dn = %f, Delta = %e\0A\00"
@.str.7 = [3 x i8] c"wt\00"
@.str.8 = [27 x i8] c"Error opening VQ file: %s\0A\00"
@.str.9 = [9 x i8] c"%ld %ld\0A\00"
@.str.10 = [5 x i8] c"%f \00"
@str = [17 x i8] c"Error in malloc.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca float
%4 = bitcast float* %3 to i8*
%5 = icmp eq i32 %0, 5
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i64 @atol(i8* %21)
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = tail call i64 @atol(i8* %24)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %22, i64 %25)
%27 = shl i64 %22, 2
%28 = trunc i64 %27 to i32
%29 = tail call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to float*
%31 = mul i64 %25, %27
%32 = trunc i64 %31 to i32
%33 = tail call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to float*
%35 = tail call i64 @malloc(i32 %32)
%36 = inttoptr i64 %35 to float*
%37 = trunc i64 %25 to i32
%38 = shl i32 %37, 3
%39 = tail call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i64*
%41 = icmp eq i64 %33, 0
%42 = icmp eq i64 %35, 0
%43 = select i1 %41, i1 true, i1 %42
%44 = icmp eq i64 %29, 0
%45 = select i1 %43, i1 true, i1 %44
br i1 %45, label %46, label %49
46: ; preds = %19
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %19, %49
%50 = phi i64 [ %53, %49 ], [ 0, %19 ]
%51 = tail call i64 @fread(float* %30, i32 4, i64 %22, i32* %13)
%52 = icmp eq i64 %51, %22
%53 = add nuw nsw i64 %50, 1
br i1 %52, label %49, label %54
54: ; preds = %49
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %50)
%56 = tail call i32 @rewind(i32* %13)
%57 = mul nsw i64 %25, %22
%58 = tail call i64 @fread(float* %34, i32 4, i64 %57, i32* %13)
%59 = icmp sgt i64 %25, 0
%60 = icmp eq i64 %50, 0
%61 = sitofp i64 %50 to float
br label %62
62: ; preds = %98, %54
%63 = phi float [ 0x4693B8B5C0000000, %54 ], [ %89, %98 ]
%64 = phi i64 [ 1, %54 ], [ %95, %98 ]
br i1 %59, label %65, label %73
65: ; preds = %62, %65
%66 = phi i64 [ %71, %65 ], [ 0, %62 ]
%67 = mul nsw i64 %66, %22
%68 = getelementptr inbounds float, float* %36, i64 %67
%69 = call i32 @zero(float* %68, i64 %22)
%70 = getelementptr inbounds i64, i64* %40, i64 %66
store i64 0, i64* %70
%71 = add nuw nsw i64 %66, 1
%72 = icmp eq i64 %71, %25
br i1 %72, label %73, label %65
73: ; preds = %65, %62
store float 0.000000e+00, float* %3
%74 = call i32 @rewind(i32* %13)
br i1 %60, label %87, label %75
75: ; preds = %73, %75
%76 = phi i64 [ %85, %75 ], [ 0, %73 ]
%77 = call i64 @fread(float* %30, i32 4, i64 %22, i32* %13)
%78 = call i64 @quantise(float* %34, float* %30, i64 %22, i64 %25, float* %3)
%79 = getelementptr inbounds i64, i64* %40, i64 %78
%80 = load i64, i64* %79
%81 = add nsw i64 %80, 1
store i64 %81, i64* %79
%82 = mul nsw i64 %78, %22
%83 = getelementptr inbounds float, float* %36, i64 %82
%84 = call i32 @acc(float* %83, float* %30, i64 %22)
%85 = add nuw nsw i64 %76, 1
%86 = icmp eq i64 %85, %50
br i1 %86, label %87, label %75
87: ; preds = %75, %73
%88 = load float, float* %3
%89 = fdiv float %88, %61
%90 = fsub float %63, %89
%91 = fdiv float %90, %89
%92 = fpext float %89 to double
%93 = fpext float %91 to double
%94 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i64 %64, double %92, double %93)
%95 = add nuw nsw i64 %64, 1
%96 = fcmp ogt double %93, 1.000000e-02
br i1 %96, label %97, label %113
97: ; preds = %87
br i1 %59, label %99, label %98
98: ; preds = %110, %97
br label %62
99: ; preds = %97, %110
%100 = phi i64 [ %111, %110 ], [ 0, %97 ]
%101 = getelementptr inbounds i64, i64* %40, i64 %100
%102 = load i64, i64* %101
%103 = icmp eq i64 %102, 0
br i1 %103, label %110, label %104
104: ; preds = %99
%105 = mul nsw i64 %100, %22
%106 = getelementptr inbounds float, float* %36, i64 %105
%107 = call i32 @norm(float* %106, i64 %22, i64 %102)
%108 = getelementptr inbounds float, float* %34, i64 %105
%109 = call i32 @memcpy(float* %108, float* %106, i64 %27)
br label %110
110: ; preds = %99, %104
%111 = add nuw nsw i64 %100, 1
%112 = icmp eq i64 %111, %25
br i1 %112, label %98, label %99
113: ; preds = %87
%114 = getelementptr inbounds i8*, i8** %1, i64 4
%115 = load i8*, i8** %114
%116 = call i32* @fopen(i8* %115, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%117 = icmp eq i32* %116, null
br i1 %117, label %118, label %122
118: ; preds = %113
%119 = load i8*, i8** %114
%120 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %119)
%121 = call i32 @exit(i32 1)
unreachable
122: ; preds = %113
%123 = call i32 (i32*, i8*, ...) @fprintf(i32* %116, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i64 %22, i64 %25)
br i1 %59, label %124, label %143
124: ; preds = %122
%125 = icmp sgt i64 %22, 0
br label %126
126: ; preds = %124, %139
%127 = phi i64 [ 0, %124 ], [ %141, %139 ]
br i1 %125, label %128, label %139
128: ; preds = %126
%129 = mul nsw i64 %127, %22
br label %130
130: ; preds = %128, %130
%131 = phi i64 [ 0, %128 ], [ %137, %130 ]
%132 = add nsw i64 %131, %129
%133 = getelementptr inbounds float, float* %34, i64 %132
%134 = load float, float* %133
%135 = fpext float %134 to double
%136 = call i32 (i32*, i8*, ...) @fprintf(i32* %116, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), double %135)
%137 = add nuw nsw i64 %131, 1
%138 = icmp eq i64 %137, %22
br i1 %138, label %139, label %130
139: ; preds = %130, %126
%140 = call i32 @fputc(i32 10, i32* %116)
%141 = add nuw nsw i64 %127, 1
%142 = icmp eq i64 %141, %25
br i1 %142, label %143, label %126
143: ; preds = %139, %122
%144 = call i32 @fclose(i32* %116)
ret i32 0
}