#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 */
char *MAP_FAILED;
int MAP_LOCKED;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcpy(char *, void *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char const *, int /*<<< orphan*/);
int update_file(const char *path, void *entry, int esize) {
  int rv = -1;
  int file;
  char *map;
  if ((file = open(path, O_RDWR)) >= 0) {
    map = mmap(NULL, esize * 60, PROT_READ | PROT_WRITE,
               MAP_SHARED | MAP_LOCKED, file, 0);
    if ((map != NULL) && (map != MAP_FAILED)) {
      memmove(map, map + esize, esize * (60 - 1));
      memcpy(map + esize * (60 - 1), entry, esize);
      munmap(map, esize * 60);
      rv = 0;
    }
    close(file);
  }
  return rv;
}


x86 assembly (clang, -O0):
<update_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1240 <update_file+0xe0>
imul   $0x3c,-0x14(%rbp),%esi
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
mov    0x2e14(%rip),%rax
or     (%rax),%edx
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0a(%rip),%rax
or     (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1238 <update_file+0xd8>
mov    -0x28(%rbp),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
je     1238 <update_file+0xd8>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
imul   $0x3b,-0x14(%rbp),%edx
call   1070 <memmove@plt>
mov    -0x28(%rbp),%rdi
imul   $0x3b,-0x14(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
imul   $0x3c,-0x14(%rbp),%esi
call   1060 <munmap@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1217 <update_file+0xb7>
mov    %eax,%r13d
imul   $0x3c,%r12d,%r15d
mov    0x2e63(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e41(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r13d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     120d <update_file+0xad>
mov    %rax,%rbp
mov    0x2de2(%rip),%rax
cmp    (%rax),%rbp
je     120d <update_file+0xad>
movslq %r12d,%rsi
add    %rbp,%rsi
imul   $0x3b,%r12d,%ebx
mov    %rbp,%rdi
mov    %ebx,%edx
call   1070 <memmove@plt>
movslq %ebx,%rdi
add    %rbp,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
call   1060 <munmap@plt>
xor    %ebx,%ebx
mov    %r13d,%edi
call   1040 <close@plt>
jmp    121c <update_file+0xbc>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i8* null
define i32 @update_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %55
14: ; preds = %3
%15 = load i32, i32* %6
%16 = mul nsw i32 %15, 60
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* @MAP_LOCKED
%22 = or i32 %20, %21
%23 = load i32, i32* %8
%24 = call i8* @mmap(i32* null, i32 %16, i32 %19, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %52
27: ; preds = %14
%28 = load i8*, i8** %9
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp ne i8* %28, %29
br i1 %30, label %31, label %52
31: ; preds = %27
%32 = load i8*, i8** %9
%33 = load i8*, i8** %9
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %6
%38 = mul nsw i32 %37, 59
%39 = call i32 @memmove(i8* %32, i8* %36, i32 %38)
%40 = load i8*, i8** %9
%41 = load i32, i32* %6
%42 = mul nsw i32 %41, 59
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
%45 = load i8*, i8** %5
%46 = load i32, i32* %6
%47 = call i32 @memcpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %9
%49 = load i32, i32* %6
%50 = mul nsw i32 %49, 60
%51 = call i32 @munmap(i8* %48, i32 %50)
store i32 0, i32* %7
br label %52
52: ; preds = %31, %27, %14
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %52, %3
%56 = load i32, i32* %7
ret i32 %56
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i8* null
define i32 @update_file(i8* nocapture %0, i8* %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %32
7: ; preds = %3
%8 = mul nsw i32 %2, 60
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = load i32, i32* @MAP_LOCKED
%14 = or i32 %13, %12
%15 = tail call i8* @mmap(i32* null, i32 %8, i32 %11, i32 %14, i32 %5, i32 0)
%16 = icmp eq i8* %15, null
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %15, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %29, label %20
20: ; preds = %7
%21 = sext i32 %2 to i64
%22 = getelementptr inbounds i8, i8* %15, i64 %21
%23 = mul nsw i32 %2, 59
%24 = tail call i32 @memmove(i8* %15, i8* %22, i32 %23)
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %15, i64 %25
%27 = tail call i32 @memcpy(i8* %26, i8* %1, i32 %2)
%28 = tail call i32 @munmap(i8* %15, i32 %8)
br label %29
29: ; preds = %20, %7
%30 = phi i32 [ 0, %20 ], [ -1, %7 ]
%31 = tail call i32 @close(i32 %5)
br label %32
32: ; preds = %29, %3
%33 = phi i32 [ %30, %29 ], [ -1, %3 ]
ret i32 %33
}