#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 *, ...);
long 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: vqtrain TrainFile K M VQFile\n");
    exit(0);
  }
  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) == 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.001)
      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.001);
  fvq = fopen(argv[4], "wt");
  if (fvq == NULL) {
    printf("Error opening VQ file: %s\n", argv[4]);
    exit(1);
  }
  fprintf(fvq, "%d %d\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     1218 <main+0x38>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0e(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    125b <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdee(%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    0xdd6(%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     1315 <main+0x135>
cmpq   $0x0,-0x30(%rbp)
je     1315 <main+0x135>
cmpq   $0x0,-0x38(%rbp)
je     1315 <main+0x135>
cmpq   $0x0,-0x28(%rbp)
jne    132d <main+0x14d>
lea    0xd70(%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    1366 <main+0x186>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1335 <main+0x155>
mov    -0x48(%rbp),%rsi
lea    0xd2d(%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  0xc5c(%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    140f <main+0x22f>
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    13c3 <main+0x1e3>
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    14af <main+0x2cf>
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    1428 <main+0x248>
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    0xbc9(%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  0xaf0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    15c4 <main+0x3e4>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    15bf <main+0x3df>
mov    -0x40(%rbp),%rax
mov    -0x50(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     15a9 <main+0x3c9>
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    15ae <main+0x3ce>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    152a <main+0x34a>
jmp    15c4 <main+0x3e4>
jmp    15c9 <main+0x3e9>
movss  -0x70(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xa2e(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     13b1 <main+0x1d1>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xaf1(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    1627 <main+0x447>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
lea    0xad1(%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    0xac8(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    16cd <main+0x4ed>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    16aa <main+0x4ca>
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    0xa77(%rip),%rsi
mov    $0x1,%al
call   1090 <fprintf@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    165f <main+0x47f>
mov    -0x80(%rbp),%rdi
lea    0x9e7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    1649 <main+0x469>
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
cmp    $0x5,%edi
jne    15bb <main+0x3bb>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xdeb(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     15ce <main+0x3ce>
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    0xde1(%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     15db <main+0x3db>
test   %rbx,%rbx
je     15db <main+0x3db>
test   %r14,%r14
je     15db <main+0x3db>
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    0xd5f(%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    0xc33(%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    0xba6(%rip),%rsi
call   1110 <fopen@plt>
test   %rax,%rax
je     15f1 <main+0x3f1>
mov    %rax,%rbp
mov    %r12,%rbx
lea    0xba9(%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    0xb91(%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
lea    0xb26(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    0x8(%rbx),%rsi
lea    0xa3a(%rip),%rdi
jmp    15fc <main+0x3fc>
lea    0xaf5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    0x20(%r14),%rsi
lea    0xab4(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

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

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