#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
char *calloc(long, int);
scalar_t__ close(int);
int compare_records(char *, char *);
int /*<<< orphan*/ free(char *);
int lseek(int, long, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
long read(int, char *, long);
long write(int, char *, long);
int sys_sort(const char *pathname, long record_size) {
  int file = open(pathname, O_RDWR);
  char *record = NULL;
  char *cur_record = NULL;
  if (file == -1) {
    perror("Can't open file.\n");
    return -2;
  } else {
    off_t begin_offset = lseek(file, 0, SEEK_SET);
    off_t end_offset = lseek(file, 0, SEEK_END);
    if (begin_offset == -1 || end_offset == -1) {
      goto sys_io_error;
    }
    off_t file_size = end_offset - begin_offset;
    long records_count = file_size / record_size;
    if (file_size % record_size != 0) {
      perror("Wrong size of file... Not integer count of records");
    }
    for (long i = 1; i < records_count; i++) {
      long last_offset = i * record_size;
      if (lseek(file, last_offset, SEEK_SET) == -1) {
        goto sys_io_error;
      }
      cur_record = calloc(record_size, sizeof(char));
      if (read(file, cur_record, record_size * sizeof(char)) !=
          record_size * sizeof(char)) {
        goto sys_io_error;
      }
      bool insert_flag = true;
      for (long offset = last_offset - record_size;
           offset >= begin_offset && insert_flag; offset -= record_size) {
        if (lseek(file, offset, SEEK_SET) == -1) {
          goto sys_io_error;
        }
        record = calloc(record_size, sizeof(char));
        if (read(file, record, record_size * sizeof(char)) !=
            record_size * sizeof(char)) {
          goto sys_io_error;
        }
        if (compare_records(cur_record, record) < 1) {
          if (write(file, record, record_size * sizeof(char)) !=
              record_size * sizeof(char)) {
            goto sys_io_error;
          }
          if (offset == begin_offset) {
            if (lseek(file, offset, SEEK_SET) == -1) {
              goto sys_io_error;
            }
            if (write(file, cur_record, record_size * sizeof(char)) !=
                record_size * sizeof(char)) {
              goto sys_io_error;
            }
          }
        } else {
          if (write(file, cur_record, record_size * sizeof(char)) !=
              record_size * sizeof(char)) {
            goto sys_io_error;
          }
          insert_flag = false;
        }
        if (record != NULL) {
          free(record);
          record = NULL;
        }
      }
      if (cur_record != NULL) {
        free(cur_record);
        cur_record = NULL;
      }
    }
  }
  if (file != 0) {
    if (close(file) != 0) {
      goto sys_io_error;
    }
    file = 0;
  }
  if (record != NULL) {
    free(record);
  }
  if (cur_record != NULL) {
    free(cur_record);
  }
  return 0;
sys_io_error:
  perror("Error during work with file.\n");
  if (file != 0) {
    close(file);
    file = 0;
  }
  if (record != NULL) {
    free(record);
  }
  if (cur_record != NULL) {
    free(cur_record);
  }
  return -1;
}


x86 assembly (clang, -O0):
<sys_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e7 <sys_sort+0x57>
lea    0xe2a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1580 <sys_sort+0x3f0>
mov    -0x1c(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
je     122b <sys_sort+0x9b>
cmpl   $0xffffffff,-0x38(%rbp)
jne    1230 <sys_sort+0xa0>
jmp    152c <sys_sort+0x39c>
mov    -0x38(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %rax,-0x48(%rbp)
movslq -0x3c(%rbp),%rax
cqto
idivq  -0x18(%rbp)
cmp    $0x0,%rdx
je     1267 <sys_sort+0xd7>
lea    0xdb0(%rip),%rdi
call   10b0 <perror@plt>
movq   $0x1,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x48(%rbp),%rax
jge    14cb <sys_sort+0x33b>
mov    -0x50(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12ad <sys_sort+0x11d>
jmp    152c <sys_sort+0x39c>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1070 <read@plt>
mov    -0x18(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     12e9 <sys_sort+0x159>
jmp    152c <sys_sort+0x39c>
movl   $0x1,-0x5c(%rbp)
mov    -0x58(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rcx
movslq -0x34(%rbp),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x69(%rbp)
jl     131c <sys_sort+0x18c>
cmpl   $0x0,-0x5c(%rbp)
setne  %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    132c <sys_sort+0x19c>
jmp    1499 <sys_sort+0x309>
mov    -0x1c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    0x2c96(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    134f <sys_sort+0x1bf>
jmp    152c <sys_sort+0x39c>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1070 <read@plt>
mov    -0x18(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     138b <sys_sort+0x1fb>
jmp    152c <sys_sort+0x39c>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <compare_records@plt>
cmp    $0x1,%eax
jge    1433 <sys_sort+0x2a3>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1040 <write@plt>
mov    -0x18(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     13cb <sys_sort+0x23b>
jmp    152c <sys_sort+0x39c>
mov    -0x68(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmp    %rcx,%rax
jne    142e <sys_sort+0x29e>
mov    -0x1c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    0x2be6(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    13ff <sys_sort+0x26f>
jmp    152c <sys_sort+0x39c>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1040 <write@plt>
mov    -0x18(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     1429 <sys_sort+0x299>
jmp    152c <sys_sort+0x39c>
jmp    142e <sys_sort+0x29e>
jmp    1464 <sys_sort+0x2d4>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1040 <write@plt>
mov    -0x18(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     145d <sys_sort+0x2cd>
jmp    152c <sys_sort+0x39c>
movl   $0x0,-0x5c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1480 <sys_sort+0x2f0>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    1485 <sys_sort+0x2f5>
mov    -0x18(%rbp),%rcx
mov    -0x68(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x68(%rbp)
jmp    12fc <sys_sort+0x16c>
cmpq   $0x0,-0x30(%rbp)
je     14b5 <sys_sort+0x325>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
jmp    14ba <sys_sort+0x32a>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    126f <sys_sort+0xdf>
jmp    14d0 <sys_sort+0x340>
cmpl   $0x0,-0x1c(%rbp)
je     14f8 <sys_sort+0x368>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     14f1 <sys_sort+0x361>
jmp    152c <sys_sort+0x39c>
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     150c <sys_sort+0x37c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
je     1520 <sys_sort+0x390>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1580 <sys_sort+0x3f0>
lea    0xb12(%rip),%rdi
call   10b0 <perror@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1551 <sys_sort+0x3c1>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1565 <sys_sort+0x3d5>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
je     1579 <sys_sort+0x3e9>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     13f6 <sys_sort+0x266>
mov    %eax,%r13d
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %r13d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebp
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%ebp
je     1462 <sys_sort+0x2d2>
mov    $0x0,%r15d
cmp    $0xffffffff,%eax
je     1465 <sys_sort+0x2d5>
sub    %ebp,%eax
movslq %eax,%rcx
mov    %rcx,%rdx
or     %rbx,%rdx
shr    $0x20,%rdx
je     1219 <sys_sort+0x89>
mov    %rcx,%rax
cqto
idiv   %rbx
jmp    121d <sys_sort+0x8d>
xor    %edx,%edx
div    %ebx
mov    %rax,0x10(%rsp)
test   %rdx,%rdx
jne    140c <sys_sort+0x27c>
cmpq   $0x2,0x10(%rsp)
jl     1424 <sys_sort+0x294>
movslq %ebp,%r14
mov    %rbx,%rax
neg    %rax
mov    %rax,0x20(%rsp)
mov    $0x1,%r15d
mov    %r13d,0x4(%rsp)
jmp    1274 <sys_sort+0xe4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rsp),%r15
add    $0x1,%r15
cmp    0x10(%rsp),%r15
je     1424 <sys_sort+0x294>
mov    %r15,%rbp
imul   %rbx,%rbp
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %rbp,%rsi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     145f <sys_sort+0x2cf>
mov    %r15,0x18(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r12
mov    %r13d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <read@plt>
cmp    %rbx,%rax
jne    1462 <sys_sort+0x2d2>
sub    %rbx,%rbp
cmp    %r14,%rbp
jge    12e0 <sys_sort+0x150>
test   %r12,%r12
je     1260 <sys_sort+0xd0>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1260 <sys_sort+0xd0>
nopl   0x0(%rax,%rax,1)
mov    %r12,0x8(%rsp)
jmp    1310 <sys_sort+0x180>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rax
add    %rbp,%rax
sub    %rbx,%rbp
cmp    %r14,%rax
setge  %al
test   %al,%r12b
mov    0x8(%rsp),%r12
mov    0x4(%rsp),%r13d
je     12cc <sys_sort+0x13c>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %rbp,%rsi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     1462 <sys_sort+0x2d2>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r15
mov    %r13d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <read@plt>
cmp    %rbx,%rax
jne    1465 <sys_sort+0x2d5>
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <compare_records@plt>
mov    %r13d,%edi
test   %eax,%eax
jle    1380 <sys_sort+0x1f0>
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    %rbx,%rax
jne    1465 <sys_sort+0x2d5>
xor    %r12d,%r12d
jmp    13e0 <sys_sort+0x250>
nop
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    %rbx,%rax
jne    1465 <sys_sort+0x2d5>
mov    $0x1,%r12b
cmp    %rbp,%r14
jne    13e0 <sys_sort+0x250>
mov    %r14,%rsi
mov    0x2c2a(%rip),%rax
mov    (%rax),%edx
mov    0x4(%rsp),%r13d
mov    %r13d,%edi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     144c <sys_sort+0x2bc>
mov    %r13d,%edi
mov    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    %rbx,%rax
jne    1453 <sys_sort+0x2c3>
nopw   0x0(%rax,%rax,1)
test   %r15,%r15
je     12f0 <sys_sort+0x160>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12f0 <sys_sort+0x160>
lea    0xc03(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xfffffffe,%ebp
jmp    149d <sys_sort+0x30d>
lea    0xbff(%rip),%rdi
call   10b0 <perror@plt>
cmpq   $0x2,0x10(%rsp)
jge    1237 <sys_sort+0xa7>
xor    %ebp,%ebp
test   %r13d,%r13d
je     149d <sys_sort+0x30d>
mov    %r13d,%edi
call   1060 <close@plt>
test   %rax,%rax
je     149d <sys_sort+0x30d>
lea    0xc06(%rip),%rdi
call   10b0 <perror@plt>
xor    %r12d,%r12d
xor    %r15d,%r15d
jmp    1476 <sys_sort+0x2e6>
mov    0x8(%rsp),%r12
jmp    1465 <sys_sort+0x2d5>
mov    0x8(%rsp),%r12
mov    0x4(%rsp),%r13d
jmp    1465 <sys_sort+0x2d5>
xor    %r12d,%r12d
xor    %r15d,%r15d
lea    0xbd9(%rip),%rdi
call   10b0 <perror@plt>
test   %r13d,%r13d
je     147e <sys_sort+0x2ee>
mov    %r13d,%edi
call   1060 <close@plt>
test   %r15,%r15
je     148b <sys_sort+0x2fb>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
test   %r12,%r12
je     149d <sys_sort+0x30d>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [18 x i8] c"Can't open file.\0A\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.1 = [51 x i8] c"Wrong size of file... Not integer count of records\00"
@.str.2 = [30 x i8] c"Error during work with file.\0A\00"
define i32 @sys_sort(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
store i8* null, i8** %7
store i8* null, i8** %8
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %3
br label %226
24: ; preds = %2
%25 = load i32, i32* %6
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i64 0, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %6
%29 = load i32, i32* @SEEK_END
%30 = call i32 @lseek(i32 %28, i64 0, i32 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, -1
br i1 %32, label %36, label %33
33: ; preds = %24
%34 = load i32, i32* %10
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %33, %24
br label %206
37: ; preds = %33
%38 = load i32, i32* %10
%39 = load i32, i32* %9
%40 = sub nsw i32 %38, %39
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = load i64, i64* %5
%44 = sdiv i64 %42, %43
store i64 %44, i64* %12
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = load i64, i64* %5
%48 = srem i64 %46, %47
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %37
%51 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %52
52: ; preds = %50, %37
store i64 1, i64* %13
br label %53
53: ; preds = %180, %52
%54 = load i64, i64* %13
%55 = load i64, i64* %12
%56 = icmp slt i64 %54, %55
br i1 %56, label %57, label %183
57: ; preds = %53
%58 = load i64, i64* %13
%59 = load i64, i64* %5
%60 = mul nsw i64 %58, %59
store i64 %60, i64* %14
%61 = load i32, i32* %6
%62 = load i64, i64* %14
%63 = load i32, i32* @SEEK_SET
%64 = call i32 @lseek(i32 %61, i64 %62, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %67
66: ; preds = %57
br label %206
67: ; preds = %57
%68 = load i64, i64* %5
%69 = call i8* @calloc(i64 %68, i32 1)
store i8* %69, i8** %8
%70 = load i32, i32* %6
%71 = load i8*, i8** %8
%72 = load i64, i64* %5
%73 = mul i64 %72, 1
%74 = call i64 @read(i32 %70, i8* %71, i64 %73)
%75 = load i64, i64* %5
%76 = mul i64 %75, 1
%77 = icmp ne i64 %74, %76
br i1 %77, label %78, label %79
78: ; preds = %67
br label %206
79: ; preds = %67
store i32 1, i32* %15
%80 = load i64, i64* %14
%81 = load i64, i64* %5
%82 = sub nsw i64 %80, %81
store i64 %82, i64* %16
br label %83
83: ; preds = %169, %79
%84 = load i64, i64* %16
%85 = load i32, i32* %9
%86 = sext i32 %85 to i64
%87 = icmp sge i64 %84, %86
br i1 %87, label %88, label %91
88: ; preds = %83
%89 = load i32, i32* %15
%90 = icmp ne i32 %89, 0
br label %91
91: ; preds = %88, %83
%92 = phi i1 [ false, %83 ], [ %90, %88 ]
br i1 %92, label %93, label %173
93: ; preds = %91
%94 = load i32, i32* %6
%95 = load i64, i64* %16
%96 = load i32, i32* @SEEK_SET
%97 = call i32 @lseek(i32 %94, i64 %95, i32 %96)
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %100
99: ; preds = %93
br label %206
100: ; preds = %93
%101 = load i64, i64* %5
%102 = call i8* @calloc(i64 %101, i32 1)
store i8* %102, i8** %7
%103 = load i32, i32* %6
%104 = load i8*, i8** %7
%105 = load i64, i64* %5
%106 = mul i64 %105, 1
%107 = call i64 @read(i32 %103, i8* %104, i64 %106)
%108 = load i64, i64* %5
%109 = mul i64 %108, 1
%110 = icmp ne i64 %107, %109
br i1 %110, label %111, label %112
111: ; preds = %100
br label %206
112: ; preds = %100
%113 = load i8*, i8** %8
%114 = load i8*, i8** %7
%115 = call i32 @compare_records(i8* %113, i8* %114)
%116 = icmp slt i32 %115, 1
br i1 %116, label %117, label %151
117: ; preds = %112
%118 = load i32, i32* %6
%119 = load i8*, i8** %7
%120 = load i64, i64* %5
%121 = mul i64 %120, 1
%122 = call i64 @write(i32 %118, i8* %119, i64 %121)
%123 = load i64, i64* %5
%124 = mul i64 %123, 1
%125 = icmp ne i64 %122, %124
br i1 %125, label %126, label %127
126: ; preds = %117
br label %206
127: ; preds = %117
%128 = load i64, i64* %16
%129 = load i32, i32* %9
%130 = sext i32 %129 to i64
%131 = icmp eq i64 %128, %130
br i1 %131, label %132, label %150
132: ; preds = %127
%133 = load i32, i32* %6
%134 = load i64, i64* %16
%135 = load i32, i32* @SEEK_SET
%136 = call i32 @lseek(i32 %133, i64 %134, i32 %135)
%137 = icmp eq i32 %136, -1
br i1 %137, label %138, label %139
138: ; preds = %132
br label %206
139: ; preds = %132
%140 = load i32, i32* %6
%141 = load i8*, i8** %8
%142 = load i64, i64* %5
%143 = mul i64 %142, 1
%144 = call i64 @write(i32 %140, i8* %141, i64 %143)
%145 = load i64, i64* %5
%146 = mul i64 %145, 1
%147 = icmp ne i64 %144, %146
br i1 %147, label %148, label %149
148: ; preds = %139
br label %206
149: ; preds = %139
br label %150
150: ; preds = %149, %127
br label %162
151: ; preds = %112
%152 = load i32, i32* %6
%153 = load i8*, i8** %8
%154 = load i64, i64* %5
%155 = mul i64 %154, 1
%156 = call i64 @write(i32 %152, i8* %153, i64 %155)
%157 = load i64, i64* %5
%158 = mul i64 %157, 1
%159 = icmp ne i64 %156, %158
br i1 %159, label %160, label %161
160: ; preds = %151
br label %206
161: ; preds = %151
store i32 0, i32* %15
br label %162
162: ; preds = %161, %150
%163 = load i8*, i8** %7
%164 = icmp ne i8* %163, null
br i1 %164, label %165, label %168
165: ; preds = %162
%166 = load i8*, i8** %7
%167 = call i32 @free(i8* %166)
store i8* null, i8** %7
br label %168
168: ; preds = %165, %162
br label %169
169: ; preds = %168
%170 = load i64, i64* %5
%171 = load i64, i64* %16
%172 = sub nsw i64 %171, %170
store i64 %172, i64* %16
br label %83
173: ; preds = %91
%174 = load i8*, i8** %8
%175 = icmp ne i8* %174, null
br i1 %175, label %176, label %179
176: ; preds = %173
%177 = load i8*, i8** %8
%178 = call i32 @free(i8* %177)
store i8* null, i8** %8
br label %179
179: ; preds = %176, %173
br label %180
180: ; preds = %179
%181 = load i64, i64* %13
%182 = add nsw i64 %181, 1
store i64 %182, i64* %13
br label %53
183: ; preds = %53
br label %184
184: ; preds = %183
%185 = load i32, i32* %6
%186 = icmp ne i32 %185, 0
br i1 %186, label %187, label %193
187: ; preds = %184
%188 = load i32, i32* %6
%189 = call i64 @close(i32 %188)
%190 = icmp ne i64 %189, 0
br i1 %190, label %191, label %192
191: ; preds = %187
br label %206
192: ; preds = %187
store i32 0, i32* %6
br label %193
193: ; preds = %192, %184
%194 = load i8*, i8** %7
%195 = icmp ne i8* %194, null
br i1 %195, label %196, label %199
196: ; preds = %193
%197 = load i8*, i8** %7
%198 = call i32 @free(i8* %197)
br label %199
199: ; preds = %196, %193
%200 = load i8*, i8** %8
%201 = icmp ne i8* %200, null
br i1 %201, label %202, label %205
202: ; preds = %199
%203 = load i8*, i8** %8
%204 = call i32 @free(i8* %203)
br label %205
205: ; preds = %202, %199
store i32 0, i32* %3
br label %226
206: ; preds = %191, %160, %148, %138, %126, %111, %99, %78, %66, %36
%207 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%208 = load i32, i32* %6
%209 = icmp ne i32 %208, 0
br i1 %209, label %210, label %213
210: ; preds = %206
%211 = load i32, i32* %6
%212 = call i64 @close(i32 %211)
store i32 0, i32* %6
br label %213
213: ; preds = %210, %206
%214 = load i8*, i8** %7
%215 = icmp ne i8* %214, null
br i1 %215, label %216, label %219
216: ; preds = %213
%217 = load i8*, i8** %7
%218 = call i32 @free(i8* %217)
br label %219
219: ; preds = %216, %213
%220 = load i8*, i8** %8
%221 = icmp ne i8* %220, null
br i1 %221, label %222, label %225
222: ; preds = %219
%223 = load i8*, i8** %8
%224 = call i32 @free(i8* %223)
br label %225
225: ; preds = %222, %219
store i32 -1, i32* %3
br label %226
226: ; preds = %225, %205, %22
%227 = load i32, i32* %3
ret i32 %227
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [18 x i8] c"Can't open file.\0A\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.1 = [51 x i8] c"Wrong size of file... Not integer count of records\00"
@.str.2 = [30 x i8] c"Error during work with file.\0A\00"
define i32 @sys_sort(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %110
8: ; preds = %2
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %4, i64 0, i32 %9)
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @lseek(i32 %4, i64 0, i32 %11)
%13 = icmp eq i32 %10, -1
%14 = icmp eq i32 %12, -1
%15 = select i1 %13, i1 true, i1 %14
br i1 %15, label %91, label %16
16: ; preds = %8
%17 = sub nsw i32 %12, %10
%18 = sext i32 %17 to i64
%19 = sdiv i64 %18, %1
%20 = srem i64 %18, %1
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = icmp sgt i64 %19, 1
br i1 %25, label %26, label %84
26: ; preds = %24
%27 = sext i32 %10 to i64
br label %28
28: ; preds = %26, %81
%29 = phi i64 [ 1, %26 ], [ %82, %81 ]
%30 = mul nsw i64 %29, %1
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @lseek(i32 %4, i64 %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %91, label %34
34: ; preds = %28
%35 = tail call i8* @calloc(i64 %1, i32 1)
%36 = tail call i64 @read(i32 %4, i8* %35, i64 %1)
%37 = icmp eq i64 %36, %1
br i1 %37, label %38, label %91
38: ; preds = %34
%39 = sub nsw i64 %30, %1
%40 = icmp slt i64 %39, %27
br i1 %40, label %77, label %41
41: ; preds = %38, %73
%42 = phi i64 [ %74, %73 ], [ %39, %38 ]
%43 = load i32, i32* @SEEK_SET
%44 = tail call i32 @lseek(i32 %4, i64 %42, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %91, label %46
46: ; preds = %41
%47 = tail call i8* @calloc(i64 %1, i32 1)
%48 = tail call i64 @read(i32 %4, i8* %47, i64 %1)
%49 = icmp eq i64 %48, %1
br i1 %49, label %50, label %91
50: ; preds = %46
%51 = tail call i32 @compare_records(i8* %35, i8* %47)
%52 = icmp slt i32 %51, 1
br i1 %52, label %53, label %65
53: ; preds = %50
%54 = tail call i64 @write(i32 %4, i8* %47, i64 %1)
%55 = icmp eq i64 %54, %1
br i1 %55, label %56, label %91
56: ; preds = %53
%57 = icmp eq i64 %42, %27
br i1 %57, label %58, label %68
58: ; preds = %56
%59 = load i32, i32* @SEEK_SET
%60 = tail call i32 @lseek(i32 %4, i64 %27, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %91, label %62
62: ; preds = %58
%63 = tail call i64 @write(i32 %4, i8* %35, i64 %1)
%64 = icmp eq i64 %63, %1
br i1 %64, label %68, label %91
65: ; preds = %50
%66 = tail call i64 @write(i32 %4, i8* %35, i64 %1)
%67 = icmp eq i64 %66, %1
br i1 %67, label %68, label %91
68: ; preds = %65, %56, %62
%69 = phi i1 [ false, %65 ], [ true, %56 ], [ true, %62 ]
%70 = icmp eq i8* %47, null
br i1 %70, label %73, label %71
71: ; preds = %68
%72 = tail call i32 @free(i8* %47)
br label %73
73: ; preds = %68, %71
%74 = sub nsw i64 %42, %1
%75 = icmp sge i64 %74, %27
%76 = and i1 %69, %75
br i1 %76, label %41, label %77
77: ; preds = %73, %38
%78 = icmp eq i8* %35, null
br i1 %78, label %81, label %79
79: ; preds = %77
%80 = tail call i32 @free(i8* %35)
br label %81
81: ; preds = %77, %79
%82 = add nuw nsw i64 %29, 1
%83 = icmp eq i64 %82, %19
br i1 %83, label %84, label %28
84: ; preds = %81, %24
%85 = icmp eq i32 %4, 0
br i1 %85, label %110, label %86
86: ; preds = %84
%87 = tail call i64 @close(i32 %4)
%88 = icmp eq i64 %87, 0
br i1 %88, label %110, label %89
89: ; preds = %86
%90 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %96
91: ; preds = %28, %34, %65, %62, %58, %53, %46, %41, %8
%92 = phi i8* [ null, %8 ], [ %35, %41 ], [ %35, %46 ], [ %35, %53 ], [ %35, %58 ], [ %35, %62 ], [ %35, %65 ], [ %35, %34 ], [ null, %28 ]
%93 = phi i8* [ null, %8 ], [ %47, %65 ], [ %47, %62 ], [ %47, %58 ], [ %47, %53 ], [ %47, %46 ], [ null, %41 ], [ null, %34 ], [ null, %28 ]
%94 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%95 = icmp eq i32 %4, 0
br i1 %95, label %100, label %96
96: ; preds = %89, %91
%97 = phi i8* [ null, %89 ], [ %92, %91 ]
%98 = phi i8* [ null, %89 ], [ %93, %91 ]
%99 = tail call i64 @close(i32 %4)
br label %100
100: ; preds = %96, %91
%101 = phi i8* [ %97, %96 ], [ %92, %91 ]
%102 = phi i8* [ %98, %96 ], [ %93, %91 ]
%103 = icmp eq i8* %102, null
br i1 %103, label %106, label %104
104: ; preds = %100
%105 = tail call i32 @free(i8* %102)
br label %106
106: ; preds = %104, %100
%107 = icmp eq i8* %101, null
br i1 %107, label %110, label %108
108: ; preds = %106
%109 = tail call i32 @free(i8* %101)
br label %110
110: ; preds = %84, %86, %106, %108, %6
%111 = phi i32 [ -2, %6 ], [ -1, %108 ], [ -1, %106 ], [ 0, %86 ], [ 0, %84 ]
ret i32 %111
}