#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;
  int ret;
  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);
  ret = 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++) {
      ret = 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    $0x90,%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>
mov    %eax,-0x84(%rbp)
movss  0xc4c(%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    141f <main+0x23f>
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    13d3 <main+0x1f3>
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    14c5 <main+0x2e5>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x78(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
mov    %eax,-0x84(%rbp)
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    1438 <main+0x258>
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    0xbc8(%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  0xada(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    15da <main+0x3fa>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    15d5 <main+0x3f5>
mov    -0x40(%rbp),%rax
mov    -0x50(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     15bf <main+0x3df>
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    15c4 <main+0x3e4>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1540 <main+0x360>
jmp    15da <main+0x3fa>
jmp    15df <main+0x3ff>
movss  -0x70(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xa18(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     13c1 <main+0x1e1>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xaf0(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    163d <main+0x45d>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
lea    0xad0(%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    0xac7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    16e3 <main+0x503>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    16c0 <main+0x4e0>
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    0xa78(%rip),%rsi
mov    $0x1,%al
call   1090 <fprintf@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1675 <main+0x495>
mov    -0x80(%rbp),%rdi
lea    0x9e6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    165f <main+0x47f>
mov    -0x80(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x90,%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*
%22 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 5
br i1 %24, label %25, label %31
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %2
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %20
%36 = load i32*, i32** %20
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %44
38: ; preds = %31
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %31
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i64 @atol(i8* %47)
store i64 %48, i64* %6
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 3
%51 = load i8*, i8** %50
%52 = call i64 @atol(i8* %51)
store i64 %52, i64* %7
%53 = load i64, i64* %6
%54 = load i64, i64* %7
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %53, i64 %54)
%56 = load i64, i64* %6
%57 = mul i64 4, %56
%58 = trunc i64 %57 to i32
%59 = call i64 @malloc(i32 %58)
%60 = inttoptr i64 %59 to float*
store float* %60, float** %8
%61 = load i64, i64* %6
%62 = mul i64 4, %61
%63 = load i64, i64* %7
%64 = mul i64 %62, %63
%65 = trunc i64 %64 to i32
%66 = call i64 @malloc(i32 %65)
%67 = inttoptr i64 %66 to float*
store float* %67, float** %9
%68 = load i64, i64* %6
%69 = mul i64 4, %68
%70 = load i64, i64* %7
%71 = mul i64 %69, %70
%72 = trunc i64 %71 to i32
%73 = call i64 @malloc(i32 %72)
%74 = inttoptr i64 %73 to float*
store float* %74, float** %10
%75 = load i64, i64* %7
%76 = mul i64 8, %75
%77 = trunc i64 %76 to i32
%78 = call i64 @malloc(i32 %77)
%79 = inttoptr i64 %78 to i64*
store i64* %79, i64** %11
%80 = load float*, float** %9
%81 = icmp eq float* %80, null
br i1 %81, label %91, label %82
82: ; preds = %44
%83 = load float*, float** %9
%84 = icmp eq float* %83, null
br i1 %84, label %91, label %85
85: ; preds = %82
%86 = load float*, float** %10
%87 = icmp eq float* %86, null
br i1 %87, label %91, label %88
88: ; preds = %85
%89 = load float*, float** %8
%90 = icmp eq float* %89, null
br i1 %90, label %91, label %94
91: ; preds = %88, %85, %82, %44
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %88
store i64 0, i64* %12
br label %95
95: ; preds = %102, %94
%96 = load float*, float** %8
%97 = load i64, i64* %6
%98 = load i32*, i32** %20
%99 = call i64 @fread(float* %96, i32 4, i64 %97, i32* %98)
%100 = load i64, i64* %6
%101 = icmp eq i64 %99, %100
br i1 %101, label %102, label %105
102: ; preds = %95
%103 = load i64, i64* %12
%104 = add nsw i64 %103, 1
store i64 %104, i64* %12
br label %95
105: ; preds = %95
%106 = load i64, i64* %12
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %106)
%108 = load i32*, i32** %20
%109 = call i32 @rewind(i32* %108)
%110 = load float*, float** %9
%111 = load i64, i64* %6
%112 = load i64, i64* %7
%113 = mul nsw i64 %111, %112
%114 = load i32*, i32** %20
%115 = call i64 @fread(float* %110, i32 4, i64 %113, i32* %114)
%116 = trunc i64 %115 to i32
store i32 %116, i32* %22
store float 0x4693B8B5C0000000, float* %17
store i64 1, i64* %14
br label %117
117: ; preds = %234, %105
%118 = load float, float* %17
store float %118, float* %18
store i64 0, i64* %13
br label %119
119: ; preds = %134, %117
%120 = load i64, i64* %13
%121 = load i64, i64* %7
%122 = icmp slt i64 %120, %121
br i1 %122, label %123, label %137
123: ; preds = %119
%124 = load float*, float** %10
%125 = load i64, i64* %13
%126 = load i64, i64* %6
%127 = mul nsw i64 %125, %126
%128 = getelementptr inbounds float, float* %124, i64 %127
%129 = load i64, i64* %6
%130 = call i32 @zero(float* %128, i64 %129)
%131 = load i64*, i64** %11
%132 = load i64, i64* %13
%133 = getelementptr inbounds i64, i64* %131, i64 %132
store i64 0, i64* %133
br label %134
134: ; preds = %123
%135 = load i64, i64* %13
%136 = add nsw i64 %135, 1
store i64 %136, i64* %13
br label %119
137: ; preds = %119
store float 0.000000e+00, float* %16
%138 = load i32*, i32** %20
%139 = call i32 @rewind(i32* %138)
store i64 0, i64* %13
br label %140
140: ; preds = %168, %137
%141 = load i64, i64* %13
%142 = load i64, i64* %12
%143 = icmp slt i64 %141, %142
br i1 %143, label %144, label %171
144: ; preds = %140
%145 = load float*, float** %8
%146 = load i64, i64* %6
%147 = load i32*, i32** %20
%148 = call i64 @fread(float* %145, i32 4, i64 %146, i32* %147)
%149 = trunc i64 %148 to i32
store i32 %149, i32* %22
%150 = load float*, float** %9
%151 = load float*, float** %8
%152 = load i64, i64* %6
%153 = load i64, i64* %7
%154 = call i64 @quantise(float* %150, float* %151, i64 %152, i64 %153, float* %16)
store i64 %154, i64* %15
%155 = load i64*, i64** %11
%156 = load i64, i64* %15
%157 = getelementptr inbounds i64, i64* %155, i64 %156
%158 = load i64, i64* %157
%159 = add nsw i64 %158, 1
store i64 %159, i64* %157
%160 = load float*, float** %10
%161 = load i64, i64* %15
%162 = load i64, i64* %6
%163 = mul nsw i64 %161, %162
%164 = getelementptr inbounds float, float* %160, i64 %163
%165 = load float*, float** %8
%166 = load i64, i64* %6
%167 = call i32 @acc(float* %164, float* %165, i64 %166)
br label %168
168: ; preds = %144
%169 = load i64, i64* %13
%170 = add nsw i64 %169, 1
store i64 %170, i64* %13
br label %140
171: ; preds = %140
%172 = load float, float* %16
%173 = load i64, i64* %12
%174 = sitofp i64 %173 to float
%175 = fdiv float %172, %174
store float %175, float* %17
%176 = load float, float* %18
%177 = load float, float* %17
%178 = fsub float %176, %177
%179 = load float, float* %17
%180 = fdiv float %178, %179
store float %180, float* %19
%181 = load i64, i64* %14
%182 = load float, float* %17
%183 = fpext float %182 to double
%184 = load float, float* %19
%185 = fpext float %184 to double
%186 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i64 %181, double %183, double %185)
%187 = load i64, i64* %14
%188 = add nsw i64 %187, 1
store i64 %188, i64* %14
%189 = load float, float* %19
%190 = fpext float %189 to double
%191 = fcmp ogt double %190, 1.000000e-02
br i1 %191, label %192, label %233
192: ; preds = %171
store i64 0, i64* %13
br label %193
193: ; preds = %229, %192
%194 = load i64, i64* %13
%195 = load i64, i64* %7
%196 = icmp slt i64 %194, %195
br i1 %196, label %197, label %232
197: ; preds = %193
%198 = load i64*, i64** %11
%199 = load i64, i64* %13
%200 = getelementptr inbounds i64, i64* %198, i64 %199
%201 = load i64, i64* %200
%202 = icmp ne i64 %201, 0
br i1 %202, label %203, label %228
203: ; preds = %197
%204 = load float*, float** %10
%205 = load i64, i64* %13
%206 = load i64, i64* %6
%207 = mul nsw i64 %205, %206
%208 = getelementptr inbounds float, float* %204, i64 %207
%209 = load i64, i64* %6
%210 = load i64*, i64** %11
%211 = load i64, i64* %13
%212 = getelementptr inbounds i64, i64* %210, i64 %211
%213 = load i64, i64* %212
%214 = call i32 @norm(float* %208, i64 %209, i64 %213)
%215 = load float*, float** %9
%216 = load i64, i64* %13
%217 = load i64, i64* %6
%218 = mul nsw i64 %216, %217
%219 = getelementptr inbounds float, float* %215, i64 %218
%220 = load float*, float** %10
%221 = load i64, i64* %13
%222 = load i64, i64* %6
%223 = mul nsw i64 %221, %222
%224 = getelementptr inbounds float, float* %220, i64 %223
%225 = load i64, i64* %6
%226 = mul i64 %225, 4
%227 = call i32 @memcpy(float* %219, float* %224, i64 %226)
br label %228
228: ; preds = %203, %197
br label %229
229: ; preds = %228
%230 = load i64, i64* %13
%231 = add nsw i64 %230, 1
store i64 %231, i64* %13
br label %193
232: ; preds = %193
br label %233
233: ; preds = %232, %171
br label %234
234: ; preds = %233
%235 = load float, float* %19
%236 = fpext float %235 to double
%237 = fcmp ogt double %236, 1.000000e-02
br i1 %237, label %117, label %238
238: ; preds = %234
%239 = load i8**, i8*** %5
%240 = getelementptr inbounds i8*, i8** %239, i64 4
%241 = load i8*, i8** %240
%242 = call i32* @fopen(i8* %241, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %242, i32** %21
%243 = load i32*, i32** %21
%244 = icmp eq i32* %243, null
br i1 %244, label %245, label %251
245: ; preds = %238
%246 = load i8**, i8*** %5
%247 = getelementptr inbounds i8*, i8** %246, i64 4
%248 = load i8*, i8** %247
%249 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %248)
%250 = call i32 @exit(i32 1)
unreachable
251: ; preds = %238
%252 = load i32*, i32** %21
%253 = load i64, i64* %6
%254 = load i64, i64* %7
%255 = call i32 (i32*, i8*, ...) @fprintf(i32* %252, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i64 %253, i64 %254)
store i64 0, i64* %14
br label %256
256: ; preds = %283, %251
%257 = load i64, i64* %14
%258 = load i64, i64* %7
%259 = icmp slt i64 %257, %258
br i1 %259, label %260, label %286
260: ; preds = %256
store i64 0, i64* %13
br label %261
261: ; preds = %277, %260
%262 = load i64, i64* %13
%263 = load i64, i64* %6
%264 = icmp slt i64 %262, %263
br i1 %264, label %265, label %280
265: ; preds = %261
%266 = load i32*, i32** %21
%267 = load float*, float** %9
%268 = load i64, i64* %14
%269 = load i64, i64* %6
%270 = mul nsw i64 %268, %269
%271 = load i64, i64* %13
%272 = add nsw i64 %270, %271
%273 = getelementptr inbounds float, float* %267, i64 %272
%274 = load float, float* %273
%275 = fpext float %274 to double
%276 = call i32 (i32*, i8*, ...) @fprintf(i32* %266, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), double %275)
br label %277
277: ; preds = %265
%278 = load i64, i64* %13
%279 = add nsw i64 %278, 1
store i64 %279, i64* %13
br label %261
280: ; preds = %261
%281 = load i32*, i32** %21
%282 = call i32 (i32*, i8*, ...) @fprintf(i32* %281, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
br label %283
283: ; preds = %280
%284 = load i64, i64* %14
%285 = add nsw i64 %284, 1
store i64 %285, i64* %14
br label %256
286: ; preds = %256
%287 = load i32*, i32** %21
%288 = call i32 @fclose(i32* %287)
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
}