#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 */

/* Variables and functions */
int /*<<< orphan*/ File_Close(int);
int File_Open(char const *);
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int get_file_size(char const *);
int /*<<< orphan*/ logcat(char *);
int /*<<< orphan*/ memmove(char *, char *, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ munmap(char *, int);
int /*<<< orphan*/ truncate(char const *, int);
int File_DeleteRecordByIndex(const char *FileName, int Record_Len,
                             int Record_Index) {
  int fd;
  char *fbp = NULL;
  int size = get_file_size(FileName);
  char *to, *from;
  if ((Record_Index + 1) > (size / Record_Len)) {
    logcat("File: Invalid Record Index.\n");
    return -1;
  }
  if ((fd = File_Open(FileName)) < 0)
    return -1;
  fbp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (fbp == MAP_FAILED) {
    logcat("file mmap error.\n");
    File_Close(fd);
    return -1;
  }
  to = fbp + (Record_Len * Record_Index);
  from = fbp + (Record_Len * (Record_Index + 1));
  memmove(to, from, (size - (Record_Len * (Record_Index + 1))));
  if (munmap(fbp, size) < 0) {
    logcat("file munmap error.\n");
  }
  File_Close(fd);
  truncate(FileName, (size - Record_Len));
  return 0;
}


x86 assembly (clang, -O0):
<File_DeleteRecordByIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <get_file_size@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jle    11db <File_DeleteRecordByIndex+0x5b>
lea    0xe36(%rip),%rdi
call   1040 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <File_DeleteRecordByIndex+0x16f>
mov    -0x10(%rbp),%rdi
call   1060 <File_Open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11fc <File_DeleteRecordByIndex+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <File_DeleteRecordByIndex+0x16f>
mov    -0x2c(%rbp),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
mov    0x2dd0(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <File_DeleteRecordByIndex+0xe2>
lea    0xdd4(%rip),%rdi
call   1040 <logcat@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <File_DeleteRecordByIndex+0x16f>
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%edx
add    $0x1,%edx
imul   %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
sub    %eax,%edx
call   1080 <memmove@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
jge    12d1 <File_DeleteRecordByIndex+0x151>
lea    0xd63(%rip),%rdi
call   1040 <logcat@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <File_Close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
sub    -0x14(%rbp),%esi
call   1050 <truncate@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<File_DeleteRecordByIndex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r12
call   1090 <get_file_size@plt>
mov    %eax,%r13d
cltd
idiv   %ebp
cmp    %ebx,%eax
jle    125e <File_DeleteRecordByIndex+0xde>
mov    %r12,%rdi
call   1060 <File_Open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1270 <File_DeleteRecordByIndex+0xf0>
mov    %ebp,0x4(%rsp)
mov    0x2e32(%rip),%rsi
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
or     (%rsi),%edx
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %r13d,%esi
mov    %eax,%ebp
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    0x2dd2(%rip),%rax
cmp    (%rax),%r15
je     1282 <File_DeleteRecordByIndex+0x102>
mov    %ebp,%r14d
lea    0x1(%rbx),%eax
mov    0x4(%rsp),%ebp
imul   %ebp,%ebx
movslq %ebx,%rdi
add    %r15,%rdi
imul   %ebp,%eax
cltq
mov    %r15,%rsi
add    %rax,%rsi
mov    %r13d,%edx
sub    %eax,%edx
call   1080 <memmove@plt>
mov    %r15,%rdi
mov    %r13d,%esi
call   1070 <munmap@plt>
test   %rax,%rax
jns    1243 <File_DeleteRecordByIndex+0xc3>
lea    0xdf1(%rip),%rdi
call   1040 <logcat@plt>
mov    %r14d,%edi
call   10a0 <File_Close@plt>
sub    %ebp,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <truncate@plt>
xor    %r14d,%r14d
jmp    1270 <File_DeleteRecordByIndex+0xf0>
lea    0xd9b(%rip),%rdi
call   1040 <logcat@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd94(%rip),%rdi
call   1040 <logcat@plt>
mov    %ebp,%edi
call   10a0 <File_Close@plt>
jmp    1270 <File_DeleteRecordByIndex+0xf0>

LLVM IR (-O0):
@.str = [29 x i8] c"File: Invalid Record Index.\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [18 x i8] c"file mmap error.\0A\00"
@.str.2 = [20 x i8] c"file munmap error.\0A\00"
define i32 @File_DeleteRecordByIndex(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* null, i8** %9
%13 = load i8*, i8** %5
%14 = call i32 @get_file_size(i8* %13)
store i32 %14, i32* %10
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
%17 = load i32, i32* %10
%18 = load i32, i32* %6
%19 = sdiv i32 %17, %18
%20 = icmp sgt i32 %16, %19
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @logcat(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %80
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = call i32 @File_Open(i8* %24)
store i32 %25, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %4
br label %80
28: ; preds = %23
%29 = load i32, i32* %10
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %8
%35 = call i8* @mmap(i32* null, i32 %29, i32 %32, i32 %33, i32 %34, i32 0)
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %43
39: ; preds = %28
%40 = call i32 @logcat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* %8
%42 = call i32 @File_Close(i32 %41)
store i32 -1, i32* %4
br label %80
43: ; preds = %28
%44 = load i8*, i8** %9
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = mul nsw i32 %45, %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
store i8* %49, i8** %11
%50 = load i8*, i8** %9
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
%54 = mul nsw i32 %51, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %50, i64 %55
store i8* %56, i8** %12
%57 = load i8*, i8** %11
%58 = load i8*, i8** %12
%59 = load i32, i32* %10
%60 = load i32, i32* %6
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
%63 = mul nsw i32 %60, %62
%64 = sub nsw i32 %59, %63
%65 = call i32 @memmove(i8* %57, i8* %58, i32 %64)
%66 = load i8*, i8** %9
%67 = load i32, i32* %10
%68 = call i64 @munmap(i8* %66, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %43
%71 = call i32 @logcat(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %70, %43
%73 = load i32, i32* %8
%74 = call i32 @File_Close(i32 %73)
%75 = load i8*, i8** %5
%76 = load i32, i32* %10
%77 = load i32, i32* %6
%78 = sub nsw i32 %76, %77
%79 = call i32 @truncate(i8* %75, i32 %78)
store i32 0, i32* %4
br label %80
80: ; preds = %72, %39, %27, %21
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
@.str = [29 x i8] c"File: Invalid Record Index.\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [18 x i8] c"file mmap error.\0A\00"
@.str.2 = [20 x i8] c"file munmap error.\0A\00"
define i32 @File_DeleteRecordByIndex(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @get_file_size(i8* %0)
%5 = add nsw i32 %2, 1
%6 = sdiv i32 %4, %1
%7 = icmp sgt i32 %6, %2
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @logcat(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %41
10: ; preds = %3
%11 = tail call i32 @File_Open(i8* %0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %41, label %13
13: ; preds = %10
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i8* @mmap(i32* null, i32 %4, i32 %16, i32 %17, i32 %11, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @logcat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @File_Close(i32 %11)
br label %41
24: ; preds = %13
%25 = mul nsw i32 %2, %1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %18, i64 %26
%28 = mul nsw i32 %5, %1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %18, i64 %29
%31 = sub nsw i32 %4, %28
%32 = tail call i32 @memmove(i8* %27, i8* %30, i32 %31)
%33 = tail call i64 @munmap(i8* %18, i32 %4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %24
%36 = tail call i32 @logcat(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %24
%38 = tail call i32 @File_Close(i32 %11)
%39 = sub nsw i32 %4, %1
%40 = tail call i32 @truncate(i8* %0, i32 %39)
br label %41
41: ; preds = %10, %37, %21, %8
%42 = phi i32 [ -1, %8 ], [ -1, %21 ], [ 0, %37 ], [ -1, %10 ]
ret i32 %42
}