#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 *const);
char *malloc(scalar_t__);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *const);
int /*<<< orphan*/ strcpy(char *, char *const);
scalar_t__ strlen(char *const);
int /*<<< orphan*/ strstr(char *, char *const);
char *open_file_with_extension(char *const filename, char *const file_extension,
                               char *const open_mode, FILE **const fp) {
  char *file_used = malloc(strlen(filename) + 1);
  strcpy(file_used, filename);
  *fp = fopen(file_used, open_mode);
  if (*fp == NULL && !strstr(file_used, file_extension)) {
    file_used =
        realloc(file_used, strlen(file_used) + strlen(file_extension) + 1);
    strcat(file_used, file_extension);
    *fp = fopen(file_used, open_mode);
  }
  return file_used;
}


x86 assembly (clang, -O0):
<open_file_with_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1243 <open_file_with_extension+0xd3>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%eax
jne    1243 <open_file_with_extension+0xd3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file_with_extension>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <fopen@plt>
mov    %rax,(%r14)
test   %rax,%rax
jne    1202 <open_file_with_extension+0x92>
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %eax,%eax
jne    1202 <open_file_with_extension+0x92>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x1,%rsi
mov    %r13,%rdi
call   1060 <realloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <fopen@plt>
mov    %rax,(%r14)
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @open_file_with_extension(i8* %0, i8* %1, i8* %2, i32** %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32**
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32** %3, i32*** %8
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = add nsw i64 %11, 1
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = load i8*, i8** %5
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = load i8*, i8** %9
%18 = load i8*, i8** %7
%19 = call i32* @fopen(i8* %17, i8* %18)
%20 = load i32**, i32*** %8
store i32* %19, i32** %20
%21 = load i32**, i32*** %8
%22 = load i32*, i32** %21
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %45
24: ; preds = %4
%25 = load i8*, i8** %9
%26 = load i8*, i8** %6
%27 = call i32 @strstr(i8* %25, i8* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %45, label %29
29: ; preds = %24
%30 = load i8*, i8** %9
%31 = load i8*, i8** %9
%32 = call i64 @strlen(i8* %31)
%33 = load i8*, i8** %6
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %32, %34
%36 = add nsw i64 %35, 1
%37 = call i8* @realloc(i8* %30, i64 %36)
store i8* %37, i8** %9
%38 = load i8*, i8** %9
%39 = load i8*, i8** %6
%40 = call i32 @strcat(i8* %38, i8* %39)
%41 = load i8*, i8** %9
%42 = load i8*, i8** %7
%43 = call i32* @fopen(i8* %41, i8* %42)
%44 = load i32**, i32*** %8
store i32* %43, i32** %44
br label %45
45: ; preds = %29, %24, %4
%46 = load i8*, i8** %9
ret i8* %46
}

LLVM IR (-O2):
define i8* @open_file_with_extension(i8* %0, i8* %1, i8* nocapture %2, i32** nocapture writeonly %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i32* @fopen(i8* %7, i8* %2)
store i32* %9, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %22
11: ; preds = %4
%12 = tail call i32 @strstr(i8* %7, i8* %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%16 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%17 = add i64 %15, 1
%18 = add i64 %17, %16
%19 = tail call i8* @realloc(i8* %7, i64 %18)
%20 = tail call i32 @strcat(i8* %19, i8* %1)
%21 = tail call i32* @fopen(i8* %19, i8* %2)
store i32* %21, i32** %3
br label %22
22: ; preds = %14, %11, %4
%23 = phi i8* [ %7, %11 ], [ %19, %14 ], [ %7, %4 ]
ret i8* %23
}