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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int log10(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int LongRank;
  unsigned char ShortRank;
  unsigned int convertetrank;
  unsigned int DocID;
  FILE *LONGFILE;
  FILE *SHORTFILE;
  if (argc < 3) {
    printf("Dette programet konverterer en 4 byte til 1 byte rank "
           "file\nBruk\n\n\tConvertRankToShortRankFile LONGFILE SHORTFILE\n\n");
    exit(0);
  }
  printf("%s -> %s\n", argv[1], argv[2]);
  if ((LONGFILE = fopen(argv[1], "rb")) == NULL) {
    perror(argv[1]);
    exit(1);
  }
  if ((SHORTFILE = fopen(argv[2], "wb")) == NULL) {
    perror(argv[2]);
    exit(1);
  }
  DocID = 0;
  while (!feof(LONGFILE)) {
    if (fread(&LongRank, sizeof(LongRank), 1, LONGFILE) != 1) {
    }
    if (LongRank != 0) {
      convertetrank = (int)(log10(LongRank) * 53);
      if (convertetrank > 255) {
        convertetrank = 255;
      }
      ShortRank = convertetrank;
    } else {
      ShortRank = 0;
    }
    if (fwrite(&ShortRank, sizeof(ShortRank), 1, SHORTFILE) != 1) {
      perror("fwrite");
    }
    DocID++;
  }
  fclose(LONGFILE);
  fclose(SHORTFILE);
}


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   $0x3,-0x8(%rbp)
jge    11c5 <main+0x35>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    121c <main+0x8c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe54(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1255 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    127a <main+0xea>
jmp    1311 <main+0x181>
mov    -0x28(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     129f <main+0x10f>
jmp    129f <main+0x10f>
cmpl   $0x0,-0x14(%rbp)
je     12d6 <main+0x146>
mov    -0x14(%rbp),%edi
call   1090 <log10@plt>
imul   $0x35,%eax,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xff,-0x1c(%rbp)
jbe    12cb <main+0x13b>
movl   $0xff,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,-0x15(%rbp)
jmp    12da <main+0x14a>
movb   $0x0,-0x15(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0x15(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10b0 <fwrite@plt>
cmp    $0x1,%eax
je     1303 <main+0x173>
lea    0xd84(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    125c <main+0xcc>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%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    12b5 <main+0x115>
mov    %rsi,%r15
mov    0x8(%rsi),%rsi
mov    0x10(%r15),%rdx
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%r15),%rdi
lea    0xe32(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12c8 <main+0x128>
mov    %rax,%rbx
mov    0x10(%r15),%rdi
lea    0xe19(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12ce <main+0x12e>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1232 <main+0x92>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x4(%rsp),%r15
lea    0x3(%rsp),%r12
lea    0xdcd(%rip),%r13
mov    $0xff,%ebp
jmp    128a <main+0xea>
nopw   0x0(%rax,%rax,1)
call   10a0 <log10@plt>
imul   $0x35,%eax,%eax
cmp    $0xff,%eax
cmovae %ebp,%eax
mov    %al,0x3(%rsp)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
cmp    $0x1,%eax
jne    12ab <main+0x10b>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1211 <main+0x71>
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
jne    1250 <main+0xb0>
xor    %eax,%eax
jmp    1260 <main+0xc0>
mov    %r13,%rdi
call   1090 <perror@plt>
jmp    127e <main+0xde>
lea    0xd5b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x8(%r15),%rdi
jmp    12d2 <main+0x132>
mov    0x10(%r15),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [114 x i8] c"Dette programet konverterer en 4 byte til 1 byte rank file\0ABruk\0A\0A\09ConvertRankToShortRankFile LONGFILE SHORTFILE\0A\0A\00"
@.str.1 = [10 x i8] c"%s -> %s\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [7 x i8] c"fwrite\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%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 slt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([114 x i8], [114 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %23)
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %10
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %36
30: ; preds = %17
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @perror(i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %17
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %40, i32** %11
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %48
42: ; preds = %36
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = call i32 @perror(i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %36
store i32 0, i32* %9
br label %49
49: ; preds = %79, %48
%50 = load i32*, i32** %10
%51 = call i32 @feof(i32* %50)
%52 = icmp ne i32 %51, 0
%53 = xor i1 %52, true
br i1 %53, label %54, label %82
54: ; preds = %49
%55 = load i32*, i32** %10
%56 = call i32 @fread(i32* %6, i32 4, i32 1, i32* %55)
%57 = icmp ne i32 %56, 1
br i1 %57, label %58, label %59
58: ; preds = %54
br label %59
59: ; preds = %58, %54
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %72
62: ; preds = %59
%63 = load i32, i32* %6
%64 = call i32 @log10(i32 %63)
%65 = mul nsw i32 %64, 53
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp ugt i32 %66, 255
br i1 %67, label %68, label %69
68: ; preds = %62
store i32 255, i32* %8
br label %69
69: ; preds = %68, %62
%70 = load i32, i32* %8
%71 = trunc i32 %70 to i8
store i8 %71, i8* %7
br label %73
72: ; preds = %59
store i8 0, i8* %7
br label %73
73: ; preds = %72, %69
%74 = load i32*, i32** %11
%75 = call i32 @fwrite(i8* %7, i32 1, i32 1, i32* %74)
%76 = icmp ne i32 %75, 1
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %79
79: ; preds = %77, %73
%80 = load i32, i32* %9
%81 = add i32 %80, 1
store i32 %81, i32* %9
br label %49
82: ; preds = %49
%83 = load i32*, i32** %10
%84 = call i32 @fclose(i32* %83)
%85 = load i32*, i32** %11
%86 = call i32 @fclose(i32* %85)
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"%s -> %s\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [7 x i8] c"fwrite\00"
@str = [113 x i8] c"Dette programet konverterer en 4 byte til 1 byte rank file\0ABruk\0A\0A\09ConvertRankToShortRankFile LONGFILE SHORTFILE\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i8
%5 = bitcast i32* %3 to i8*
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([113 x i8], [113 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %14)
%16 = load i8*, i8** %11
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %10
%20 = load i8*, i8** %11
%21 = tail call i32 @perror(i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %10
%24 = load i8*, i8** %13
%25 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = tail call i32 @feof(i32* %17)
%29 = icmp eq i32 %28, 0
br i1 %29, label %34, label %53
30: ; preds = %23
%31 = load i8*, i8** %13
%32 = tail call i32 @perror(i8* %31)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27, %50
%35 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %17)
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %34
%39 = tail call i32 @log10(i32 %36)
%40 = mul nsw i32 %39, 53
%41 = icmp ult i32 %40, 255
%42 = select i1 %41, i32 %40, i32 255
%43 = trunc i32 %42 to i8
br label %44
44: ; preds = %34, %38
%45 = phi i8 [ %43, %38 ], [ 0, %34 ]
store i8 %45, i8* %4
%46 = call i32 @fwrite(i8* %4, i32 1, i32 1, i32* %25)
%47 = icmp eq i32 %46, 1
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = tail call i32 @feof(i32* %17)
%52 = icmp eq i32 %51, 0
br i1 %52, label %34, label %53
53: ; preds = %50, %27
%54 = tail call i32 @fclose(i32* %17)
%55 = tail call i32 @fclose(i32* %25)
ret i32 0
}