#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 */
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strstr(char *, char *);
char *IP2Location_replace(char *substr, char *replace, char *targetString) {
  char *buff = malloc(
      sizeof(char) *
      ((int)(strlen(targetString) / strlen(substr) + 0.5) * strlen(replace)));
  char *headCaret = targetString;
  char *endCaret = 0;
  buff[0] = 0;
  while ((endCaret = strstr(headCaret, substr)) != 0) {
    strncat(buff, headCaret, endCaret - headCaret);
    strcat(buff, replace);
    headCaret = endCaret + strlen(substr);
  }
  strcat(buff, headCaret);
  return buff;
}


x86 assembly (clang, -O0):
<IP2Location_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  0xe73(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
imul   %ecx,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1239 <IP2Location_replace+0xe9>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11d1 <IP2Location_replace+0x81>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IP2Location_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
addsd  0xe77(%rip),%xmm0
cvttsd2si %xmm0,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
imul   %eax,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11fc <IP2Location_replace+0xac>
mov    %rax,%r13
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edx
sub    %ebx,%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <strncat@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11c0 <IP2Location_replace+0x70>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @IP2Location_replace(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = tail call i32 @strlen(i8* %0)
%6 = sdiv i32 %4, %5
%7 = sitofp i32 %6 to double
%8 = fadd double %7, 5.000000e-01
%9 = fptosi double %8 to i32
%10 = tail call i32 @strlen(i8* %1)
%11 = mul nsw i32 %10, %9
%12 = tail call i8* @malloc(i32 %11)
store i8 0, i8* %12
%13 = tail call i8* @strstr(i8* dereferenceable(1) %2, i8* dereferenceable(1) %0)
%14 = icmp eq i8* %13, null
br i1 %14, label %29, label %15
15: ; preds = %3, %15
%16 = phi i8* [ %27, %15 ], [ %13, %3 ]
%17 = phi i8* [ %26, %15 ], [ %2, %3 ]
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = tail call i32 @strncat(i8* %12, i8* %17, i32 %21)
%23 = tail call i32 @strcat(i8* %12, i8* %1)
%24 = tail call i32 @strlen(i8* %0)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %16, i64 %25
%27 = tail call i8* @strstr(i8* dereferenceable(1) %26, i8* dereferenceable(1) %0)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %15
29: ; preds = %15, %3
%30 = phi i8* [ %2, %3 ], [ %26, %15 ]
%31 = tail call i32 @strcat(i8* %12, i8* %30)
ret i8* %12
}