#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 *mk_file_path(char const *, char const *);
int read_text_file(const char *dir, const char *file, char *buffer,
                   int buffer_length) {
  int result = -1;
  char *file_path = mk_file_path(dir, file);
  if (file_path != NULL) {
    BIO *file_io = BIO_new_file(file_path, "r");
    OPENSSL_free(file_path);
    if (file_io != NULL) {
      result = BIO_read(file_io, buffer, buffer_length);
      BIO_free(file_io);
    }
  }
  return result;
}


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 <mk_file_path@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d3 <read_text_file+0x83>
mov    -0x28(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1070 <BIO_new_file@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <OPENSSL_free@plt>
cmpq   $0x0,-0x30(%rbp)
je     11ce <read_text_file+0x7e>
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>
jmp    11d3 <read_text_file+0x83>
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 <mk_file_path@plt>
test   %rax,%rax
je     11a5 <read_text_file+0x55>
mov    %rax,%rbx
lea    0xe8f(%rip),%rsi
mov    %rax,%rdi
call   1070 <BIO_new_file@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <OPENSSL_free@plt>
test   %rbp,%rbp
je     11a5 <read_text_file+0x55>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <BIO_read@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <BIO_free@plt>
mov    %ebx,%eax
jmp    11aa <read_text_file+0x5a>
mov    $0xffffffff,%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* @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 %32
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 i8*, i8** %10
%21 = call i32 @OPENSSL_free(i8* %20)
%22 = load i32*, i32** %11
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i32*, i32** %11
%26 = load i8*, i8** %7
%27 = load i32, i32* %8
%28 = call i32 @BIO_read(i32* %25, i8* %26, i32 %27)
store i32 %28, i32* %9
%29 = load i32*, i32** %11
%30 = call i32 @BIO_free(i32* %29)
br label %31
31: ; preds = %24, %17
br label %32
32: ; preds = %31, %4
%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* @mk_file_path(i8* %0, i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %14, 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 = tail call i32 @OPENSSL_free(i8* %5)
%10 = icmp eq i32* %8, null
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = tail call i32 @BIO_read(i32* %8, i8* %2, i32 %3)
%13 = tail call i32 @BIO_free(i32* %8)
br label %14
14: ; preds = %7, %11, %4
%15 = phi i32 [ -1, %4 ], [ %12, %11 ], [ -1, %7 ]
ret i32 %15
}