#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ nfo_file;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            char const *);
int stat(char *, struct stat *);
char *strdup(char *);
char *nfo_file_exists(const char *dir, const char *file, const char *ext) {
  struct stat st;
  char nfo_file[1024] = {0};
  int err;
  snprintf(nfo_file, sizeof(nfo_file), "%s/%s.%s", dir, file, ext);
  err = stat(nfo_file, &st);
  return !err ? strdup(nfo_file) : NULL;
}


x86 assembly (clang, -O0):
<nfo_file_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
mov    $0x400,%esi
lea    0xe77(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jne    11c9 <nfo_file_exists+0x89>
lea    -0x420(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x430(%rbp)
jmp    11d7 <nfo_file_exists+0x97>
xor    %eax,%eax
mov    %rax,-0x430(%rbp)
jmp    11d7 <nfo_file_exists+0x97>
mov    -0x430(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nfo_file_exists>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0x10(%rsp),%r13
xor    %r14d,%r14d
mov    $0x400,%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe89(%rip),%rdx
mov    %r13,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
mov    %r12,%r8
mov    %r15,%r9
call   1030 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r13,%rdi
call   1050 <stat@plt>
test   %eax,%eax
jne    11ab <nfo_file_exists+0x6b>
lea    0x10(%rsp),%rdi
call   1060 <strdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"%s/%s.%s\00"
define i8* @nfo_file_exists(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca [1024 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = bitcast [1024 x i8]* %8 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = load i8*, i8** %5
%14 = load i8*, i8** %6
%15 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %13, i8* %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%17 = call i32 @stat(i8* %16, %struct.stat* %7)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %3
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%22 = call i8* @strdup(i8* %21)
br label %24
23: ; preds = %3
br label %24
24: ; preds = %23, %20
%25 = phi i8* [ %22, %20 ], [ null, %23 ]
ret i8* %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"%s/%s.%s\00"
define noalias i8* @nfo_file_exists(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
%9 = call i32 @stat(i8* %7, %struct.stat* %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i8* @strdup(i8* %7)
br label %13
13: ; preds = %3, %11
%14 = phi i8* [ %12, %11 ], [ null, %3 ]
ret i8* %14
}