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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *strdup(char *);
char *string_expand_home(char const *);
char *secure_config_get_passphrase_from_file(const char *filename) {
  FILE *file;
  char *passphrase, *filename2, buffer[1024 + 1], *pos;
  size_t num_read;
  passphrase = NULL;
  filename2 = string_expand_home(filename);
  if (!filename2)
    return NULL;
  file = fopen(filename2, "r");
  if (file) {
    num_read = fread(buffer, 1, sizeof(buffer) - 1, file);
    if (num_read > 0) {
      buffer[num_read] = '\0';
      pos = strchr(buffer, '\r');
      if (pos)
        pos[0] = '\0';
      pos = strchr(buffer, '\n');
      if (pos)
        pos[0] = '\0';
      if (buffer[0])
        passphrase = strdup(buffer);
    }
    fclose(file);
  }
  free(filename2);
  return passphrase;
}


x86 assembly (clang, -O0):
<secure_config_get_passphrase_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <string_expand_home@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ac <secure_config_get_passphrase_from_file+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    12a5 <secure_config_get_passphrase_from_file+0x135>
mov    -0x28(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1294 <secure_config_get_passphrase_from_file+0x124>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jbe    128b <secure_config_get_passphrase_from_file+0x11b>
mov    -0x440(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
mov    $0xd,%esi
call   1070 <strchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
je     1239 <secure_config_get_passphrase_from_file+0xc9>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x430(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
je     1269 <secure_config_get_passphrase_from_file+0xf9>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
cmpb   $0x0,-0x430(%rbp)
je     1286 <secure_config_get_passphrase_from_file+0x116>
lea    -0x430(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    128b <secure_config_get_passphrase_from_file+0x11b>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_config_get_passphrase_from_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
call   1060 <string_expand_home@plt>
test   %rax,%rax
je     11fb <secure_config_get_passphrase_from_file+0x8b>
mov    %rax,%r15
lea    0xe70(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ff <secure_config_get_passphrase_from_file+0x8f>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1203 <secure_config_get_passphrase_from_file+0x93>
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0xd,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11d3 <secure_config_get_passphrase_from_file+0x63>
movb   $0x0,(%rax)
mov    %rsp,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11e8 <secure_config_get_passphrase_from_file+0x78>
movb   $0x0,(%rax)
cmpb   $0x0,(%rsp)
je     1203 <secure_config_get_passphrase_from_file+0x93>
mov    %rsp,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
jmp    1205 <secure_config_get_passphrase_from_file+0x95>
xor    %ebx,%ebx
jmp    1215 <secure_config_get_passphrase_from_file+0xa5>
xor    %ebx,%ebx
jmp    120d <secure_config_get_passphrase_from_file+0x9d>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @secure_config_get_passphrase_from_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [1025 x i8]
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %5
%10 = load i8*, i8** %3
%11 = call i8* @string_expand_home(i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
store i8* null, i8** %2
br label %59
15: ; preds = %1
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %55
20: ; preds = %15
%21 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %4
%23 = call i64 @fread(i8* %21, i32 1, i32 1024, i32* %22)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = icmp ugt i64 %24, 0
br i1 %25, label %26, label %52
26: ; preds = %20
%27 = load i64, i64* %9
%28 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%30 = call i8* @strchr(i8* %29, i8 signext 13)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 0
store i8 0, i8* %35
br label %36
36: ; preds = %33, %26
%37 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%38 = call i8* @strchr(i8* %37, i8 signext 10)
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i64 0
store i8 0, i8* %43
br label %44
44: ; preds = %41, %36
%45 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%46 = load i8, i8* %45
%47 = icmp ne i8 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%50 = call i8* @strdup(i8* %49)
store i8* %50, i8** %5
br label %51
51: ; preds = %48, %44
br label %52
52: ; preds = %51, %20
%53 = load i32*, i32** %4
%54 = call i32 @fclose(i32* %53)
br label %55
55: ; preds = %52, %15
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
%58 = load i8*, i8** %5
store i8* %58, i8** %2
br label %59
59: ; preds = %55, %14
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i8* @secure_config_get_passphrase_from_file(i8* %0) {
%2 = alloca [1025 x i8]
%3 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @string_expand_home(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %32, label %6
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %29, label %9
9: ; preds = %6
%10 = call i64 @fread(i8* %3, i32 1, i32 1024, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %9
%13 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %10
store i8 0, i8* %13
%14 = call i8* @strchr(i8* %3, i8 signext 13)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %12
store i8 0, i8* %14
br label %17
17: ; preds = %16, %12
%18 = call i8* @strchr(i8* %3, i8 signext 10)
%19 = icmp eq i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %17
store i8 0, i8* %18
br label %21
21: ; preds = %20, %17
%22 = load i8, i8* %3
%23 = icmp eq i8 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i8* @strdup(i8* %3)
br label %26
26: ; preds = %21, %24, %9
%27 = phi i8* [ %25, %24 ], [ null, %21 ], [ null, %9 ]
%28 = call i32 @fclose(i32* %7)
br label %29
29: ; preds = %26, %6
%30 = phi i8* [ %27, %26 ], [ null, %6 ]
%31 = call i32 @free(i8* %4)
br label %32
32: ; preds = %1, %29
%33 = phi i8* [ %30, %29 ], [ null, %1 ]
ret i8* %33
}