#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 */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ log_msg(char *, int /*<<< orphan*/);
char *strdup(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *get_saved_state(void) {
  FILE *saved_state_file;
  char saved_state[40];
  char *ret;
  saved_state_file = fopen("/var/lib/dsme/saved_state", "r");
  if (!saved_state_file) {
    log_msg("Could not open "
            "/var/lib/dsme/saved_state"
            " - %s\n",
            strerror(errno));
    return "USER";
  }
  if (!fgets(saved_state, 40, saved_state_file)) {
    log_msg("Reading "
            "/var/lib/dsme/saved_state"
            " failed"
            " - %s\n",
            strerror(errno));
    fclose(saved_state_file);
    return "USER";
  }
  fclose(saved_state_file);
  ret = strdup(saved_state);
  if (!ret) {
    return "USER";
  } else {
    return ret;
  }
}


x86 assembly (clang, -O0):
<get_saved_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xe91(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b6 <get_saved_state+0x56>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe7b(%rip),%rdi
call   1050 <log_msg@plt>
lea    0xe9e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123f <get_saved_state+0xdf>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x28,%esi
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    1206 <get_saved_state+0xa6>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe68(%rip),%rdi
call   1050 <log_msg@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe4e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123f <get_saved_state+0xdf>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x40(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1237 <get_saved_state+0xd7>
lea    0xe1d(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123f <get_saved_state+0xdf>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_saved_state>:
push   %rbx
sub    $0x30,%rsp
lea    0xe94(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b7 <get_saved_state+0x57>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x28,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     11d5 <get_saved_state+0x75>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rsp,%rdi
call   1070 <strdup@plt>
mov    %rax,%rcx
test   %rax,%rax
lea    0xe9a(%rip),%rax
cmovne %rcx,%rax
jmp    1200 <get_saved_state+0xa0>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe50(%rip),%rdi
mov    %eax,%esi
call   1050 <log_msg@plt>
jmp    11f9 <get_saved_state+0x99>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe66(%rip),%rdi
mov    %eax,%esi
call   1050 <log_msg@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe4b(%rip),%rax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/var/lib/dsme/saved_state\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [47 x i8] c"Could not open /var/lib/dsme/saved_state - %s\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"USER\00"
@.str.4 = [47 x i8] c"Reading /var/lib/dsme/saved_state failed - %s\0A\00"
define i8* @get_saved_state() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca [40 x i8]
%4 = alloca i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 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** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %10)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %33
12: ; preds = %0
%13 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i32 @fgets(i8* %13, i32 40, i32* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %33
23: ; preds = %12
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
%26 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%27 = call i8* @strdup(i8* %26)
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %23
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %33
31: ; preds = %23
%32 = load i8*, i8** %4
store i8* %32, i8** %1
br label %33
33: ; preds = %31, %30, %17, %8
%34 = load i8*, i8** %1
ret i8* %34
}

LLVM IR (-O2):
@.str = [26 x i8] c"/var/lib/dsme/saved_state\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [47 x i8] c"Could not open /var/lib/dsme/saved_state - %s\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"USER\00"
@.str.4 = [47 x i8] c"Reading /var/lib/dsme/saved_state failed - %s\0A\00"
define i8* @get_saved_state() {
%1 = alloca [40 x i8]
%2 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %22
9: ; preds = %0
%10 = call i32 @fgets(i8* %2, i32 40, i32* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = call i32 @fclose(i32* %3)
br label %22
17: ; preds = %9
%18 = call i32 @fclose(i32* %3)
%19 = call i8* @strdup(i8* %2)
%20 = icmp eq i8* %19, null
%21 = select i1 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %19
br label %22
22: ; preds = %17, %12, %5
%23 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %5 ], [ %21, %17 ]
ret i8* %23
}