#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 scalar_t__ ulong;
typedef int pid_t;

/* Variables and functions */
scalar_t__ get_module_base(int, char const *);
int /*<<< orphan*/ printf(char *, ...);
void *get_remote_addr(pid_t target_pid, const char *module_name,
                      ulong local_addr) {
  ulong local_handle, remote_handle;
  local_handle = get_module_base(-1, module_name);
  remote_handle = get_module_base(target_pid, module_name);
  {
    printf("[D] ");
    printf("[+] [%s] get_remote_addr: local[%p], remote[%p]\n", module_name,
           local_handle, remote_handle);
  };
  ulong ret_addr = local_addr + remote_handle - local_handle;
  return (void *)ret_addr;
}


x86 assembly (clang, -O0):
<get_remote_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0xffffffff,%edi
call   1030 <get_module_base@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <get_module_base@plt>
mov    %rax,-0x28(%rbp)
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_remote_addr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0xffffffff,%edi
call   1030 <get_module_base@plt>
mov    %rax,%r12
mov    %r15d,%edi
mov    %r14,%rsi
call   1030 <get_module_base@plt>
mov    %rax,%rbp
lea    0xead(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea4(%rip),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
sub    %r12,%rbx
add    %rbp,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"[D] \00"
@.str.1 = [49 x i8] c"[+] [%s] get_remote_addr: local[%p], remote[%p]\0A\00"
define i8* @get_remote_addr(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
%11 = call i64 @get_module_base(i32 -1, i8* %10)
store i64 %11, i64* %7
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = call i64 @get_module_base(i32 %12, i8* %13)
store i64 %14, i64* %8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = load i64, i64* %8
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %16, i64 %17, i64 %18)
%20 = load i64, i64* %6
%21 = load i64, i64* %8
%22 = add nsw i64 %20, %21
%23 = load i64, i64* %7
%24 = sub nsw i64 %22, %23
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = inttoptr i64 %25 to i8*
ret i8* %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"[D] \00"
@.str.1 = [49 x i8] c"[+] [%s] get_remote_addr: local[%p], remote[%p]\0A\00"
define i8* @get_remote_addr(i32 %0, i8* %1, i64 %2) {
%4 = tail call i64 @get_module_base(i32 -1, i8* %1)
%5 = tail call i64 @get_module_base(i32 %0, i8* %1)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %1, i64 %4, i64 %5)
%8 = sub i64 %2, %4
%9 = add i64 %8, %5
%10 = inttoptr i64 %9 to i8*
ret i8* %10
}