#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ log_msg(char *);
size_t strcspn(char *, char *);
char *strdup(char *);
char *get_saved_state(void) {
  char *ret = 0;
  FILE *fh = 0;
  char buf[40];
  if (!(fh = fopen("/var/lib/dsme/saved_state", "r"))) {
    log_msg("Could not open "
            "/var/lib/dsme/saved_state"
            " - %m\n");
    goto EXIT;
  }
  if (!fgets(buf, 40, fh)) {
    log_msg("Reading "
            "/var/lib/dsme/saved_state"
            " failed"
            " - %m\n");
    goto EXIT;
  }
  if ((ret = strdup(buf)))
    ret[strcspn(buf, "\r\n")] = 0;
EXIT:
  if (fh)
    fclose(fh);
  return ret ?: strdup("USER");
}


x86 assembly (clang, -O0):
<get_saved_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe81(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11aa <get_saved_state+0x4a>
lea    0xe7c(%rip),%rdi
call   1060 <log_msg@plt>
jmp    1215 <get_saved_state+0xb5>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x28,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    11d6 <get_saved_state+0x76>
lea    0xe7f(%rip),%rdi
call   1060 <log_msg@plt>
jmp    1215 <get_saved_state+0xb5>
lea    -0x40(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     1210 <get_saved_state+0xb0>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1040 <strcspn@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
jmp    1215 <get_saved_state+0xb5>
cmpq   $0x0,-0x10(%rbp)
je     1229 <get_saved_state+0xc9>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     1248 <get_saved_state+0xe8>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    1258 <get_saved_state+0xf8>
lea    0xe2e(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_saved_state>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xe92(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c3 <get_saved_state+0x63>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x28,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     11d1 <get_saved_state+0x71>
mov    %rsp,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     11dd <get_saved_state+0x7d>
mov    %rax,%rbx
lea    0xecd(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcspn@plt>
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11f4 <get_saved_state+0x94>
lea    0xe52(%rip),%rdi
call   1060 <log_msg@plt>
jmp    11e5 <get_saved_state+0x85>
lea    0xe73(%rip),%rdi
call   1060 <log_msg@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
lea    0xe91(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
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 - %m\0A\00"
@.str.3 = [47 x i8] c"Reading /var/lib/dsme/saved_state failed - %m\0A\00"
@.str.4 = [3 x i8] c"\0D\0A\00"
@.str.5 = [5 x i8] c"USER\00"
define i8* @get_saved_state() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca [40 x i8]
store i8* null, i8** %1
store i32* null, i32** %2
%4 = 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* %4, i32** %2
%5 = icmp ne i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %25
8: ; preds = %0
%9 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i32 @fgets(i8* %9, i32 40, i32* %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %25
15: ; preds = %8
%16 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %1
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i8*, i8** %1
%21 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%22 = call i64 @strcspn(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %15
br label %25
25: ; preds = %24, %13, %6
%26 = load i32*, i32** %2
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** %2
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %28, %25
%32 = load i8*, i8** %1
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %31
br label %37
35: ; preds = %31
%36 = call i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %34
%38 = phi i8* [ %32, %34 ], [ %36, %35 ]
ret i8* %38
}

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 - %m\0A\00"
@.str.3 = [47 x i8] c"Reading /var/lib/dsme/saved_state failed - %m\0A\00"
@.str.4 = [3 x i8] c"\0D\0A\00"
@.str.5 = [5 x i8] c"USER\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 %13, label %5
5: ; preds = %0
%6 = call i32 @fgets(i8* %2, i32 40, i32* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %15
10: ; preds = %5
%11 = call i8* @strdup(i8* %2)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %17
13: ; preds = %0
%14 = tail call i32 @log_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %21
15: ; preds = %8, %10
%16 = call i32 @fclose(i32* %3)
br label %21
17: ; preds = %10
%18 = call i64 @strcspn(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%19 = getelementptr inbounds i8, i8* %11, i64 %18
store i8 0, i8* %19
%20 = call i32 @fclose(i32* %3)
br label %23
21: ; preds = %15, %13
%22 = call dereferenceable_or_null(5) i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %17, %21
%24 = phi i8* [ %22, %21 ], [ %11, %17 ]
ret i8* %24
}