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

/* Variables and functions */
char *dirname(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memmove(char *, char *, int);
scalar_t__ readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
size_t strlen(char *);
char *strrchr(char *, char);
char *get_pattern_path(char *pattern) {
  static char buffer[255];
  char *ext = strrchr(pattern, '.');
  if (ext && !strcmp(ext, ".so"))
    return pattern;
  if (readlink("/proc/self/exe", buffer, sizeof(buffer)) < 0) {
    fprintf(stderr, "Could not resolve pattern path.\n");
    return pattern;
  }
  char *dir = dirname(buffer);
  memmove(buffer, dir, sizeof(buffer));
  size_t len = strlen(buffer);
  snprintf(buffer + len, sizeof(buffer) - len, "/patterns/%s.so", pattern);
  return buffer;
}


x86 assembly (clang, -O0):
<get_pattern_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cf <get_pattern_path+0x4f>
mov    -0x18(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    11cf <get_pattern_path+0x4f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1279 <get_pattern_path+0xf9>
lea    0xe2a(%rip),%rdi
lea    0x2e93(%rip),%rsi
mov    $0xff,%edx
call   1030 <readlink@plt>
cmp    $0x0,%rax
jge    1213 <get_pattern_path+0x93>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1279 <get_pattern_path+0xf9>
lea    0x2e56(%rip),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0x2e42(%rip),%rdi
mov    $0xff,%edx
call   10a0 <memmove@plt>
lea    0x2e31(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
lea    0x2e21(%rip),%rdi
add    -0x28(%rbp),%rdi
mov    $0xff,%eax
sub    -0x28(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xdc7(%rip),%rdx
call   1050 <snprintf@plt>
lea    0x2dfb(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pattern_path>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11a9 <get_pattern_path+0x29>
lea    0xe9f(%rip),%rsi
mov    %rax,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
je     121f <get_pattern_path+0x9f>
lea    0xe50(%rip),%rdi
lea    0x2eb9(%rip),%r14
mov    $0xff,%ebp
mov    %r14,%rsi
mov    $0xff,%edx
call   1030 <readlink@plt>
test   %rax,%rax
js     120a <get_pattern_path+0x8a>
mov    %r14,%rdi
call   1070 <dirname@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0xff,%edx
call   10a0 <memmove@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rdi
sub    %eax,%ebp
lea    0xe35(%rip),%rdx
mov    %ebp,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %r14,%rbx
jmp    121f <get_pattern_path+0x9f>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1090 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@get_pattern_path.buffer = internal global [255 x i8] zeroinitializer
@.str = [4 x i8] c".so\00"
@.str.1 = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Could not resolve pattern path.\0A\00"
@.str.3 = [16 x i8] c"/patterns/%s.so\00"
define i8* @get_pattern_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strrchr(i8* %7, i8 signext 46)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i32 @strcmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = load i8*, i8** %3
store i8* %16, i8** %2
br label %36
17: ; preds = %11, %1
%18 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), i32 255)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%23 = load i8*, i8** %3
store i8* %23, i8** %2
br label %36
24: ; preds = %17
%25 = call i8* @dirname(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0))
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = call i32 @memmove(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), i8* %26, i32 255)
%28 = call i64 @strlen(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0))
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = getelementptr inbounds i8, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), i64 %29
%31 = load i64, i64* %6
%32 = sub i64 255, %31
%33 = trunc i64 %32 to i32
%34 = load i8*, i8** %3
%35 = call i32 @snprintf(i8* %30, i32 %33, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %34)
store i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), i8** %2
br label %36
36: ; preds = %24, %20, %15
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@get_pattern_path.buffer = internal global [255 x i8] zeroinitializer
@.str = [4 x i8] c".so\00"
@.str.1 = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Could not resolve pattern path.\0A\00"
@.str.3 = [16 x i8] c"/patterns/%s.so\00"
define i8* @get_pattern_path(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%3 = icmp eq i8* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call i32 @strcmp(i8* %2, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %4, %1
%8 = tail call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), i32 255)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %21
13: ; preds = %7
%14 = tail call i8* @dirname(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0))
%15 = tail call i32 @memmove(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), i8* %14, i32 255)
%16 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0))
%17 = getelementptr inbounds [255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 %16
%18 = trunc i64 %16 to i32
%19 = sub i32 255, %18
%20 = tail call i32 @snprintf(i8* %17, i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %4, %13, %10
%22 = phi i8* [ %0, %10 ], [ getelementptr inbounds ([255 x i8], [255 x i8]* @get_pattern_path.buffer, i64 0, i64 0), %13 ], [ %0, %4 ]
ret i8* %22
}