#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*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
char *strrchr(char const *, char);
int fixup_address(const char *target_addr, const char *remote_chan_addr,
                  char *dest) {
  const char *p, *colon;
  int i = 0;
  p = target_addr;
  colon = strrchr(p, ':');
  if (colon) {
    i = colon - p + 1;
    memcpy(dest, target_addr, i);
  } else {
    strcpy(dest, target_addr);
    i = strlen(dest);
    dest[i++] = ':';
  }
  colon = strrchr(remote_chan_addr, ':');
  p = (colon ? (colon + 1) : remote_chan_addr);
  strcpy(dest + i, p);
  return 0;
}


x86 assembly (clang, -O0):
<fixup_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a7 <fixup_address+0x67>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1060 <memcpy@plt>
jmp    11d6 <fixup_address+0x96>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x3a,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1204 <fixup_address+0xc4>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    120c <fixup_address+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fixup_address>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x3a,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1178 <fixup_address+0x38>
mov    %rax,%rbx
sub    %r12d,%ebx
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
jmp    1197 <fixup_address+0x57>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
cltq
add    $0x1,%ebx
movb   $0x3a,(%r15,%rax,1)
mov    %r14,%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %r14,%rsi
movslq %ebx,%rax
add    %rax,%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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