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

/* Variables and functions */
int LotDocIDOfset(int);
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int rLotForDOCid(int);
int /*<<< orphan*/ rSendFileByOpenHandler(int /*<<< orphan*/ *, char *, int,
                                          char *, char *);
int /*<<< orphan*/ *tmpfile();
int main() {
  FILE *FH, *LOTFILE;
  struct stat inode;
  int nrOfElements;
  int LotNr, DocIDPlace, oldLotNr, i, n, rank;
  if ((FH = fopen("/home/boitho/config/shortRank", "rb")) == NULL) {
    perror("open");
  }
  fstat(fileno(FH), &inode);
  nrOfElements = inode.st_size;
  oldLotNr = -1;
  for (i = 0; i < nrOfElements; i++) {
    if ((n = fread(&rank, sizeof(unsigned char), 1, FH)) == -1) {
      perror("read");
    }
    LotNr = rLotForDOCid(i);
    DocIDPlace = (i - LotDocIDOfset(LotNr));
    if (LotNr != oldLotNr) {
      if (oldLotNr != -1) {
        rSendFileByOpenHandler(LOTFILE, "Brank", oldLotNr, "w", "www");
        close(LOTFILE);
      }
      LOTFILE = tmpfile();
      printf("lot %i\n", LotNr);
    }
    oldLotNr = LotNr;
    fseek(LOTFILE, DocIDPlace, SEEK_SET);
    fwrite(&rank, sizeof(unsigned char), 1, LOTFILE);
  }
  rSendFileByOpenHandler(LOTFILE, "Brank", oldLotNr, "w", "www");
  close(LOTFILE);
  close(FH);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe1a(%rip),%rdi
lea    0xe31(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    120c <main+0x3c>
lea    0xe1a(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   10f0 <fstat@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0xffffffff,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1326 <main+0x156>
mov    -0x10(%rbp),%rcx
lea    -0x3c(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    126c <main+0x9c>
lea    0xdbf(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x34(%rbp),%edi
call   10d0 <rLotForDOCid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%edi
call   10e0 <LotDocIDOfset@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     12e9 <main+0x119>
cmpl   $0xffffffff,-0x30(%rbp)
je     12cf <main+0xff>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xd78(%rip),%rsi
lea    0xd79(%rip),%rcx
lea    0xd70(%rip),%r8
call   1070 <rSendFileByOpenHandler@plt>
mov    -0x18(%rbp),%rdi
call   1050 <close@plt>
mov    $0x0,%al
call   1060 <tmpfile@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%esi
lea    0xd51(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rcx
lea    -0x3c(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10c0 <fwrite@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1234 <main+0x64>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xcf7(%rip),%rsi
lea    0xcf8(%rip),%rcx
lea    0xcef(%rip),%r8
call   1070 <rSendFileByOpenHandler@plt>
mov    -0x18(%rbp),%rdi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe1b(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     133a <main+0x16a>
mov    %rbx,%rdi
call   1090 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   10f0 <fstat@plt>
mov    $0xffffffff,%ebp
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
xor    %r15d,%r15d
mov    %rbx,0x10(%rsp)
jmp    127d <main+0xad>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1070 <tmpfile@plt>
mov    %rax,%r12
lea    0xdf4(%rip),%rdi
mov    %r13d,%esi
call   1040 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r14d,%esi
call   10a0 <fseek@plt>
movsbl 0xc(%rsp),%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
add    $0x1,%r15d
mov    %r13d,%ebp
cmp    %r15d,0x8(%rsp)
mov    0x10(%rsp),%rbx
je     12f9 <main+0x129>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0xffffffff,%eax
je     12eb <main+0x11b>
mov    %r15d,%edi
call   10d0 <rLotForDOCid@plt>
mov    %eax,%r13d
mov    %eax,%edi
call   10e0 <LotDocIDOfset@plt>
mov    %r15d,%r14d
sub    %eax,%r14d
cmp    %ebp,%r13d
je     1249 <main+0x79>
cmp    $0xffffffff,%ebp
je     1230 <main+0x60>
mov    %r12,%rdi
lea    0xd62(%rip),%rsi
mov    %ebp,%edx
lea    0xd61(%rip),%rcx
lea    0xd58(%rip),%r8
call   1080 <rSendFileByOpenHandler@plt>
mov    %r12,%rdi
call   1050 <close@plt>
jmp    1230 <main+0x60>
lea    0xd34(%rip),%rdi
call   10c0 <perror@plt>
jmp    1299 <main+0xc9>
lea    0xd2b(%rip),%rsi
lea    0xd2c(%rip),%rcx
lea    0xd23(%rip),%r8
mov    %r12,%rdi
mov    %r13d,%edx
call   1080 <rSendFileByOpenHandler@plt>
mov    %r12,%rdi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce0(%rip),%rdi
call   10c0 <perror@plt>
jmp    11fd <main+0x2d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [30 x i8] c"/home/boitho/config/shortRank\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [6 x i8] c"Brank\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [4 x i8] c"www\00"
@.str.7 = [8 x i8] c"lot %i\0A\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
%12 = call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32*, i32** %2
%18 = call i32 @fileno(i32* %17)
%19 = call i32 @fstat(i32 %18, %struct.stat* %4)
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %5
store i32 -1, i32* %8
store i32 0, i32* %9
br label %22
22: ; preds = %63, %16
%23 = load i32, i32* %9
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %66
26: ; preds = %22
%27 = load i32*, i32** %2
%28 = call i32 @fread(i32* %11, i32 1, i32 1, i32* %27)
store i32 %28, i32* %10
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i32, i32* %9
%34 = call i32 @rLotForDOCid(i32 %33)
store i32 %34, i32* %6
%35 = load i32, i32* %9
%36 = load i32, i32* %6
%37 = call i32 @LotDocIDOfset(i32 %36)
%38 = sub nsw i32 %35, %37
store i32 %38, i32* %7
%39 = load i32, i32* %6
%40 = load i32, i32* %8
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %55
42: ; preds = %32
%43 = load i32, i32* %8
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = load i32*, i32** %3
%47 = load i32, i32* %8
%48 = call i32 @rSendFileByOpenHandler(i32* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%49 = load i32*, i32** %3
%50 = call i32 @close(i32* %49)
br label %51
51: ; preds = %45, %42
%52 = call i32* (...) @tmpfile()
store i32* %52, i32** %3
%53 = load i32, i32* %6
%54 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %51, %32
%56 = load i32, i32* %6
store i32 %56, i32* %8
%57 = load i32*, i32** %3
%58 = load i32, i32* %7
%59 = load i32, i32* @SEEK_SET
%60 = call i32 @fseek(i32* %57, i32 %58, i32 %59)
%61 = load i32*, i32** %3
%62 = call i32 @fwrite(i32* %11, i32 1, i32 1, i32* %61)
br label %63
63: ; preds = %55
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %22
66: ; preds = %22
%67 = load i32*, i32** %3
%68 = load i32, i32* %8
%69 = call i32 @rSendFileByOpenHandler(i32* %67, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%70 = load i32*, i32** %3
%71 = call i32 @close(i32* %70)
%72 = load i32*, i32** %2
%73 = call i32 @close(i32* %72)
%74 = load i32, i32* %1
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [30 x i8] c"/home/boitho/config/shortRank\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [6 x i8] c"Brank\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [4 x i8] c"www\00"
@.str.7 = [8 x i8] c"lot %i\0A\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca i32
%3 = bitcast %struct.stat* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = tail call i32 @fileno(i32* %5)
%11 = call i32 @fstat(i32 %10, %struct.stat* %1)
%12 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp sgt i32 %13, 0
br label %15
15: ; preds = %9, %36
%16 = phi i32* [ %37, %36 ], [ undef, %9 ]
%17 = phi i32 [ %43, %36 ], [ 0, %9 ]
%18 = phi i32 [ %24, %36 ], [ -1, %9 ]
%19 = call i32 @fread(i32* %2, i32 1, i32 1, i32* %5)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = tail call i32 @rLotForDOCid(i32 %17)
%25 = tail call i32 @LotDocIDOfset(i32 %24)
%26 = sub nsw i32 %17, %25
%27 = icmp eq i32 %24, %18
br i1 %27, label %36, label %28
28: ; preds = %23
%29 = icmp eq i32 %18, -1
br i1 %29, label %33, label %30
30: ; preds = %28
%31 = tail call i32 @rSendFileByOpenHandler(i32* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%32 = tail call i32 @close(i32* %16)
br label %33
33: ; preds = %30, %28
%34 = tail call i32* (...) @tmpfile()
%35 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %24)
br label %36
36: ; preds = %33, %23
%37 = phi i32* [ %34, %33 ], [ %16, %23 ]
%38 = load i32, i32* @SEEK_SET
%39 = tail call i32 @fseek(i32* %37, i32 %26, i32 %38)
%40 = load i8, i8* %4
%41 = sext i8 %40 to i32
%42 = tail call i32 @fputc(i32 %41, i32* %37)
%43 = add nuw nsw i32 %17, 1
%44 = icmp eq i32 %43, %13
br i1 %44, label %45, label %15
45: ; preds = %36
%46 = tail call i32 @rSendFileByOpenHandler(i32* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%47 = tail call i32 @close(i32* %37)
%48 = tail call i32 @close(i32* %5)
ret i32 0
}