#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*/ FILE;

/* Variables and functions */
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fsync(int /*<<< orphan*/);
scalar_t__ fwrite(char const *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_msg(char *, int /*<<< orphan*/);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strlen(char const *);
int save_state(const char *state) {
  FILE *saved_state_file;
  saved_state_file = fopen("/var/lib/dsme/saved_state"
                           ".new",
                           "w");
  if (!saved_state_file) {
    log_msg("Could not open "
            "/var/lib/dsme/saved_state"
            ".new - %s\n",
            strerror(errno));
    return -1;
  }
  errno = 0;
  if (fwrite(state, 1, strlen(state), saved_state_file) <= 0) {
    log_msg("Could not write state"
            " - %s\n",
            strerror(errno));
    fclose(saved_state_file);
    return -1;
  }
  fflush(saved_state_file);
  if (fsync(fileno(saved_state_file)) < 0) {
    log_msg("Could not sync data"
            " - %s\n",
            strerror(errno));
    fclose(saved_state_file);
    return -1;
  }
  if (fclose(saved_state_file) < 0) {
    log_msg("Could not write state"
            " - %s\n",
            strerror(errno));
    return -1;
  }
  if (rename("/var/lib/dsme/saved_state"
             ".new",
             "/var/lib/dsme/saved_state")) {
    log_msg("Could not rename "
            "/var/lib/dsme/saved_state"
            ".new to "
            "/var/lib/dsme/saved_state"
            " - %s\n",
            strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<save_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f7 <save_state+0x57>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe38(%rip),%rdi
call   1070 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <save_state+0x1a4>
mov    0x2de2(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
cmp    $0x0,%rax
jg     1266 <save_state+0xc6>
mov    0x2da5(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe05(%rip),%rdi
call   1070 <log_msg@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <save_state+0x1a4>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
call   1080 <fsync@plt>
cmp    $0x0,%rax
jge    12bb <save_state+0x11b>
mov    0x2d50(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xdcc(%rip),%rdi
call   1070 <log_msg@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <save_state+0x1a4>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
jge    12f7 <save_state+0x157>
mov    0x2d0b(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xd6b(%rip),%rdi
call   1070 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <save_state+0x1a4>
lea    0xd02(%rip),%rdi
lea    0xd82(%rip),%rsi
call   10a0 <rename@plt>
cmp    $0x0,%rax
je     133d <save_state+0x19d>
mov    0x2cc5(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xd75(%rip),%rdi
call   1070 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <save_state+0x1a4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_state>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe51(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     124a <save_state+0xaa>
mov    %rax,%r14
mov    0x2e12(%rip),%r15
movq   $0x0,(%r15)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
test   %rax,%rax
jle    1269 <save_state+0xc9>
mov    %r14,%rdi
call   1060 <fflush@plt>
mov    %r14,%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
call   1080 <fsync@plt>
test   %rax,%rax
js     127a <save_state+0xda>
mov    %r14,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
js     12a3 <save_state+0x103>
lea    0xdd8(%rip),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <rename@plt>
test   %rax,%rax
je     12b4 <save_state+0x114>
mov    (%r15),%rdi
call   10c0 <strerror@plt>
lea    0xe59(%rip),%rdi
jmp    1260 <save_state+0xc0>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xdbe(%rip),%rdi
mov    %eax,%esi
call   1070 <log_msg@plt>
jmp    1298 <save_state+0xf8>
mov    (%r15),%rdi
call   10c0 <strerror@plt>
lea    0xdd9(%rip),%rdi
jmp    1289 <save_state+0xe9>
mov    (%r15),%rdi
call   10c0 <strerror@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
call   1070 <log_msg@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%r15),%rdi
call   10c0 <strerror@plt>
lea    0xd9f(%rip),%rdi
jmp    1260 <save_state+0xc0>
xor    %eax,%eax
jmp    129d <save_state+0xfd>

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

LLVM IR (-O2):
@.str = [30 x i8] c"/var/lib/dsme/saved_state.new\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [51 x i8] c"Could not open /var/lib/dsme/saved_state.new - %s\0A\00"
@errno = global i64 0
@.str.3 = [28 x i8] c"Could not write state - %s\0A\00"
@.str.4 = [26 x i8] c"Could not sync data - %s\0A\00"
@.str.5 = [26 x i8] c"/var/lib/dsme/saved_state\00"
@.str.6 = [82 x i8] c"Could not rename /var/lib/dsme/saved_state.new to /var/lib/dsme/saved_state - %s\0A\00"
define i32 @save_state(i8* %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = tail call i32 @strerror(i64 %5)
%7 = tail call i32 @log_msg(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %6)
br label %41
8: ; preds = %1
store i64 0, i64* @errno
%9 = tail call i32 @strlen(i8* %0)
%10 = tail call i64 @fwrite(i8* %0, i32 1, i32 %9, i32* %2)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = tail call i32 @strerror(i64 %13)
%15 = tail call i32 @log_msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i64 @fclose(i32* %2)
br label %41
17: ; preds = %8
%18 = tail call i32 @fflush(i32* %2)
%19 = tail call i32 @fileno(i32* %2)
%20 = tail call i64 @fsync(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = tail call i32 @strerror(i64 %23)
%25 = tail call i32 @log_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = tail call i64 @fclose(i32* %2)
br label %41
27: ; preds = %17
%28 = tail call i64 @fclose(i32* %2)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = tail call i32 @strerror(i64 %31)
%33 = tail call i32 @log_msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %41
34: ; preds = %27
%35 = tail call i64 @rename(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = tail call i32 @strerror(i64 %38)
%40 = tail call i32 @log_msg(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.6, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %34, %37, %30, %22, %12, %4
%42 = phi i32 [ -1, %12 ], [ -1, %22 ], [ -1, %30 ], [ -1, %37 ], [ -1, %4 ], [ 0, %34 ]
ret i32 %42
}