#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 */
int /*<<< orphan*/ free(char *);
scalar_t__ get_first_matching_file_pattern_list(char *, int, char *, char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *replace_char_with_string_alloc(char *, char, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *get_file_matching_given_pattern_alloc(char *original_file,
                                            char *file_pattern) {
  int path_length = 0, filename_without_ext_length = 0;
  char *d = strrchr(original_file, '/');
  char *ext = (d ? strrchr(d, '.') : strrchr(original_file, '.'));
  char path[256] = "", filename_without_ext[256] = "";
  char new_file[256] = "";
  char *pattern = NULL;
  char *res_str = NULL;
  if (d != NULL) {
    path_length = d - original_file;
    if (ext != NULL)
      filename_without_ext_length = ext - d - 1;
    strncpy(filename_without_ext, d + 1, filename_without_ext_length);
  }
  strncpy(path, original_file, path_length);
  if (filename_without_ext[0] != '\0') {
    pattern =
        replace_char_with_string_alloc(file_pattern, '$', filename_without_ext);
  } else {
    pattern = file_pattern;
  }
  printf("util: path = %s\n", path);
  if (get_first_matching_file_pattern_list(new_file, 256, path, pattern)) {
    res_str = malloc(256);
    snprintf(res_str, 255, "%s/%s", path, new_file);
    res_str[255] = '\0';
  }
  if (filename_without_ext[0] != '\0')
    free(pattern);
  return res_str;
}


x86 assembly (clang, -O0):
<get_file_matching_given_pattern_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e8 <get_file_matching_given_pattern_alloc+0x58>
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x348(%rbp)
jmp    11fd <get_file_matching_given_pattern_alloc+0x6d>
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x348(%rbp)
mov    -0x348(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x330(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
movq   $0x0,-0x338(%rbp)
movq   $0x0,-0x340(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12a4 <get_file_matching_given_pattern_alloc+0x114>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     128d <get_file_matching_given_pattern_alloc+0xfd>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,-0x18(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
movsbl -0x230(%rbp),%eax
cmp    $0x0,%eax
je     12e8 <get_file_matching_given_pattern_alloc+0x158>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rdx
mov    $0x24,%esi
call   10b0 <replace_char_with_string_alloc@plt>
mov    %rax,-0x338(%rbp)
jmp    12f3 <get_file_matching_given_pattern_alloc+0x163>
mov    -0x10(%rbp),%rax
mov    %rax,-0x338(%rbp)
lea    -0x130(%rbp),%rsi
lea    0xcff(%rip),%rdi
call   1050 <printf@plt>
lea    -0x330(%rbp),%rdi
lea    -0x130(%rbp),%rdx
mov    -0x338(%rbp),%rcx
mov    $0x100,%esi
call   10a0 <get_first_matching_file_pattern_list@plt>
cmp    $0x0,%rax
je     1374 <get_file_matching_given_pattern_alloc+0x1e4>
mov    $0x100,%edi
call   1090 <malloc@plt>
mov    %rax,-0x340(%rbp)
mov    -0x340(%rbp),%rdi
lea    -0x130(%rbp),%rcx
lea    -0x330(%rbp),%r8
mov    $0xff,%esi
lea    0xcb0(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x340(%rbp),%rax
movb   $0x0,0xff(%rax)
movsbl -0x230(%rbp),%eax
cmp    $0x0,%eax
je     1390 <get_file_matching_given_pattern_alloc+0x200>
mov    -0x338(%rbp),%rdi
call   1030 <free@plt>
mov    -0x340(%rbp),%rax
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_matching_given_pattern_alloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x308,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     1347 <get_file_matching_given_pattern_alloc+0x1c7>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movaps %xmm0,0x2f0(%rsp)
movaps %xmm0,0x2e0(%rsp)
movaps %xmm0,0x2d0(%rsp)
movaps %xmm0,0x2c0(%rsp)
movaps %xmm0,0x2b0(%rsp)
movaps %xmm0,0x2a0(%rsp)
movaps %xmm0,0x290(%rsp)
movaps %xmm0,0x280(%rsp)
movaps %xmm0,0x270(%rsp)
movaps %xmm0,0x260(%rsp)
movaps %xmm0,0x250(%rsp)
movaps %xmm0,0x240(%rsp)
movaps %xmm0,0x230(%rsp)
movaps %xmm0,0x220(%rsp)
movaps %xmm0,0x210(%rsp)
movaps %xmm0,0x200(%rsp)
mov    %ebx,%ebp
sub    %r15d,%ebp
mov    %ebx,%ecx
not    %ecx
add    %eax,%ecx
xor    %edx,%edx
test   %rax,%rax
cmovne %ecx,%edx
add    $0x1,%rbx
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
jmp    14b3 <get_file_matching_given_pattern_alloc+0x333>
xorps  %xmm0,%xmm0
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movaps %xmm0,0x2f0(%rsp)
movaps %xmm0,0x2e0(%rsp)
movaps %xmm0,0x2d0(%rsp)
movaps %xmm0,0x2c0(%rsp)
movaps %xmm0,0x2b0(%rsp)
movaps %xmm0,0x2a0(%rsp)
movaps %xmm0,0x290(%rsp)
movaps %xmm0,0x280(%rsp)
movaps %xmm0,0x270(%rsp)
movaps %xmm0,0x260(%rsp)
movaps %xmm0,0x250(%rsp)
movaps %xmm0,0x240(%rsp)
movaps %xmm0,0x230(%rsp)
movaps %xmm0,0x220(%rsp)
movaps %xmm0,0x210(%rsp)
movaps %xmm0,0x200(%rsp)
xor    %ebp,%ebp
lea    0x100(%rsp),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
cmpb   $0x0,(%rsp)
je     14de <get_file_matching_given_pattern_alloc+0x35e>
mov    %rsp,%rdx
mov    %r14,%rdi
mov    $0x24,%esi
call   10a0 <replace_char_with_string_alloc@plt>
mov    %rax,%r14
lea    0xb1b(%rip),%rdi
lea    0x100(%rsp),%rbx
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0x200(%rsp),%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1090 <get_first_matching_file_pattern_list@plt>
test   %rax,%rax
je     1569 <get_file_matching_given_pattern_alloc+0x3e9>
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xaeb(%rip),%rdx
lea    0x100(%rsp),%rcx
lea    0x200(%rsp),%r8
mov    %rax,%rdi
mov    $0xff,%esi
call   1060 <snprintf@plt>
movb   $0x0,0xff(%rbx)
cmpb   $0x0,(%rsp)
je     1558 <get_file_matching_given_pattern_alloc+0x3d8>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x308,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
cmpb   $0x0,(%rsp)
jne    1550 <get_file_matching_given_pattern_alloc+0x3d0>
jmp    1558 <get_file_matching_given_pattern_alloc+0x3d8>

LLVM IR (-O0):
@.str = [17 x i8] c"util: path = %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @get_file_matching_given_pattern_alloc(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
%14 = load i8*, i8** %3
%15 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i8*, i8** %7
%20 = call i8* @strrchr(i8* %19, i8 signext 46)
br label %24
21: ; preds = %2
%22 = load i8*, i8** %3
%23 = call i8* @strrchr(i8* %22, i8 signext 46)
br label %24
24: ; preds = %21, %18
%25 = phi i8* [ %20, %18 ], [ %23, %21 ]
store i8* %25, i8** %8
%26 = bitcast [256 x i8]* %9 to i8*
%27 = bitcast [256 x i8]* %10 to i8*
%28 = bitcast [256 x i8]* %11 to i8*
store i8* null, i8** %12
store i8* null, i8** %13
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %54
31: ; preds = %24
%32 = load i8*, i8** %7
%33 = load i8*, i8** %3
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
store i32 %37, i32* %5
%38 = load i8*, i8** %8
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %48
40: ; preds = %31
%41 = load i8*, i8** %8
%42 = load i8*, i8** %7
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = sub nsw i64 %45, 1
%47 = trunc i64 %46 to i32
store i32 %47, i32* %6
br label %48
48: ; preds = %40, %31
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%50 = load i8*, i8** %7
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = load i32, i32* %6
%53 = call i32 @strncpy(i8* %49, i8* %51, i32 %52)
br label %54
54: ; preds = %48, %24
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%56 = load i8*, i8** %3
%57 = load i32, i32* %5
%58 = call i32 @strncpy(i8* %55, i8* %56, i32 %57)
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %54
%64 = load i8*, i8** %4
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%66 = call i8* @replace_char_with_string_alloc(i8* %64, i8 signext 36, i8* %65)
store i8* %66, i8** %12
br label %69
67: ; preds = %54
%68 = load i8*, i8** %4
store i8* %68, i8** %12
br label %69
69: ; preds = %67, %63
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%71 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %70)
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%74 = load i8*, i8** %12
%75 = call i64 @get_first_matching_file_pattern_list(i8* %72, i32 256, i8* %73, i8* %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %85
77: ; preds = %69
%78 = call i8* @malloc(i32 256)
store i8* %78, i8** %13
%79 = load i8*, i8** %13
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%81 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%82 = call i32 @snprintf(i8* %79, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %80, i8* %81)
%83 = load i8*, i8** %13
%84 = getelementptr inbounds i8, i8* %83, i64 255
store i8 0, i8* %84
br label %85
85: ; preds = %77, %69
%86 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%87 = load i8, i8* %86
%88 = sext i8 %87 to i32
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %93
90: ; preds = %85
%91 = load i8*, i8** %12
%92 = call i32 @free(i8* %91)
br label %93
93: ; preds = %90, %85
%94 = load i8*, i8** %13
ret i8* %94
}

LLVM IR (-O2):
@.str = [17 x i8] c"util: path = %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @get_file_matching_given_pattern_alloc(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
br label %29
12: ; preds = %2
%13 = tail call i8* @strrchr(i8* %6, i8 signext 46)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = ptrtoint i8* %6 to i64
%18 = ptrtoint i8* %0 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
%21 = icmp eq i8* %13, null
%22 = ptrtoint i8* %13 to i64
%23 = xor i64 %17, -1
%24 = add i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = select i1 %21, i32 0, i32 %25
%27 = getelementptr inbounds i8, i8* %6, i64 1
%28 = call i32 @strncpy(i8* %15, i8* %27, i32 %26)
br label %29
29: ; preds = %8, %12
%30 = phi i32 [ %20, %12 ], [ 0, %8 ]
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%34 = call i32 @strncpy(i8* %31, i8* %0, i32 %30)
%35 = load i8, i8* %32
%36 = icmp eq i8 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = call i8* @replace_char_with_string_alloc(i8* %1, i8 signext 36, i8* %32)
br label %39
39: ; preds = %29, %37
%40 = phi i8* [ %38, %37 ], [ %1, %29 ]
%41 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %31)
%42 = call i64 @get_first_matching_file_pattern_list(i8* %33, i32 256, i8* %31, i8* %40)
%43 = icmp eq i64 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %39
%45 = call dereferenceable_or_null(256) i8* @malloc(i32 256)
%46 = call i32 @snprintf(i8* %45, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %33)
%47 = getelementptr inbounds i8, i8* %45, i64 255
store i8 0, i8* %47
br label %48
48: ; preds = %44, %39
%49 = phi i8* [ %45, %44 ], [ null, %39 ]
%50 = load i8, i8* %32
%51 = icmp eq i8 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %48
%53 = call i32 @free(i8* %40)
br label %54
54: ; preds = %52, %48
ret i8* %49
}