#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 */
unsigned int KNN(char *, void *, unsigned int *, unsigned int);
scalar_t__ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ srandom(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *args[]) {
  unsigned int i, k, ret, queryID;
  unsigned int *ans;
  void *queryCompleteRecord;
  FILE *fin;
  char choice;
  if (argc < 3) {
    printf("usage: inputfile k\n");
    exit(-1);
  }
  if (atoi(args[2]) <= 0) {
    fprintf(stderr, "value of k should be non-zero positive\n");
    exit(-1);
  }
  k = (unsigned int)atoi(args[2]);
  ans = malloc(sizeof(unsigned int) * k);
  if (ans == NULL) {
    perror("ans in main");
    exit(-1);
  }
  queryCompleteRecord = malloc(1024);
  if (queryCompleteRecord == NULL) {
    perror("malloc q");
    exit(-1);
  }
  fin = fopen(args[1], "rb");
  if (fin == NULL) {
    perror("fin in main");
    exit(-1);
  }
  srandom(2);
  for (queryID = 1; queryID < 5000; queryID++) {
    fread(queryCompleteRecord, 1024, 1, fin);
    ret = KNN(args[1], queryCompleteRecord, ans, k);
  }
  fclose(fin);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e6 <main+0x36>
lea    0xe29(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
cmp    $0x0,%rax
jg     121c <main+0x6c>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125f <main+0xaf>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    128e <main+0xde>
lea    0xdc9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdb4(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12c7 <main+0x117>
lea    0xd9c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    $0x2,%edi
call   1030 <srandom@plt>
movl   $0x1,-0x20(%rbp)
cmpl   $0x1388,-0x20(%rbp)
jae    1325 <main+0x175>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%ecx
call   10d0 <KNN@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12d8 <main+0x128>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jle    1285 <main+0xd5>
mov    %rsi,%r14
mov    0x10(%rsi),%rdi
call   10b0 <atoi@plt>
test   %rax,%rax
jle    129b <main+0xeb>
mov    %rax,%r15
lea    0x0(,%rax,4),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12ba <main+0x10a>
mov    %rax,%r12
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12c3 <main+0x113>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
lea    0xe28(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12cc <main+0x11c>
mov    %rax,%r13
mov    $0x2,%edi
call   1030 <srandom@plt>
mov    $0x1387,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1050 <fread@plt>
mov    0x8(%r14),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
call   10d0 <KNN@plt>
add    $0xffffffff,%ebp
jne    1240 <main+0x90>
mov    %r13,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xd67(%rip),%rdi
jmp    12d3 <main+0x123>
lea    0xd6a(%rip),%rdi
jmp    12d3 <main+0x123>
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"usage: inputfile k\0A\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"value of k should be non-zero positive\0A\00"
@.str.2 = [12 x i8] c"ans in main\00"
@.str.3 = [9 x i8] c"malloc q\00"
@.str.4 = [3 x i8] c"rb\00"
@.str.5 = [12 x i8] c"fin in main\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i64 @atoi(i8* %22)
%24 = icmp sle i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %19
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i64 @atoi(i8* %32)
%34 = trunc i64 %33 to i32
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = zext i32 %35 to i64
%37 = mul i64 4, %36
%38 = trunc i64 %37 to i32
%39 = call i8* @malloc(i32 %38)
%40 = bitcast i8* %39 to i32*
store i32* %40, i32** %10
%41 = load i32*, i32** %10
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %29
%44 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %29
%47 = call i8* @malloc(i32 1024)
store i8* %47, i8** %11
%48 = load i8*, i8** %11
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %46
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32* @fopen(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %57, i32** %12
%58 = load i32*, i32** %12
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %63
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @exit(i32 -1)
unreachable
63: ; preds = %53
%64 = call i32 @srandom(i32 2)
store i32 1, i32* %9
br label %65
65: ; preds = %79, %63
%66 = load i32, i32* %9
%67 = icmp ult i32 %66, 5000
br i1 %67, label %68, label %82
68: ; preds = %65
%69 = load i8*, i8** %11
%70 = load i32*, i32** %12
%71 = call i32 @fread(i8* %69, i32 1024, i32 1, i32* %70)
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 1
%74 = load i8*, i8** %73
%75 = load i8*, i8** %11
%76 = load i32*, i32** %10
%77 = load i32, i32* %7
%78 = call i32 @KNN(i8* %74, i8* %75, i32* %76, i32 %77)
store i32 %78, i32* %8
br label %79
79: ; preds = %68
%80 = load i32, i32* %9
%81 = add i32 %80, 1
store i32 %81, i32* %9
br label %65
82: ; preds = %65
%83 = load i32*, i32** %12
%84 = call i32 @fclose(i32* %83)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [40 x i8] c"value of k should be non-zero positive\0A\00"
@.str.2 = [12 x i8] c"ans in main\00"
@.str.3 = [9 x i8] c"malloc q\00"
@.str.4 = [3 x i8] c"rb\00"
@.str.5 = [12 x i8] c"fin in main\00"
@str = [19 x i8] c"usage: inputfile k\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i64 @atoi(i8* %9)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %7
%17 = trunc i64 %10 to i32
%18 = shl i32 %17, 2
%19 = tail call i8* @malloc(i32 %18)
%20 = bitcast i8* %19 to i32*
%21 = icmp eq i8* %19, null
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %16
%26 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %25
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = tail call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%38 = tail call i32 @exit(i32 -1)
unreachable
39: ; preds = %31
%40 = tail call i32 @srandom(i32 2)
br label %41
41: ; preds = %39, %41
%42 = phi i32 [ 1, %39 ], [ %46, %41 ]
%43 = tail call i32 @fread(i8* %26, i32 1024, i32 1, i32* %34)
%44 = load i8*, i8** %32
%45 = tail call i32 @KNN(i8* %44, i8* %26, i32* %20, i32 %17)
%46 = add nuw nsw i32 %42, 1
%47 = icmp eq i32 %46, 5000
br i1 %47, label %48, label %41
48: ; preds = %41
%49 = tail call i32 @fclose(i32* %34)
ret i32 0
}