#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 uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
scalar_t__ calloc(int, int);
int ceil(double);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ free(int *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int *bitmap_sort(const char *infile, const char *outfile, int max) {
  int datum, i, j, map_size = ceil((double)max / (double)16);
  uint16_t *bitmap = (uint16_t *)calloc(map_size, sizeof(uint16_t));
  FILE *input, *output;
  input = fopen(infile, "r");
  while (fscanf(input, "%d", &datum) != EOF) {
    bitmap[datum / 16] ^= 1 << (datum % 16);
  }
  fclose(input);
  output = fopen(outfile, "w");
  for (i = 0; i < map_size; ++i) {
    for (j = 0; j < 16; ++j) {
      if (bitmap[i] & (1 << j)) {
        fprintf(output, "%d\n", i * 16 + j);
      }
    }
  }
  fclose(output);
  free(bitmap);
}


x86 assembly (clang, -O0):
<bitmap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  0xe70(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1040 <ceil@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xe3e(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <fscanf@plt>
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
je     1226 <bitmap_sort+0xb6>
mov    -0x20(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    -0x54(%rbp),%edx
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
xor    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
jmp    11c1 <bitmap_sort+0x51>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12c8 <bitmap_sort+0x158>
movl   $0x0,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jge    12b5 <bitmap_sort+0x145>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x28(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     12a2 <bitmap_sort+0x132>
mov    -0x48(%rbp),%rdi
mov    -0x24(%rbp),%edx
shl    $0x4,%edx
add    -0x28(%rbp),%edx
lea    0xd72(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12a7 <bitmap_sort+0x137>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    125d <bitmap_sort+0xed>
jmp    12ba <bitmap_sort+0x14a>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    124a <bitmap_sort+0xda>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitmap_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbp
cvtsi2sd %edx,%xmm0
mulsd  0xe6e(%rip),%xmm0
call   1040 <ceil@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
lea    0xe58(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbp
lea    0xe48(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    0x2e12(%rip),%r14
cmp    (%r14),%rax
je     1225 <bitmap_sort+0xb5>
lea    0xe28(%rip),%rbx
lea    0xc(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    0xc(%rsp),%eax
mov    %eax,%ecx
and    $0xf,%cl
mov    $0x1,%edx
shl    %cl,%edx
lea    0xf(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x4,%ecx
movslq %ecx,%rax
xor    %edx,(%r12,%rax,4)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <fscanf@plt>
cmp    (%r14),%rax
jne    11f0 <bitmap_sort+0x80>
mov    %rbp,%rdi
call   1050 <fclose@plt>
lea    0xdd9(%rip),%rsi
mov    0x10(%rsp),%rdi
call   1090 <fopen@plt>
mov    %rax,%r13
test   %r15d,%r15d
jle    12a5 <bitmap_sort+0x135>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
lea    0xdb8(%rip),%r14
xor    %ebp,%ebp
jmp    126e <bitmap_sort+0xfe>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
add    $0x10,%ebx
cmp    0x10(%rsp),%rbp
je     12a5 <bitmap_sort+0x135>
xor    %r15d,%r15d
jmp    128a <bitmap_sort+0x11a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r15d
cmp    $0x10,%r15d
je     1260 <bitmap_sort+0xf0>
mov    (%r12,%rbp,4),%eax
bt     %r15d,%eax
jae    1280 <bitmap_sort+0x110>
lea    (%rbx,%r15,1),%edx
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <fprintf@plt>
jmp    1280 <bitmap_sort+0x110>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32* @bitmap_sort(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%15 = load i32, i32* %7
%16 = sitofp i32 %15 to double
%17 = fdiv double %16, 1.600000e+01
%18 = call i32 @ceil(double %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = call i64 @calloc(i32 %19, i32 4)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %12
%22 = load i8*, i8** %5
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %13
br label %24
24: ; preds = %29, %3
%25 = load i32*, i32** %13
%26 = call i64 @fscanf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%27 = load i64, i64* @EOF
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = load i32, i32* %8
%31 = srem i32 %30, 16
%32 = shl i32 1, %31
%33 = load i32*, i32** %12
%34 = load i32, i32* %8
%35 = sdiv i32 %34, 16
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
%39 = xor i32 %38, %32
store i32 %39, i32* %37
br label %24
40: ; preds = %24
%41 = load i32*, i32** %13
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %6
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %44, i32** %14
store i32 0, i32* %9
br label %45
45: ; preds = %75, %40
%46 = load i32, i32* %9
%47 = load i32, i32* %11
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %78
49: ; preds = %45
store i32 0, i32* %10
br label %50
50: ; preds = %71, %49
%51 = load i32, i32* %10
%52 = icmp slt i32 %51, 16
br i1 %52, label %53, label %74
53: ; preds = %50
%54 = load i32*, i32** %12
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %57
%59 = load i32, i32* %10
%60 = shl i32 1, %59
%61 = and i32 %58, %60
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %53
%64 = load i32*, i32** %14
%65 = load i32, i32* %9
%66 = mul nsw i32 %65, 16
%67 = load i32, i32* %10
%68 = add nsw i32 %66, %67
%69 = call i32 @fprintf(i32* %64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %68)
br label %70
70: ; preds = %63, %53
br label %71
71: ; preds = %70
%72 = load i32, i32* %10
%73 = add nsw i32 %72, 1
store i32 %73, i32* %10
br label %50
74: ; preds = %50
br label %75
75: ; preds = %74
%76 = load i32, i32* %9
%77 = add nsw i32 %76, 1
store i32 %77, i32* %9
br label %45
78: ; preds = %45
%79 = load i32*, i32** %14
%80 = call i32 @fclose(i32* %79)
%81 = load i32*, i32** %12
%82 = call i32 @free(i32* %81)
%83 = load i32*, i32** %4
ret i32* %83
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define noalias i32* @bitmap_sort(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = sitofp i32 %2 to double
%7 = fmul double %6, 6.250000e-02
%8 = tail call i32 @ceil(double %7)
%9 = tail call i64 @calloc(i32 %8, i32 4)
%10 = inttoptr i64 %9 to i32*
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = call i64 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %27, label %15
15: ; preds = %3, %15
%16 = load i32, i32* %4
%17 = and i32 %16, 15
%18 = shl nuw nsw i32 1, %17
%19 = sdiv i32 %16, 16
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %10, i64 %20
%22 = load i32, i32* %21
%23 = xor i32 %18, %22
store i32 %23, i32* %21
%24 = call i64 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%25 = load i64, i64* @EOF
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %15
27: ; preds = %15, %3
%28 = call i32 @fclose(i32* %11)
%29 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp sgt i32 %8, 0
br i1 %30, label %31, label %53
31: ; preds = %27
%32 = zext i32 %8 to i64
br label %33
33: ; preds = %31, %50
%34 = phi i64 [ 0, %31 ], [ %51, %50 ]
%35 = getelementptr inbounds i32, i32* %10, i64 %34
%36 = trunc i64 %34 to i32
%37 = shl nsw i32 %36, 4
br label %38
38: ; preds = %33, %47
%39 = phi i32 [ 0, %33 ], [ %48, %47 ]
%40 = load i32, i32* %35
%41 = shl nuw nsw i32 1, %39
%42 = and i32 %40, %41
%43 = icmp eq i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %38
%45 = add nuw nsw i32 %39, %37
%46 = call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %38, %44
%48 = add nuw nsw i32 %39, 1
%49 = icmp eq i32 %48, 16
br i1 %49, label %50, label %38
50: ; preds = %47
%51 = add nuw nsw i64 %34, 1
%52 = icmp eq i64 %51, %32
br i1 %52, label %53, label %33
53: ; preds = %50, %27
%54 = call i32 @fclose(i32* %29)
%55 = call i32 @free(i32* %10)
ret i32* undef
}