#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*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char *);
FILE *fopen_file(char *prefix, char *path, char *flag_str) {
  int prefix_len = (prefix == NULL) ? 0 : strlen(prefix);
  int path_len = strlen(path);
  char *fullpath;
  FILE *fptr;
  fullpath = malloc(prefix_len + path_len + 1);
  if (fullpath == NULL)
    return NULL;
  memcpy(fullpath, prefix, prefix_len);
  memcpy(fullpath + prefix_len, path, path_len + 1);
  fptr = fopen(fullpath, flag_str);
  free(fullpath);
  return fptr;
}


x86 assembly (clang, -O0):
<fopen_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1179 <fopen_file+0x29>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1185 <fopen_file+0x35>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
add    -0x28(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c1 <fopen_file+0x71>
movq   $0x0,-0x8(%rbp)
jmp    120d <fopen_file+0xbd>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
test   %rdi,%rdi
je     1176 <fopen_file+0x26>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
jmp    1179 <fopen_file+0x29>
xor    %r13d,%r13d
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0x1(%rbp,%r13,1),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d1 <fopen_file+0x81>
mov    %rax,%rbx
add    $0x1,%ebp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
movslq %r13d,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
jmp    11d3 <fopen_file+0x83>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @fopen_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%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
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
br label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
br label %18
18: ; preds = %15, %14
%19 = phi i32 [ 0, %14 ], [ %17, %15 ]
store i32 %19, i32* %8
%20 = load i8*, i8** %6
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = add nsw i32 %22, %23
%25 = add nsw i32 %24, 1
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %18
store i32* null, i32** %4
br label %49
30: ; preds = %18
%31 = load i8*, i8** %10
%32 = load i8*, i8** %5
%33 = load i32, i32* %8
%34 = call i32 @memcpy(i8* %31, i8* %32, i32 %33)
%35 = load i8*, i8** %10
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8*, i8** %6
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
%42 = call i32 @memcpy(i8* %38, i8* %39, i32 %41)
%43 = load i8*, i8** %10
%44 = load i8*, i8** %7
%45 = call i32* @fopen(i8* %43, i8* %44)
store i32* %45, i32** %11
%46 = load i8*, i8** %10
%47 = call i32 @free(i8* %46)
%48 = load i32*, i32** %11
store i32* %48, i32** %4
br label %49
49: ; preds = %30, %29
%50 = load i32*, i32** %4
ret i32* %50
}

LLVM IR (-O2):
define noalias i32* @fopen_file(i8* %0, i8* %1, i8* nocapture %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
br label %7
7: ; preds = %3, %5
%8 = phi i32 [ %6, %5 ], [ 0, %3 ]
%9 = tail call i32 @strlen(i8* %1)
%10 = add i32 %9, 1
%11 = add i32 %10, %8
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %21, label %14
14: ; preds = %7
%15 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %8)
%16 = sext i32 %8 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = tail call i32 @memcpy(i8* %17, i8* %1, i32 %10)
%19 = tail call i32* @fopen(i8* %12, i8* %2)
%20 = tail call i32 @free(i8* %12)
br label %21
21: ; preds = %7, %14
%22 = phi i32* [ %19, %14 ], [ null, %7 ]
ret i32* %22
}