#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 /*<<< orphan*/ cur_time_str;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ flock(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ get_localtime_stamp(char *, int);
scalar_t__ malloc(long);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
FILE *open_msg_file(const char *file_name, long max_file_len) {
  FILE *file_handle;
  file_handle = fopen(file_name, "a+t");
  if (file_handle) {
    long log_size;
    size_t log_size_loaded;
    char cur_time_str[19 + 1];
    flock(fileno(file_handle), LOCK_UN);
    setbuf(file_handle, NULL);
    fseek(file_handle, 0, SEEK_END);
    log_size = ftell(file_handle);
    if (log_size > max_file_len) {
      char *log_file_buf;
      log_file_buf = (char *)malloc(max_file_len * sizeof(char));
      if (log_file_buf) {
        fseek(file_handle, -max_file_len, SEEK_END);
        log_size_loaded =
            fread(log_file_buf, sizeof(char), max_file_len, file_handle);
        fclose(file_handle);
        file_handle = fopen(file_name, "wt");
        if (file_handle) {
          get_localtime_stamp(cur_time_str, sizeof(cur_time_str));
          fprintf(file_handle, "\n[%s] <Old messages deleted>\n\n",
                  cur_time_str);
          fwrite(log_file_buf, sizeof(char), log_size_loaded, file_handle);
        }
        free(log_file_buf);
      }
    }
    if (file_handle) {
      get_localtime_stamp(cur_time_str, sizeof(cur_time_str));
      fprintf(
          file_handle,
          "\n[%s] --------------------- Log initiated ---------------------\n",
          cur_time_str);
      fprintf(file_handle, "[%s] alarm4pi daemon running\n", cur_time_str);
    }
  }
  return (file_handle);
}


x86 assembly (clang, -O0):
<open_msg_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     135a <open_msg_file+0x18a>
mov    -0x18(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   10e0 <flock@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setbuf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    1314 <open_msg_file+0x144>
mov    -0x10(%rbp),%rdi
shl    $0x0,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     130f <open_msg_file+0x13f>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
sub    -0x10(%rbp),%rsi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
call   10c0 <fseek@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x8(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1306 <open_msg_file+0x136>
lea    -0x40(%rbp),%rdi
mov    $0x14,%esi
call   1070 <get_localtime_stamp@plt>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xd1c(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    1314 <open_msg_file+0x144>
cmpq   $0x0,-0x18(%rbp)
je     1355 <open_msg_file+0x185>
lea    -0x40(%rbp),%rdi
mov    $0x14,%esi
call   1070 <get_localtime_stamp@plt>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xcea(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xd17(%rip),%rsi
call   1080 <fprintf@plt>
jmp    135a <open_msg_file+0x18a>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_msg_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe16(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1319 <open_msg_file+0x149>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <fileno@plt>
mov    0x2dce(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   10e0 <flock@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <setbuf@plt>
mov    0x2dc4(%rip),%r13
mov    0x0(%r13),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
cmp    %r15,%rax
jle    12db <open_msg_file+0x10b>
mov    %r15,%rdi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12db <open_msg_file+0x10b>
mov    %rax,%r12
mov    %r15,%rsi
neg    %rsi
mov    0x0(%r13),%edx
mov    %rbx,%rdi
call   10c0 <fseek@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd76(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1311 <open_msg_file+0x141>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x14,%esi
call   1070 <get_localtime_stamp@plt>
lea    0xd52(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   10f0 <fwrite@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x14,%esi
call   1070 <get_localtime_stamp@plt>
lea    0xd34(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fprintf@plt>
lea    0xd63(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fprintf@plt>
jmp    131b <open_msg_file+0x14b>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"a+t\00"
@LOCK_UN = global i32 0
@SEEK_END = global i32 0
@.str.1 = [3 x i8] c"wt\00"
@.str.2 = [31 x i8] c"\0A[%s] <Old messages deleted>\0A\0A\00"
@.str.3 = [65 x i8] c"\0A[%s] --------------------- Log initiated ---------------------\0A\00"
@.str.4 = [30 x i8] c"[%s] alarm4pi daemon running\0A\00"
define i32* @open_msg_file(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca [20 x i8]
%9 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %79
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = call i32 @fileno(i32* %15)
%17 = load i32, i32* @LOCK_UN
%18 = call i32 @flock(i32 %16, i32 %17)
%19 = load i32*, i32** %5
%20 = call i32 @setbuf(i32* %19, i32* null)
%21 = load i32*, i32** %5
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i64 0, i32 %22)
%24 = load i32*, i32** %5
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = load i64, i64* %4
%28 = icmp sgt i64 %26, %27
br i1 %28, label %29, label %66
29: ; preds = %14
%30 = load i64, i64* %4
%31 = mul i64 %30, 1
%32 = call i64 @malloc(i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %9
%34 = load i8*, i8** %9
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %65
36: ; preds = %29
%37 = load i32*, i32** %5
%38 = load i64, i64* %4
%39 = sub nsw i64 0, %38
%40 = load i32, i32* @SEEK_END
%41 = call i32 @fseek(i32* %37, i64 %39, i32 %40)
%42 = load i8*, i8** %9
%43 = load i64, i64* %4
%44 = load i32*, i32** %5
%45 = call i64 @fread(i8* %42, i32 1, i64 %43, i32* %44)
store i64 %45, i64* %7
%46 = load i32*, i32** %5
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %3
%49 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %49, i32** %5
%50 = load i32*, i32** %5
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %62
52: ; preds = %36
%53 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%54 = call i32 @get_localtime_stamp(i8* %53, i32 20)
%55 = load i32*, i32** %5
%56 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%57 = call i32 @fprintf(i32* %55, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %56)
%58 = load i8*, i8** %9
%59 = load i64, i64* %7
%60 = load i32*, i32** %5
%61 = call i32 @fwrite(i8* %58, i32 1, i64 %59, i32* %60)
br label %62
62: ; preds = %52, %36
%63 = load i8*, i8** %9
%64 = call i32 @free(i8* %63)
br label %65
65: ; preds = %62, %29
br label %66
66: ; preds = %65, %14
%67 = load i32*, i32** %5
%68 = icmp ne i32* %67, null
br i1 %68, label %69, label %78
69: ; preds = %66
%70 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%71 = call i32 @get_localtime_stamp(i8* %70, i32 20)
%72 = load i32*, i32** %5
%73 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%74 = call i32 @fprintf(i32* %72, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %73)
%75 = load i32*, i32** %5
%76 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%77 = call i32 @fprintf(i32* %75, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %76)
br label %78
78: ; preds = %69, %66
br label %79
79: ; preds = %78, %2
%80 = load i32*, i32** %5
ret i32* %80
}

LLVM IR (-O2):
@.str = [4 x i8] c"a+t\00"
@LOCK_UN = global i32 0
@SEEK_END = global i32 0
@.str.1 = [3 x i8] c"wt\00"
@.str.2 = [31 x i8] c"\0A[%s] <Old messages deleted>\0A\0A\00"
@.str.3 = [65 x i8] c"\0A[%s] --------------------- Log initiated ---------------------\0A\00"
@.str.4 = [30 x i8] c"[%s] alarm4pi daemon running\0A\00"
define i32* @open_msg_file(i8* nocapture %0, i64 %1) {
%3 = alloca [20 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %42, label %6
6: ; preds = %2
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = tail call i32 @fileno(i32* %4)
%9 = load i32, i32* @LOCK_UN
%10 = tail call i32 @flock(i32 %8, i32 %9)
%11 = tail call i32 @setbuf(i32* %4, i32* null)
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %4, i64 0, i32 %12)
%14 = tail call i64 @ftell(i32* %4)
%15 = icmp sgt i64 %14, %1
br i1 %15, label %16, label %35
16: ; preds = %6
%17 = tail call i64 @malloc(i64 %1)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %17, 0
br i1 %19, label %35, label %20
20: ; preds = %16
%21 = sub nsw i64 0, %1
%22 = load i32, i32* @SEEK_END
%23 = tail call i32 @fseek(i32* %4, i64 %21, i32 %22)
%24 = tail call i64 @fread(i8* %18, i32 1, i64 %1, i32* %4)
%25 = tail call i32 @fclose(i32* %4)
%26 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = tail call i32 @free(i8* %18)
br label %40
30: ; preds = %20
%31 = call i32 @get_localtime_stamp(i8* %7, i32 20)
%32 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%33 = call i32 @fwrite(i8* %18, i32 1, i64 %24, i32* %26)
%34 = call i32 @free(i8* %18)
br label %35
35: ; preds = %16, %6, %30
%36 = phi i32* [ %26, %30 ], [ %4, %6 ], [ %4, %16 ]
%37 = call i32 @get_localtime_stamp(i8* %7, i32 20)
%38 = call i32 @fprintf(i32* %36, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%39 = call i32 @fprintf(i32* %36, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %7)
br label %40
40: ; preds = %28, %35
%41 = phi i32* [ %36, %35 ], [ null, %28 ]
br label %42
42: ; preds = %40, %2
%43 = phi i32* [ %41, %40 ], [ null, %2 ]
ret i32* %43
}