#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 dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *malloc(size_t const);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t const, char *, char const *,
                            char const *);
int /*<<< orphan*/ strcmp_insensitive(char const *, char const *);
int strlen(char const *);
char *find_audio_ts_file(const char *audio_ts_path, const char *filename) {
  DIR *dir = opendir(audio_ts_path);
  struct dirent *dirent;
  if (!dir) {
    return NULL;
  }
  for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
    if (!strcmp_insensitive(filename, dirent->d_name)) {
      const size_t dirent_len = strlen(dirent->d_name);
      const size_t full_path_len = (strlen(audio_ts_path) + 1 + dirent_len + 1);
      char *full_path = malloc(full_path_len);
      snprintf(full_path, full_path_len, "%s/%s", audio_ts_path,
               dirent->d_name);
      closedir(dir);
      return full_path;
    }
  }
  closedir(dir);
  return NULL;
}


x86 assembly (clang, -O0):
<find_audio_ts_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a5 <find_audio_ts_file+0x35>
movq   $0x0,-0x8(%rbp)
jmp    126c <find_audio_ts_file+0xfc>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125b <find_audio_ts_file+0xeb>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   1090 <strcmp_insensitive@plt>
cmp    $0x0,%eax
jne    1244 <find_audio_ts_file+0xd4>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
add    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xdd7(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126c <find_audio_ts_file+0xfc>
jmp    1249 <find_audio_ts_file+0xd9>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
jmp    11b2 <find_audio_ts_file+0x42>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_audio_ts_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r13
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     11c1 <find_audio_ts_file+0x51>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11c6 <find_audio_ts_file+0x56>
mov    %rax,%rbx
xor    %r15d,%r15d
nop
mov    (%rbx),%rsi
mov    %r13,%rdi
call   1090 <strcmp_insensitive@plt>
test   %eax,%eax
je     11cb <find_audio_ts_file+0x5b>
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <find_audio_ts_file+0x30>
jmp    120b <find_audio_ts_file+0x9b>
xor    %r15d,%r15d
jmp    1213 <find_audio_ts_file+0xa3>
xor    %r15d,%r15d
jmp    120b <find_audio_ts_file+0x9b>
mov    (%rbx),%rdi
call   1040 <strlen@plt>
movslq %eax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rax,%r15,1),%r13
add    $0x2,%r13
mov    %r13,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    (%rbx),%r8
lea    0xe03(%rip),%rdx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1060 <closedir@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define i8* @find_audio_ts_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
br label %61
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %7
br label %19
19: ; preds = %55, %16
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %58
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = load %struct.dirent*, %struct.dirent** %7
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strcmp_insensitive(i8* %23, i8* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %54, label %29
29: ; preds = %22
%30 = load %struct.dirent*, %struct.dirent** %7
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
store i64 %34, i64* %8
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = load i64, i64* %8
%40 = add i64 %38, %39
%41 = add i64 %40, 1
store i64 %41, i64* %9
%42 = load i64, i64* %9
%43 = call i8* @malloc(i64 %42)
store i8* %43, i8** %10
%44 = load i8*, i8** %10
%45 = load i64, i64* %9
%46 = load i8*, i8** %4
%47 = load %struct.dirent*, %struct.dirent** %7
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @snprintf(i8* %44, i64 %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %46, i8* %49)
%51 = load i32*, i32** %6
%52 = call i32 @closedir(i32* %51)
%53 = load i8*, i8** %10
store i8* %53, i8** %3
br label %61
54: ; preds = %22
br label %55
55: ; preds = %54
%56 = load i32*, i32** %6
%57 = call %struct.dirent* @readdir(i32* %56)
store %struct.dirent* %57, %struct.dirent** %7
br label %19
58: ; preds = %19
%59 = load i32*, i32** %6
%60 = call i32 @closedir(i32* %59)
store i8* null, i8** %3
br label %61
61: ; preds = %58, %29, %15
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define i8* @find_audio_ts_file(i8* %0, i8* %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %33, label %5
5: ; preds = %2
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %30, label %8
8: ; preds = %5, %27
%9 = phi %struct.dirent* [ %28, %27 ], [ %6, %5 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i32 @strcmp_insensitive(i8* %1, i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %8
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = tail call i32 @strlen(i8* %0)
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = add nsw i64 %18, 1
%23 = add nsw i64 %22, %21
%24 = tail call i8* @malloc(i64 %23)
%25 = load i8*, i8** %15
%26 = tail call i32 @snprintf(i8* %24, i64 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %25)
br label %30
27: ; preds = %8
%28 = tail call %struct.dirent* @readdir(i32* %3)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %8
30: ; preds = %27, %5, %14
%31 = phi i8* [ %24, %14 ], [ null, %5 ], [ null, %27 ]
%32 = tail call i32 @closedir(i32* %3)
br label %33
33: ; preds = %30, %2
%34 = phi i8* [ null, %2 ], [ %31, %30 ]
ret i8* %34
}