#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*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcasecmp(char const *, char const *);
char *strecat(char const *, char *, char const *, char *, char const *,
              int /*<<< orphan*/ *);
unsigned int strlen(char const *);
FILE *try_other(const char *dir, const char *category, const char *resource) {
  FILE *res = NULL;
  DIR *dh;
  struct dirent *de;
  unsigned catlen = strlen(category);
  char *path = strecat(dir, "/", category, "/", resource, NULL);
  char *catptr = path + strlen(dir) + 1;
  if (!(dh = opendir(dir)))
    goto out;
  while ((de = readdir(dh))) {
    if (strlen(de->d_name) != catlen)
      continue;
    if (strcasecmp(de->d_name, category))
      continue;
    memcpy(catptr, de->d_name, catlen);
    if ((res = fopen(path, "r")))
      break;
  };
out:
  free(path);
  return res;
}


x86 assembly (clang, -O0):
<try_other>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%r8
lea    0xe45(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    %rcx,%rsi
call   1030 <strecat@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    120c <try_other+0x8c>
jmp    12a1 <try_other+0x121>
jmp    1211 <try_other+0x91>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     129c <try_other+0x11c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
cmp    -0x34(%rbp),%eax
je     1242 <try_other+0xc2>
jmp    1211 <try_other+0x91>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcasecmp@plt>
cmp    $0x0,%rax
je     1261 <try_other+0xe1>
jmp    1211 <try_other+0x91>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x34(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x40(%rbp),%rdi
lea    0xd83(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1297 <try_other+0x117>
jmp    129c <try_other+0x11c>
jmp    1211 <try_other+0x91>
jmp    12a1 <try_other+0x121>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_other>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1070 <strlen@plt>
mov    %eax,%r13d
lea    0xe5a(%rip),%rcx
xor    %r15d,%r15d
mov    %rbp,%rdi
mov    %rcx,%rsi
mov    %r12,%rdx
mov    %rbx,%r8
xor    %r9d,%r9d
call   1030 <strecat@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1070 <strlen@plt>
mov    %eax,(%rsp)
mov    %rbp,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     125b <try_other+0xdb>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1258 <try_other+0xd8>
mov    %rax,%r15
mov    (%rsp),%eax
add    %r14,%rax
add    $0x1,%rax
mov    %rax,(%rsp)
jmp    1210 <try_other+0x90>
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1258 <try_other+0xd8>
mov    (%r15),%rdi
call   1070 <strlen@plt>
cmp    %r13d,%eax
jne    1200 <try_other+0x80>
mov    (%r15),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <strcasecmp@plt>
test   %rax,%rax
jne    1200 <try_other+0x80>
mov    (%rsp),%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1080 <memcpy@plt>
mov    %r14,%rdi
lea    0xdb9(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1200 <try_other+0x80>
mov    %rax,%r15
jmp    125b <try_other+0xdb>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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