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

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                   unsigned int, unsigned int);
int fsync(int /*<<< orphan*/);
int /*<<< orphan*/ log_msg(char *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void write_loop_counts(unsigned boots, unsigned wd_resets, time_t when) {
  FILE *f;
  if ((f = fopen("/var/lib/dsme/boot_count", "w")) == 0) {
    log_msg("Could not open "
            "/var/lib/dsme/boot_count"
            ": %s\n",
            strerror(errno));
  } else {
    if (fprintf(f, "%lu %u %u", when, boots, wd_resets) < 0) {
      log_msg("Error writing "
              "/var/lib/dsme/boot_count"
              "\n");
    } else if (ferror(f) || fflush(f) == EOF) {
      log_msg("Error flushing "
              "/var/lib/dsme/boot_count"
              ": %s\n",
              strerror(errno));
    } else if (fsync(fileno(f)) == -1) {
      log_msg("Error syncing "
              "/var/lib/dsme/boot_count"
              ": %s\n",
              strerror(errno));
    }
    if (fclose(f) == EOF) {
      log_msg("Error closing "
              "/var/lib/dsme/boot_count"
              ": %s\n",
              strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<write_loop_counts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    0xe58(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e5 <write_loop_counts+0x55>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
jmp    12e8 <write_loop_counts+0x158>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%r8d
lea    0xe4e(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    121c <write_loop_counts+0x8c>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
jmp    12ac <write_loop_counts+0x11c>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    1248 <write_loop_counts+0xb8>
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
jne    126b <write_loop_counts+0xdb>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
jmp    12a7 <write_loop_counts+0x117>
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1090 <fsync@plt>
cmp    $0xffffffff,%eax
jne    12a2 <write_loop_counts+0x112>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
jmp    12a7 <write_loop_counts+0x117>
jmp    12ac <write_loop_counts+0x11c>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    12e3 <write_loop_counts+0x153>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
jmp    12e8 <write_loop_counts+0x158>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_loop_counts>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%ebp
lea    0xe5b(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1219 <write_loop_counts+0x89>
mov    %rax,%r14
lea    0xe88(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %r15d,%r8d
call   1050 <fprintf@plt>
test   %rax,%rax
js     1222 <write_loop_counts+0x92>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11f9 <write_loop_counts+0x69>
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
jne    127a <write_loop_counts+0xea>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe6c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <log_msg@plt>
jmp    1230 <write_loop_counts+0xa0>
lea    0xdfb(%rip),%rbx
jmp    124b <write_loop_counts+0xbb>
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1080 <log_msg@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
jne    126f <write_loop_counts+0xdf>
lea    0xe88(%rip),%rbx
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <log_msg@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1090 <fsync@plt>
cmp    $0xffffffff,%eax
jne    1230 <write_loop_counts+0xa0>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe04(%rip),%rdi
jmp    120e <write_loop_counts+0x7e>

LLVM IR (-O0):
@.str = [25 x i8] c"/var/lib/dsme/boot_count\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [45 x i8] c"Could not open /var/lib/dsme/boot_count: %s\0A\00"
@errno = global i32 0
@.str.3 = [10 x i8] c"%lu %u %u\00"
@.str.4 = [40 x i8] c"Error writing /var/lib/dsme/boot_count\0A\00"
@EOF = global i64 0
@.str.5 = [45 x i8] c"Error flushing /var/lib/dsme/boot_count: %s\0A\00"
@.str.6 = [44 x i8] c"Error syncing /var/lib/dsme/boot_count: %s\0A\00"
@.str.7 = [44 x i8] c"Error closing /var/lib/dsme/boot_count: %s\0A\00"
define void @write_loop_counts(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %7
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %57
14: ; preds = %3
%15 = load i32*, i32** %7
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = load i32, i32* %5
%19 = call i64 @fprintf(i32* %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %47
23: ; preds = %14
%24 = load i32*, i32** %7
%25 = call i64 @ferror(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32*, i32** %7
%29 = call i64 @fflush(i32* %28)
%30 = load i64, i64* @EOF
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %27, %23
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %34)
br label %46
36: ; preds = %27
%37 = load i32*, i32** %7
%38 = call i32 @fileno(i32* %37)
%39 = call i32 @fsync(i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %41, %36
br label %46
46: ; preds = %45, %32
br label %47
47: ; preds = %46, %21
%48 = load i32*, i32** %7
%49 = call i64 @fclose(i32* %48)
%50 = load i64, i64* @EOF
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %56
52: ; preds = %47
%53 = load i32, i32* @errno
%54 = call i32 @strerror(i32 %53)
%55 = call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %52, %47
br label %57
57: ; preds = %56, %10
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"/var/lib/dsme/boot_count\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [45 x i8] c"Could not open /var/lib/dsme/boot_count: %s\0A\00"
@errno = global i32 0
@.str.3 = [10 x i8] c"%lu %u %u\00"
@.str.4 = [40 x i8] c"Error writing /var/lib/dsme/boot_count\0A\00"
@EOF = global i64 0
@.str.5 = [45 x i8] c"Error flushing /var/lib/dsme/boot_count: %s\0A\00"
@.str.6 = [44 x i8] c"Error syncing /var/lib/dsme/boot_count: %s\0A\00"
@.str.7 = [44 x i8] c"Error closing /var/lib/dsme/boot_count: %s\0A\00"
define void @write_loop_counts(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %34, label %6
6: ; preds = %3
%7 = tail call i64 @fprintf(i32* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %2, i32 %0, i32 %1)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %30
11: ; preds = %6
%12 = tail call i64 @ferror(i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = tail call i64 @fflush(i32* %4)
%16 = load i64, i64* @EOF
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14, %11
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %20)
br label %30
22: ; preds = %14
%23 = tail call i32 @fileno(i32* %4)
%24 = tail call i32 @fsync(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @errno
%28 = tail call i32 @strerror(i32 %27)
%29 = tail call i32 (i8*, ...) @log_msg(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %18, %26, %22, %9
%31 = tail call i64 @fclose(i32* %4)
%32 = load i64, i64* @EOF
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %30, %3
%35 = phi i8* [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), %3 ], [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), %30 ]
%36 = load i32, i32* @errno
%37 = tail call i32 @strerror(i32 %36)
%38 = tail call i32 (i8*, ...) @log_msg(i8* %35, i32 %37)
br label %39
39: ; preds = %34, %30
ret void
}