#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 KMP_match(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main() {
  char *s1 = "abtetetedtedddc";
  char *s2 = "ted";
  int n = strlen(s1);
  int m = strlen(s2);
  char *s = (char *)malloc(n * sizeof(char));
  strcpy(s, s1);
  int offset = 0;
  while (1) {
    offset = KMP_match(s, s2);
    if (offset < 0)
      break;
    int copy_num = n - offset - m;
    printf("offset = %d\n", offset);
    printf("copy_num = %d\n", copy_num);
    strncpy(s + offset, s + offset + m, copy_num);
    s[n - m] = '\0';
    printf("s = %s\n", s);
  }
  printf("final str is :%s\n", s);
  free(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe7f(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcpy@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <KMP_match@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f3 <main+0x83>
jmp    126e <main+0xfe>
mov    -0x1c(%rbp),%eax
sub    -0x2c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x30(%rbp),%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x28(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x30(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    11d4 <main+0x64>
mov    -0x28(%rbp),%rsi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%r14
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%r15d
lea    0xe7c(%rip),%r12
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebp
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <KMP_match@plt>
test   %eax,%eax
js     1247 <main+0xd7>
mov    %eax,%r14d
movslq %ebp,%r12
mov    %r15d,%eax
sub    %r12d,%eax
movslq %eax,%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%ebp
sub    %r12d,%ebp
sub    %r14d,%ebp
lea    0xe24(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xe20(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r14d,%edi
add    %rbx,%rdi
lea    (%rdi,%r12,1),%rsi
mov    %ebp,%edx
call   1050 <strncpy@plt>
movb   $0x0,(%rbx,%r13,1)
lea    0xe09(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
lea    0xdd5(%rip),%rsi
call   1040 <KMP_match@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11e0 <main+0x70>
lea    0xdea(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"abtetetedtedddc\00"
@.str.1 = [4 x i8] c"ted\00"
@.str.2 = [13 x i8] c"offset = %d\0A\00"
@.str.3 = [15 x i8] c"copy_num = %d\0A\00"
@.str.4 = [8 x i8] c"s = %s\0A\00"
@.str.5 = [18 x i8] c"final str is :%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %3
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %5
%13 = load i32, i32* %4
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = load i8*, i8** %2
%21 = call i32 @strcpy(i8* %19, i8* %20)
store i32 0, i32* %7
br label %22
22: ; preds = %0, %29
%23 = load i8*, i8** %6
%24 = load i8*, i8** %3
%25 = call i32 @KMP_match(i8* %23, i8* %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %60
29: ; preds = %22
%30 = load i32, i32* %4
%31 = load i32, i32* %7
%32 = sub nsw i32 %30, %31
%33 = load i32, i32* %5
%34 = sub nsw i32 %32, %33
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i32, i32* %8
%51 = call i32 @strncpy(i8* %42, i8* %49, i32 %50)
%52 = load i8*, i8** %6
%53 = load i32, i32* %4
%54 = load i32, i32* %5
%55 = sub nsw i32 %53, %54
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %52, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %6
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %22
60: ; preds = %28
%61 = load i8*, i8** %6
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %61)
%63 = load i8*, i8** %6
%64 = call i32 @free(i8* %63)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"abtetetedtedddc\00"
@.str.1 = [4 x i8] c"ted\00"
@.str.2 = [13 x i8] c"offset = %d\0A\00"
@.str.3 = [15 x i8] c"copy_num = %d\0A\00"
@.str.4 = [8 x i8] c"s = %s\0A\00"
@.str.5 = [18 x i8] c"final str is :%s\0A\00"
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i64 @malloc(i32 %1)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @KMP_match(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp slt i32 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %0
%9 = sext i32 %2 to i64
%10 = sub nsw i32 %1, %2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %4, i64 %11
br label %13
13: ; preds = %8, %13
%14 = phi i32 [ %6, %8 ], [ %24, %13 ]
%15 = add i32 %2, %14
%16 = sub i32 %1, %15
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%19 = zext i32 %14 to i64
%20 = getelementptr inbounds i8, i8* %4, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 %9
%22 = tail call i32 @strncpy(i8* %20, i8* %21, i32 %16)
store i8 0, i8* %12
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%24 = tail call i32 @KMP_match(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %13
26: ; preds = %13, %0
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%28 = tail call i32 @free(i8* %4)
ret i32 0
}