#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*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
int replaceSubstr(char *, char **, char *, char *);
int /*<<< orphan*/ system(char *);
int main() {
  char *p = "abcd11111abcd2222abcdqqqqq";
  int ret = 0;
  char *buf = NULL;
  ret = replaceSubstr(p, &buf, "abcd", "12345");
  if (ret != 0) {
    printf("replaceSubstr err:　%d\n", ret);
    return ret;
  }
  printf("buf = %s\n", buf);
  if (buf != NULL) {
    free(buf);
    buf = NULL;
  }
  printf("\n");
  system("pause");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    0xea3(%rip),%rdx
lea    0xea1(%rip),%rcx
call   1060 <replaceSubstr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ad <main+0x6d>
mov    -0x14(%rbp),%esi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <main+0xbc>
mov    -0x20(%rbp),%rsi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     11db <main+0x9b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe59(%rip),%rdi
call   1040 <system@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe9b(%rip),%rdi
lea    0xeaf(%rip),%rdx
lea    0xead(%rip),%rcx
lea    0x8(%rsp),%rsi
call   1070 <replaceSubstr@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1195 <main+0x45>
lea    0xe9c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d6 <main+0x86>
mov    0x8(%rsp),%rsi
lea    0xe9e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11c0 <main+0x70>
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xe78(%rip),%rdi
call   1050 <system@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [27 x i8] c"abcd11111abcd2222abcdqqqqq\00"
@.str.1 = [5 x i8] c"abcd\00"
@.str.2 = [6 x i8] c"12345\00"
@.str.3 = [25 x i8] c"replaceSubstr err:\E3\80\80%d\0A\00"
@.str.4 = [10 x i8] c"buf = %s\0A\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [6 x i8] c"pause\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %2
store i32 0, i32* %3
store i8* null, i8** %4
%5 = load i8*, i8** %2
%6 = call i32 @replaceSubstr(i8* %5, i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %3
store i32 %12, i32* %1
br label %24
13: ; preds = %0
%14 = load i8*, i8** %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
store i8* null, i8** %4
br label %21
21: ; preds = %18, %13
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%23 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %24
24: ; preds = %21, %9
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [27 x i8] c"abcd11111abcd2222abcdqqqqq\00"
@.str.1 = [5 x i8] c"abcd\00"
@.str.2 = [6 x i8] c"12345\00"
@.str.3 = [25 x i8] c"replaceSubstr err:\E3\80\80%d\0A\00"
@.str.4 = [10 x i8] c"buf = %s\0A\00"
@.str.6 = [6 x i8] c"pause\00"
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%3 = call i32 @replaceSubstr(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%4 = icmp eq i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %3)
br label %17
7: ; preds = %0
%8 = load i8*, i8** %1
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %1
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = call i32 @free(i8* %10)
store i8* null, i8** %1
br label %14
14: ; preds = %12, %7
%15 = call i32 @putchar(i32 10)
%16 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %17
17: ; preds = %14, %5
ret i32 %3
}