#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};
struct CT {
  int dummy;
};
typedef int /*<<< orphan*/ off_t;
typedef scalar_t__ clock_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
double CLOCKS_PER_SEC;
int EXIT_FAILURE;
int /*<<< orphan*/ POSIX_FADV_SEQUENTIAL;
scalar_t__ clock();
int counting_sort(int /*<<< orphan*/ *, struct CT *, size_t);
int /*<<< orphan*/ ct_free(struct CT *);
struct CT *ct_new();
int /*<<< orphan*/ ct_out(struct CT *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ posix_fadvise(int /*<<< orphan*/, int /*<<< orphan*/,
                                 int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ readahead(int /*<<< orphan*/, int /*<<< orphan*/,
                             int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
size_t strtol(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  FILE *f;
  off_t filesize;
  struct stat sb;
  struct CT *T;
  clock_t start, end;
  int n;
  size_t bufn = 100;
  if (argc < 2) {
    fprintf(stderr, "%s <filename> [buffer elements=100]\n", argv[0]);
    return EXIT_FAILURE;
  }
  f = fopen(argv[1], "rb");
  if (f == NULL) {
    fprintf(stderr, "Failed to open file %s\n", argv[1]);
    return EXIT_FAILURE;
  }
  if (argc == 3)
    bufn = strtol(argv[2], NULL, 0);
  if (stat(argv[1], &sb)) {
    perror("stat");
    exit(EXIT_FAILURE);
  }
  filesize = sb.st_size;
  posix_fadvise(fileno(f), 0, 0, POSIX_FADV_SEQUENTIAL);
  readahead(fileno(f), 0, filesize);
  start = clock();
  T = ct_new();
  n = counting_sort(f, T, bufn);
  end = clock();
  ct_out(T);
  ct_free(T);
  fclose(f);
  fprintf(stderr, "%ld bytes sorted in %f seconds\n", filesize,
          (double)(end - start) / CLOCKS_PER_SEC);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x64,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1247 <main+0x57>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <main+0x1c2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdcf(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    129a <main+0xaa>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <main+0x1c2>
cmpl   $0x3,-0x8(%rbp)
jne    12bb <main+0xcb>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12f0 <main+0x100>
lea    0xd63(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
mov    0x2cc8(%rip),%rax
mov    (%rax),%ecx
xor    %edx,%edx
mov    %edx,%esi
call   1060 <posix_fadvise@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1110 <readahead@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1100 <ct_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1050 <counting_sort@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   10e0 <ct_out@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <ct_free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
movslq -0x1c(%rbp),%rdx
mov    -0x38(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2c4f(%rip),%rax
divsd  (%rax),%xmm0
lea    0xca1(%rip),%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     121f <main+0x2f>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xde6(%rip),%rsi
jmp    1331 <main+0x141>
mov    %edi,%ebx
mov    0x8(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     131d <main+0x12d>
mov    %rax,%r13
mov    $0x64,%eax
cmp    $0x3,%ebx
jne    1254 <main+0x64>
mov    0x10(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rbp),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1354 <main+0x164>
mov    0x8(%rsp),%r14d
mov    %r13,%rdi
call   10a0 <fileno@plt>
mov    0x2d4c(%rip),%rcx
mov    (%rcx),%ecx
xor    %r15d,%r15d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <posix_fadvise@plt>
mov    %r13,%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    %r14d,%edx
call   1110 <readahead@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1100 <ct_new@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %rax,%rsi
mov    0x10(%rsp),%rdx
call   1050 <counting_sort@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   10e0 <ct_out@plt>
mov    %rbx,%rdi
call   10b0 <ct_free@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
sub    %r12,%rbp
cvtsi2sd %rbp,%xmm0
movslq %r14d,%rdx
mov    0x2cdf(%rip),%rax
divsd  (%rax),%xmm0
lea    0xd31(%rip),%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
jmp    1342 <main+0x152>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xcf7(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce5(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.CT = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"%s <filename> [buffer elements=100]\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Failed to open file %s\0A\00"
@.str.3 = [5 x i8] c"stat\00"
@POSIX_FADV_SEQUENTIAL = global i32 0
@.str.4 = [32 x i8] c"%ld bytes sorted in %f seconds\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+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 %struct.stat
%9 = alloca %struct.CT*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 100, i64* %13
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
store i32 %22, i32* %3
br label %90
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
%28 = load i32*, i32** %6
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %37
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %3
br label %90
37: ; preds = %23
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, 3
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i64 @strtol(i8* %43, i32* null, i32 0)
store i64 %44, i64* %13
br label %45
45: ; preds = %40, %37
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i64 @stat(i8* %48, %struct.stat* %8)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %45
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%57 = load i32, i32* %56
store i32 %57, i32* %7
%58 = load i32*, i32** %6
%59 = call i32 @fileno(i32* %58)
%60 = load i32, i32* @POSIX_FADV_SEQUENTIAL
%61 = call i32 @posix_fadvise(i32 %59, i32 0, i32 0, i32 %60)
%62 = load i32*, i32** %6
%63 = call i32 @fileno(i32* %62)
%64 = load i32, i32* %7
%65 = call i32 @readahead(i32 %63, i32 0, i32 %64)
%66 = call i64 (...) @clock()
store i64 %66, i64* %10
%67 = call %struct.CT* (...) @ct_new()
store %struct.CT* %67, %struct.CT** %9
%68 = load i32*, i32** %6
%69 = load %struct.CT*, %struct.CT** %9
%70 = load i64, i64* %13
%71 = call i32 @counting_sort(i32* %68, %struct.CT* %69, i64 %70)
store i32 %71, i32* %12
%72 = call i64 (...) @clock()
store i64 %72, i64* %11
%73 = load %struct.CT*, %struct.CT** %9
%74 = call i32 @ct_out(%struct.CT* %73)
%75 = load %struct.CT*, %struct.CT** %9
%76 = call i32 @ct_free(%struct.CT* %75)
%77 = load i32*, i32** %6
%78 = call i32 @fclose(i32* %77)
%79 = load i32, i32* @stderr
%80 = load i32, i32* %7
%81 = sext i32 %80 to i64
%82 = inttoptr i64 %81 to i8*
%83 = load i64, i64* %11
%84 = load i64, i64* %10
%85 = sub nsw i64 %83, %84
%86 = sitofp i64 %85 to double
%87 = load double, double* @CLOCKS_PER_SEC
%88 = fdiv double %86, %87
%89 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %82, double %88)
store i32 0, i32* %3
br label %90
90: ; preds = %55, %30, %16
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.CT = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"%s <filename> [buffer elements=100]\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Failed to open file %s\0A\00"
@.str.3 = [5 x i8] c"stat\00"
@POSIX_FADV_SEQUENTIAL = global i32 0
@.str.4 = [32 x i8] c"%ld bytes sorted in %f seconds\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
br label %59
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %12
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
br label %59
21: ; preds = %11
%22 = icmp eq i32 %0, 3
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i64 @strtol(i8* nocapture %25, i32* null, i32 0)
br label %27
27: ; preds = %23, %21
%28 = phi i64 [ %26, %23 ], [ 100, %21 ]
%29 = load i8*, i8** %12
%30 = call i64 @stat(i8* %29, %struct.stat* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %27
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @fileno(i32* %14)
%40 = load i32, i32* @POSIX_FADV_SEQUENTIAL
%41 = call i32 @posix_fadvise(i32 %39, i32 0, i32 0, i32 %40)
%42 = call i32 @fileno(i32* %14)
%43 = call i32 @readahead(i32 %42, i32 0, i32 %38)
%44 = call i64 (...) @clock()
%45 = call %struct.CT* (...) @ct_new()
%46 = call i32 @counting_sort(i32* %14, %struct.CT* %45, i64 %28)
%47 = call i64 (...) @clock()
%48 = call i32 @ct_out(%struct.CT* %45)
%49 = call i32 @ct_free(%struct.CT* %45)
%50 = call i32 @fclose(i32* %14)
%51 = load i32, i32* @stderr
%52 = sext i32 %38 to i64
%53 = inttoptr i64 %52 to i8*
%54 = sub nsw i64 %47, %44
%55 = sitofp i64 %54 to double
%56 = load double, double* @CLOCKS_PER_SEC
%57 = fdiv double %55, %56
%58 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %53, double %57)
br label %59
59: ; preds = %36, %16, %6
%60 = phi i32 [ %10, %6 ], [ %20, %16 ], [ 0, %36 ]
ret i32 %60
}