#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*/);
scalar_t__ timeof(char *);
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)) {
      if (timeof(entry) > timeof(map + esize * (60 - 1))) {
        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    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     127f <update_file+0x10f>
imul   $0x3c,-0x14(%rbp),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfa(%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     1277 <update_file+0x107>
mov    -0x28(%rbp),%rax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
je     1277 <update_file+0x107>
mov    -0x10(%rbp),%rdi
call   1060 <timeof@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
imul   $0x3b,-0x14(%rbp),%eax
cltq
add    %rax,%rdi
call   1060 <timeof@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jle    1263 <update_file+0xf3>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
imul   $0x3b,-0x14(%rbp),%edx
call   1080 <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   1070 <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
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %rsi,%r14
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1256 <update_file+0xe6>
mov    %eax,%r13d
imul   $0x3c,%r12d,%r15d
mov    0x2e50(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
mov    0x2e07(%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     124c <update_file+0xdc>
mov    %rax,%rbp
mov    0x2dcf(%rip),%rax
cmp    (%rax),%rbp
je     124c <update_file+0xdc>
mov    %r15d,0xc(%rsp)
mov    %r14,%rdi
call   1060 <timeof@plt>
mov    %rax,%r15
mov    %r14,0x10(%rsp)
imul   $0x3b,%r12d,%r14d
movslq %r14d,%rbx
add    %rbp,%rbx
mov    %rbx,%rdi
call   1060 <timeof@plt>
cmp    %rax,%r15
jle    123e <update_file+0xce>
movslq %r12d,%rax
mov    %rbp,%rsi
add    %rax,%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   1080 <memmove@plt>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    0xc(%rsp),%esi
call   1070 <munmap@plt>
xor    %ebx,%ebx
mov    %r13d,%edi
call   1040 <close@plt>
jmp    125b <update_file+0xeb>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%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 %66
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 %63
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 %63
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = call i64 @timeof(i8* %32)
%34 = load i8*, i8** %9
%35 = load i32, i32* %6
%36 = mul nsw i32 %35, 59
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = call i64 @timeof(i8* %38)
%40 = icmp sgt i64 %33, %39
br i1 %40, label %41, label %58
41: ; preds = %31
%42 = load i8*, i8** %9
%43 = load i8*, i8** %9
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i32, i32* %6
%48 = mul nsw i32 %47, 59
%49 = call i32 @memmove(i8* %42, i8* %46, i32 %48)
%50 = load i8*, i8** %9
%51 = load i32, i32* %6
%52 = mul nsw i32 %51, 59
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
%55 = load i8*, i8** %5
%56 = load i32, i32* %6
%57 = call i32 @memcpy(i8* %54, i8* %55, i32 %56)
br label %58
58: ; preds = %41, %31
%59 = load i8*, i8** %9
%60 = load i32, i32* %6
%61 = mul nsw i32 %60, 60
%62 = call i32 @munmap(i8* %59, i32 %61)
store i32 0, i32* %7
br label %63
63: ; preds = %58, %27, %14
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63, %3
%67 = load i32, i32* %7
ret i32 %67
}

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 %37
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 %34, label %20
20: ; preds = %7
%21 = tail call i64 @timeof(i8* %1)
%22 = mul nsw i32 %2, 59
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %15, i64 %23
%25 = tail call i64 @timeof(i8* %24)
%26 = icmp sgt i64 %21, %25
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = sext i32 %2 to i64
%29 = getelementptr inbounds i8, i8* %15, i64 %28
%30 = tail call i32 @memmove(i8* %15, i8* %29, i32 %22)
%31 = tail call i32 @memcpy(i8* %24, i8* %1, i32 %2)
br label %32
32: ; preds = %27, %20
%33 = tail call i32 @munmap(i8* %15, i32 %8)
br label %34
34: ; preds = %32, %7
%35 = phi i32 [ 0, %32 ], [ -1, %7 ]
%36 = tail call i32 @close(i32 %5)
br label %37
37: ; preds = %34, %3
%38 = phi i32 [ %35, %34 ], [ -1, %3 ]
ret i32 %38
}