#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*/ uint8_t;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ counting_sort(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int write(int, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  int fd, file_size;
  ssize_t bytes_read, bytes_written;
  uint8_t *buffer, *sorted;
  if (argc != 2) {
    err(1, "A path to a binary file must be passed as a single argument!");
  }
  if ((fd = open(argv[1], O_RDWR, S_IWUSR)) == -1) {
    err(1, "Cannot open file for read/write!");
  }
  file_size = lseek(fd, 0, SEEK_END);
  buffer = (uint8_t *)malloc(sizeof(uint8_t) * file_size);
  sorted = (uint8_t *)malloc(sizeof(uint8_t) * file_size);
  lseek(fd, 0, SEEK_SET);
  bytes_read = read(fd, buffer, file_size);
  if (bytes_read != file_size) {
    err(1, "Error while reading file!");
  }
  counting_sort(buffer, sorted, file_size);
  lseek(fd, 0, SEEK_SET);
  bytes_written = write(fd, sorted, file_size);
  if (bytes_written != file_size) {
    err(1, "Error while writing file!");
  }
  free(sorted);
  free(buffer);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c1 <main+0x31>
mov    $0x1,%edi
lea    0xe44(%rip),%rsi
call   1090 <err@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fd <main+0x6d>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
call   1090 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     127b <main+0xeb>
mov    $0x1,%edi
lea    0xde8(%rip),%rsi
call   1090 <err@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <counting_sort@plt>
mov    -0x14(%rbp),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     12cd <main+0x13d>
mov    $0x1,%edi
lea    0xdb0(%rip),%rsi
call   1090 <err@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11b1 <main+0x21>
lea    0xe59(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x8(%rbx),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
call   10b0 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11e5 <main+0x55>
lea    0xe62(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebx
lea    0x0(,%rbx,4),%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    0x2dac(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1080 <read@plt>
cmp    %ebx,%eax
je     124b <main+0xbb>
lea    0xe1d(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <counting_sort@plt>
mov    0x0(%rbp),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1287 <main+0xf7>
lea    0xdfb(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [61 x i8] c"A path to a binary file must be passed as a single argument!\00"
@O_RDWR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [33 x i8] c"Cannot open file for read/write!\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [26 x i8] c"Error while reading file!\00"
@.str.3 = [26 x i8] c"Error while writing file!\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 i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @S_IWUSR
%22 = call i32 @open(i8* %19, i32 %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
%27 = load i32, i32* %6
%28 = load i32, i32* @SEEK_END
%29 = call i32 @lseek(i32 %27, i32 0, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = mul i64 4, %31
%33 = trunc i64 %32 to i32
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i32*
store i32* %35, i32** %10
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = mul i64 4, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i32*
store i32* %41, i32** %11
%42 = load i32, i32* %6
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @lseek(i32 %42, i32 0, i32 %43)
%45 = load i32, i32* %6
%46 = load i32*, i32** %10
%47 = load i32, i32* %7
%48 = call i32 @read(i32 %45, i32* %46, i32 %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = load i32, i32* %7
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %26
%53 = call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %26
%55 = load i32*, i32** %10
%56 = load i32*, i32** %11
%57 = load i32, i32* %7
%58 = call i32 @counting_sort(i32* %55, i32* %56, i32 %57)
%59 = load i32, i32* %6
%60 = load i32, i32* @SEEK_SET
%61 = call i32 @lseek(i32 %59, i32 0, i32 %60)
%62 = load i32, i32* %6
%63 = load i32*, i32** %11
%64 = load i32, i32* %7
%65 = call i32 @write(i32 %62, i32* %63, i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = load i32, i32* %7
%68 = icmp ne i32 %66, %67
br i1 %68, label %69, label %71
69: ; preds = %54
%70 = call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %71
71: ; preds = %69, %54
%72 = load i32*, i32** %11
%73 = call i32 @free(i32* %72)
%74 = load i32*, i32** %10
%75 = call i32 @free(i32* %74)
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [61 x i8] c"A path to a binary file must be passed as a single argument!\00"
@O_RDWR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [33 x i8] c"Cannot open file for read/write!\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [26 x i8] c"Error while reading file!\00"
@.str.3 = [26 x i8] c"Error while writing file!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @S_IWUSR
%11 = tail call i32 @open(i8* %8, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %6
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @lseek(i32 %11, i32 0, i32 %16)
%18 = shl i32 %17, 2
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
%21 = tail call i64 @malloc(i32 %18)
%22 = inttoptr i64 %21 to i32*
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @lseek(i32 %11, i32 0, i32 %23)
%25 = tail call i32 @read(i32 %11, i32* %20, i32 %17)
%26 = icmp eq i32 %25, %17
br i1 %26, label %29, label %27
27: ; preds = %15
%28 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %15
%30 = tail call i32 @counting_sort(i32* %20, i32* %22, i32 %17)
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @lseek(i32 %11, i32 0, i32 %31)
%33 = tail call i32 @write(i32 %11, i32* %22, i32 %17)
%34 = icmp eq i32 %33, %17
br i1 %34, label %37, label %35
35: ; preds = %29
%36 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = tail call i32 @free(i32* %22)
%39 = tail call i32 @free(i32* %20)
%40 = tail call i32 @close(i32 %11)
ret i32 0
}