#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 bootmod {
  int dummy;
};

/* Variables and functions */
char *alloca(int);
int load_module_internal(char *, struct bootmod *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char const *, char);
int try_load_module(char const *name, char const *extension,
                    struct bootmod *bm) {
  char const *dotpos = strrchr(name, '.');
  char *buf;
  if (dotpos == NULL) {
    dotpos = name + strlen(name);
  }
  buf = alloca(dotpos - name + strlen(extension) + 1);
  memcpy(buf, name, dotpos - name);
  strcpy(buf + (dotpos - name), extension);
  return load_module_internal(buf, bm);
}


x86 assembly (clang, -O0):
<try_load_module>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b2 <try_load_module+0x52>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <alloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <load_module_internal@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_load_module>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1192 <try_load_module+0x32>
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
add    %r12,%rbx
sub    %r12,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbx,1),%edi
add    $0x1,%edi
call   1070 <alloca@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1080 <memcpy@plt>
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <load_module_internal@plt>

LLVM IR (-O0):
%struct.bootmod = type { i32 }
define i32 @try_load_module(i8* %0, i8* %1, %struct.bootmod* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.bootmod*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.bootmod* %2, %struct.bootmod** %6
%9 = load i8*, i8** %4
%10 = call i8* @strrchr(i8* %9, i8 signext 46)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8* %18, i8** %7
br label %19
19: ; preds = %13, %3
%20 = load i8*, i8** %7
%21 = load i8*, i8** %4
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = sext i32 %26 to i64
%28 = add nsw i64 %24, %27
%29 = add nsw i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i8* @alloca(i32 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = load i8*, i8** %4
%34 = load i8*, i8** %7
%35 = load i8*, i8** %4
%36 = ptrtoint i8* %34 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = trunc i64 %38 to i32
%40 = call i32 @memcpy(i8* %32, i8* %33, i32 %39)
%41 = load i8*, i8** %8
%42 = load i8*, i8** %7
%43 = load i8*, i8** %4
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = getelementptr inbounds i8, i8* %41, i64 %46
%48 = load i8*, i8** %5
%49 = call i32 @strcpy(i8* %47, i8* %48)
%50 = load i8*, i8** %8
%51 = load %struct.bootmod*, %struct.bootmod** %6
%52 = call i32 @load_module_internal(i8* %50, %struct.bootmod* %51)
ret i32 %52
}

LLVM IR (-O2):
%struct.bootmod = type { i32 }
define i32 @try_load_module(i8* %0, i8* %1, %struct.bootmod* %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
br label %10
10: ; preds = %6, %3
%11 = phi i8* [ %9, %6 ], [ %4, %3 ]
%12 = ptrtoint i8* %11 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = tail call i32 @strlen(i8* %1)
%16 = trunc i64 %14 to i32
%17 = add i32 %15, 1
%18 = add i32 %17, %16
%19 = tail call i8* @alloca(i32 %18)
%20 = tail call i32 @memcpy(i8* %19, i8* %0, i32 %16)
%21 = getelementptr inbounds i8, i8* %19, i64 %14
%22 = tail call i32 @strcpy(i8* %21, i8* %1)
%23 = tail call i32 @load_module_internal(i8* %19, %struct.bootmod* %2)
ret i32 %23
}