#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*/ strcat(char *, char *);
scalar_t__ strrchr(char *, char);
FILE *open_ext(char *fn, char *ext) {
  FILE *fp;
  char *p;
  if ((fp = fopen(fn, "rb")) != 0)
    return fp;
  if ((p = strrchr(fn, '/')) == 0)
    p = fn;
  if (strrchr(p, '.') != 0)
    return 0;
  strcat(p, ".");
  strcat(p, ext);
  if ((fp = fopen(fn, "rb")) != 0)
    return fp;
  return 0;
}


x86 assembly (clang, -O0):
<open_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     116b <open_ext+0x3b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1204 <open_ext+0xd4>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    118f <open_ext+0x5f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
cmp    $0x0,%rax
je     11b4 <open_ext+0x84>
movq   $0x0,-0x8(%rbp)
jmp    1204 <open_ext+0xd4>
mov    -0x28(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11fc <open_ext+0xcc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1204 <open_ext+0xd4>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_ext>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xebe(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
jne    1177 <open_ext+0x47>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
cmove  %r15,%rbx
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     117d <open_ext+0x4d>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
lea    0xe62(%rip),%rsi
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fopen@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [2 x i8] c".\00"
define i32* @open_ext(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32*, i32** %6
store i32* %12, i32** %3
br label %37
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i64 @strrchr(i8* %14, i8 signext 47)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i8*, i8** %4
store i8* %19, i8** %7
br label %20
20: ; preds = %18, %13
%21 = load i8*, i8** %7
%22 = call i64 @strrchr(i8* %21, i8 signext 46)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32* null, i32** %3
br label %37
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %7
%29 = load i8*, i8** %5
%30 = call i32 @strcat(i8* %28, i8* %29)
%31 = load i8*, i8** %4
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %32, i32** %6
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = load i32*, i32** %6
store i32* %35, i32** %3
br label %37
36: ; preds = %25
store i32* null, i32** %3
br label %37
37: ; preds = %36, %34, %24, %11
%38 = load i32*, i32** %3
ret i32* %38
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [2 x i8] c".\00"
define noalias i32* @open_ext(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = tail call i64 @strrchr(i8* %0, i8 signext 47)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
%9 = select i1 %8, i8* %0, i8* %7
%10 = tail call i64 @strrchr(i8* %9, i8 signext 46)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %5
%13 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @strcat(i8* %9, i8* %1)
%15 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %12, %5, %2
%17 = phi i32* [ %3, %2 ], [ null, %5 ], [ %15, %12 ]
ret i32* %17
}