#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*/ error_exit(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ gettext(char *);
char *strchr(char *, char);
char *strdup(char *);
char *read_file(const char *file) {
  char buffer[4096] = {0}, *lf = NULL;
  FILE *fh = fopen(file, "rb");
  if (!fh)
    error_exit(gettext("Cannot open password-file %s"), file);
  if (!fgets(buffer, sizeof buffer, fh))
    error_exit(gettext("Problem reading password from file %s"), file);
  fclose(fh);
  lf = strchr(buffer, '\n');
  if (lf)
    *lf = 0x00;
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
movq   $0x0,-0x1018(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jne    11e9 <read_file+0x69>
lea    0xe2a(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1070 <error_exit@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rdx
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%eax
jne    1221 <read_file+0xa1>
lea    0xe0f(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1070 <error_exit@plt>
mov    -0x1020(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x1010(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
je     125d <read_file+0xdd>
mov    -0x1018(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1010(%rbp),%rdi
call   10a0 <strdup@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe5d(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c9 <read_file+0x49>
lea    0xe49(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <error_exit@plt>
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %eax,%eax
jne    11f3 <read_file+0x73>
lea    0xe3c(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <error_exit@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1210 <read_file+0x90>
movb   $0x0,(%rax)
mov    %rsp,%rdi
call   10a0 <strdup@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [29 x i8] c"Cannot open password-file %s\00"
@.str.2 = [38 x i8] c"Problem reading password from file %s\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %2
%6 = bitcast [4096 x i8]* %3 to i8*
store i8* null, i8** %4
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = call i32 @gettext(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = load i8*, i8** %2
%14 = call i32 @error_exit(i32 %12, i8* %13)
br label %15
15: ; preds = %11, %1
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32 @fgets(i8* %16, i32 4096, i32* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = call i32 @gettext(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%22 = load i8*, i8** %2
%23 = call i32 @error_exit(i32 %21, i8* %22)
br label %24
24: ; preds = %20, %15
%25 = load i32*, i32** %5
%26 = call i32 @fclose(i32* %25)
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%28 = call i8* @strchr(i8* %27, i8 signext 10)
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = load i8*, i8** %4
store i8 0, i8* %32
br label %33
33: ; preds = %31, %24
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%35 = call i8* @strdup(i8* %34)
ret i8* %35
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [29 x i8] c"Cannot open password-file %s\00"
@.str.2 = [38 x i8] c"Problem reading password from file %s\00"
define noalias i8* @read_file(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @gettext(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @error_exit(i32 %7, i8* %0)
br label %9
9: ; preds = %6, %1
%10 = call i32 @fgets(i8* %3, i32 4096, i32* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @gettext(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @error_exit(i32 %13, i8* %0)
br label %15
15: ; preds = %12, %9
%16 = call i32 @fclose(i32* %4)
%17 = call i8* @strchr(i8* %3, i8 signext 10)
%18 = icmp eq i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %15
store i8 0, i8* %17
br label %20
20: ; preds = %19, %15
%21 = call i8* @strdup(i8* %3)
ret i8* %21
}