#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*/ sprintf(char *, char *, char const *);
const char *test_util_find_img(const char *fname, char *buf) {
  sprintf(buf, "data/%s", fname);
  if (file_exists(buf))
    return buf;
  sprintf(buf, "../data/%s", fname);
  if (file_exists(buf))
    return buf;
  sprintf(buf, "../../data/%s", fname);
  if (file_exists(buf))
    return buf;
  sprintf(buf, "../../../data/%s", fname);
  if (file_exists(buf))
    return buf;
  return fname;
}


x86 assembly (clang, -O0):
<test_util_find_img>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xeca(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%rax
je     1164 <test_util_find_img+0x44>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1208 <test_util_find_img+0xe8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe93(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%rax
je     1198 <test_util_find_img+0x78>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1208 <test_util_find_img+0xe8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe5c(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%rax
je     11cc <test_util_find_img+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1208 <test_util_find_img+0xe8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe25(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%rax
je     1200 <test_util_find_img+0xe0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1208 <test_util_find_img+0xe8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_util_find_img>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xed8(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <file_exists@plt>
test   %rax,%rax
jne    11a8 <test_util_find_img+0x88>
lea    0xeb6(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <file_exists@plt>
test   %rax,%rax
jne    11a8 <test_util_find_img+0x88>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <file_exists@plt>
test   %rax,%rax
jne    11a8 <test_util_find_img+0x88>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <file_exists@plt>
test   %rax,%rax
cmove  %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [8 x i8] c"data/%s\00"
@.str.1 = [11 x i8] c"../data/%s\00"
@.str.2 = [14 x i8] c"../../data/%s\00"
@.str.3 = [17 x i8] c"../../../data/%s\00"
define i8* @test_util_find_img(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %5
%7 = load i8*, i8** %4
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %5
%10 = call i64 @file_exists(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i8*, i8** %5
store i8* %13, i8** %3
br label %43
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = load i8*, i8** %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %5
%19 = call i64 @file_exists(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i8*, i8** %5
store i8* %22, i8** %3
br label %43
23: ; preds = %14
%24 = load i8*, i8** %5
%25 = load i8*, i8** %4
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %5
%28 = call i64 @file_exists(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i8*, i8** %5
store i8* %31, i8** %3
br label %43
32: ; preds = %23
%33 = load i8*, i8** %5
%34 = load i8*, i8** %4
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %5
%37 = call i64 @file_exists(i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = load i8*, i8** %5
store i8* %40, i8** %3
br label %43
41: ; preds = %32
%42 = load i8*, i8** %4
store i8* %42, i8** %3
br label %43
43: ; preds = %41, %39, %30, %21, %12
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
@.str = [8 x i8] c"data/%s\00"
@.str.1 = [11 x i8] c"../data/%s\00"
@.str.2 = [14 x i8] c"../../data/%s\00"
@.str.3 = [17 x i8] c"../../../data/%s\00"
define i8* @test_util_find_img(i8* %0, i8* %1) {
%3 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = tail call i64 @file_exists(i8* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call i64 @file_exists(i8* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %6
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%12 = tail call i64 @file_exists(i8* %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%16 = tail call i64 @file_exists(i8* %1)
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i8* %0, i8* %1
br label %19
19: ; preds = %14, %10, %6, %2
%20 = phi i8* [ %1, %2 ], [ %1, %6 ], [ %1, %10 ], [ %18, %14 ]
ret i8* %20
}