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

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_file(char *, char *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ OPENSSL_free(char *);
char *test_mk_file_path(char const *, char const *);
int read_text_file(const char *dir, const char *file, char *buffer,
                   int buffer_length) {
  int len = -1;
  char *file_path = test_mk_file_path(dir, file);
  if (file_path != NULL) {
    BIO *file_io = BIO_new_file(file_path, "r");
    if (file_io != NULL)
      len = BIO_read(file_io, buffer, buffer_length);
    BIO_free(file_io);
  }
  OPENSSL_free(file_path);
  return len;
}


x86 assembly (clang, -O0):
<read_text_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <test_mk_file_path@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c5 <read_text_file+0x75>
mov    -0x28(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1070 <BIO_new_file@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11bc <read_text_file+0x6c>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <BIO_read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <BIO_free@plt>
mov    -0x28(%rbp),%rdi
call   1060 <OPENSSL_free@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
call   1040 <test_mk_file_path@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1194 <read_text_file+0x44>
lea    0xe8f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <BIO_new_file@plt>
mov    %rax,%rbp
test   %rax,%rax
je     119c <read_text_file+0x4c>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <BIO_read@plt>
mov    %eax,%r14d
jmp    11a2 <read_text_file+0x52>
mov    $0xffffffff,%r14d
jmp    11aa <read_text_file+0x5a>
mov    $0xffffffff,%r14d
mov    %rbp,%rdi
call   1050 <BIO_free@plt>
mov    %rbx,%rdi
call   1060 <OPENSSL_free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @read_text_file(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 -1, i32* %9
%12 = load i8*, i8** %5
%13 = load i8*, i8** %6
%14 = call i8* @test_mk_file_path(i8* %12, i8* %13)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %30
17: ; preds = %4
%18 = load i8*, i8** %10
%19 = call i32* @BIO_new_file(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %11
%20 = load i32*, i32** %11
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32*, i32** %11
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = call i32 @BIO_read(i32* %23, i8* %24, i32 %25)
store i32 %26, i32* %9
br label %27
27: ; preds = %22, %17
%28 = load i32*, i32** %11
%29 = call i32 @BIO_free(i32* %28)
br label %30
30: ; preds = %27, %4
%31 = load i8*, i8** %10
%32 = call i32 @OPENSSL_free(i8* %31)
%33 = load i32, i32* %9
ret i32 %33
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @read_text_file(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i8* @test_mk_file_path(i8* %0, i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %15, label %7
7: ; preds = %4
%8 = tail call i32* @BIO_new_file(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @BIO_read(i32* %8, i8* %2, i32 %3)
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ %11, %10 ], [ -1, %7 ]
%14 = tail call i32 @BIO_free(i32* %8)
br label %15
15: ; preds = %12, %4
%16 = phi i32 [ %13, %12 ], [ -1, %4 ]
%17 = tail call i32 @OPENSSL_free(i8* %5)
ret i32 %16
}