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

/* Variables and functions */
scalar_t__ file_exists(char *);
int /*<<< orphan*/ safe_free(char *);
char *strdup_together3(char const *, char const *, char const *);
char *upper_case_string(char const *);
char *exists_with_extension(const char *dir, const char *name,
                            const char *ext) {
  char *t, *x;
  t = strdup_together3(dir, name, ext);
  if (file_exists(t))
    return t;
  safe_free(t);
  x = upper_case_string(ext);
  t = strdup_together3(dir, name, x);
  safe_free(x);
  if (file_exists(t))
    return t;
  safe_free(t);
  return NULL;
}


x86 assembly (clang, -O0):
<exists_with_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <strdup_together3@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%rax
je     1189 <exists_with_extension+0x49>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <exists_with_extension+0xae>
mov    -0x28(%rbp),%rdi
call   1060 <safe_free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <upper_case_string@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <strdup_together3@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <safe_free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%rax
je     11dd <exists_with_extension+0x9d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <exists_with_extension+0xae>
mov    -0x28(%rbp),%rdi
call   1060 <safe_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exists_with_extension>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <strdup_together3@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <file_exists@plt>
test   %rax,%rax
jne    11a9 <exists_with_extension+0x69>
mov    %rbx,%rdi
call   1060 <safe_free@plt>
mov    %r12,%rdi
call   1040 <upper_case_string@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <strdup_together3@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <safe_free@plt>
mov    %rbx,%rdi
call   1030 <file_exists@plt>
test   %rax,%rax
jne    11a9 <exists_with_extension+0x69>
mov    %rbx,%rdi
call   1060 <safe_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @exists_with_extension(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = load i8*, i8** %7
%13 = call i8* @strdup_together3(i8* %10, i8* %11, i8* %12)
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = call i64 @file_exists(i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i8*, i8** %8
store i8* %18, i8** %4
br label %38
19: ; preds = %3
%20 = load i8*, i8** %8
%21 = call i32 @safe_free(i8* %20)
%22 = load i8*, i8** %7
%23 = call i8* @upper_case_string(i8* %22)
store i8* %23, i8** %9
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = load i8*, i8** %9
%27 = call i8* @strdup_together3(i8* %24, i8* %25, i8* %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %9
%29 = call i32 @safe_free(i8* %28)
%30 = load i8*, i8** %8
%31 = call i64 @file_exists(i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %19
%34 = load i8*, i8** %8
store i8* %34, i8** %4
br label %38
35: ; preds = %19
%36 = load i8*, i8** %8
%37 = call i32 @safe_free(i8* %36)
store i8* null, i8** %4
br label %38
38: ; preds = %35, %33, %17
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
define i8* @exists_with_extension(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strdup_together3(i8* %0, i8* %1, i8* %2)
%5 = tail call i64 @file_exists(i8* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %3
%8 = tail call i32 @safe_free(i8* %4)
%9 = tail call i8* @upper_case_string(i8* %2)
%10 = tail call i8* @strdup_together3(i8* %0, i8* %1, i8* %9)
%11 = tail call i32 @safe_free(i8* %9)
%12 = tail call i64 @file_exists(i8* %10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @safe_free(i8* %10)
br label %16
16: ; preds = %7, %3, %14
%17 = phi i8* [ null, %14 ], [ %4, %3 ], [ %10, %7 ]
ret i8* %17
}