#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*/ memcpy(char *, void *, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ munmap(char *, int);
int File_UpdateRecordByIndex(const char *FileName, void *Record, int Record_Len,
                             int Record_Index) {
  int fd;
  char *fbp = NULL;
  int size = get_file_size(FileName);
  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;
  }
  memcpy((fbp + (Record_Len * Record_Index)), Record, Record_Len);
  if (munmap(fbp, size) < 0) {
    logcat("file munmap error.\n");
  }
  File_Close(fd);
  return Record_Len;
}


x86 assembly (clang, -O0):
<File_UpdateRecordByIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <get_file_size@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <File_Open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bb <File_UpdateRecordByIndex+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <File_UpdateRecordByIndex+0xfd>
mov    -0x34(%rbp),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    0x2e02(%rip),%rax
or     (%rax),%edx
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <File_UpdateRecordByIndex+0xb1>
lea    0xdf8(%rip),%rdi
call   1040 <logcat@plt>
mov    -0x24(%rbp),%edi
call   1090 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <File_UpdateRecordByIndex+0xfd>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
jge    125f <File_UpdateRecordByIndex+0xef>
lea    0xdb8(%rip),%rdi
call   1040 <logcat@plt>
mov    -0x24(%rbp),%edi
call   1090 <File_Close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<File_UpdateRecordByIndex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%rbx
call   1080 <get_file_size@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1060 <File_Open@plt>
test   %eax,%eax
js     1205 <File_UpdateRecordByIndex+0x95>
mov    %eax,%ebp
mov    0x2e54(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2df6(%rip),%rax
cmp    (%rax),%rbx
je     120d <File_UpdateRecordByIndex+0x9d>
imul   %r12d,%r13d
movslq %r13d,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <munmap@plt>
test   %rax,%rax
jns    121f <File_UpdateRecordByIndex+0xaf>
lea    0xe0f(%rip),%rdi
jmp    121a <File_UpdateRecordByIndex+0xaa>
mov    $0xffffffff,%r12d
jmp    1226 <File_UpdateRecordByIndex+0xb6>
mov    $0xffffffff,%r12d
lea    0xde6(%rip),%rdi
call   1040 <logcat@plt>
mov    %ebp,%edi
call   1090 <File_Close@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [18 x i8] c"file mmap error.\0A\00"
@.str.1 = [20 x i8] c"file munmap error.\0A\00"
define i32 @File_UpdateRecordByIndex(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* null, i8** %11
%13 = load i8*, i8** %6
%14 = call i32 @get_file_size(i8* %13)
store i32 %14, i32* %12
%15 = load i8*, i8** %6
%16 = call i32 @File_Open(i8* %15)
store i32 %16, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %54
19: ; preds = %4
%20 = load i32, i32* %12
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* %10
%26 = call i8* @mmap(i32* null, i32 %20, i32 %23, i32 %24, i32 %25, i32 0)
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %34
30: ; preds = %19
%31 = call i32 @logcat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* %10
%33 = call i32 @File_Close(i32 %32)
store i32 -1, i32* %5
br label %54
34: ; preds = %19
%35 = load i8*, i8** %11
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = mul nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %35, i64 %39
%41 = load i8*, i8** %7
%42 = load i32, i32* %8
%43 = call i32 @memcpy(i8* %40, i8* %41, i32 %42)
%44 = load i8*, i8** %11
%45 = load i32, i32* %12
%46 = call i64 @munmap(i8* %44, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %34
%49 = call i32 @logcat(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %34
%51 = load i32, i32* %10
%52 = call i32 @File_Close(i32 %51)
%53 = load i32, i32* %8
store i32 %53, i32* %5
br label %54
54: ; preds = %50, %30, %18
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [18 x i8] c"file mmap error.\0A\00"
@.str.1 = [20 x i8] c"file munmap error.\0A\00"
define i32 @File_UpdateRecordByIndex(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @get_file_size(i8* %0)
%6 = tail call i32 @File_Open(i8* %0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %4
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i8* @mmap(i32* null, i32 %5, i32 %11, i32 %12, i32 %6, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %23, label %16
16: ; preds = %8
%17 = mul nsw i32 %3, %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %13, i64 %18
%20 = tail call i32 @memcpy(i8* %19, i8* %1, i32 %2)
%21 = tail call i64 @munmap(i8* %13, i32 %5)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %16, %8
%24 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %16 ]
%25 = phi i32 [ -1, %8 ], [ %2, %16 ]
%26 = tail call i32 @logcat(i8* %24)
br label %27
27: ; preds = %23, %16
%28 = phi i32 [ %2, %16 ], [ %25, %23 ]
%29 = tail call i32 @File_Close(i32 %6)
br label %30
30: ; preds = %27, %4
%31 = phi i32 [ -1, %4 ], [ %28, %27 ]
ret i32 %31
}