#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int main() {
  char *original_literal = "replaceoriginal";
  printf("address of string literal %p\n", original_literal);
  char *original = (char *)malloc(sizeof(char) * 50);
  strncpy(original, original_literal,
          (strlen(original_literal) + 1) * sizeof(char *));
  printf("original: \n(%s)\n", original);
  char substr[] = "orig";
  char newstr[] = "abcd";
  char *offset = strstr(original, substr);
  printf("offset: (%s)\n", offset);
  int size = strlen(newstr) * sizeof(char);
  strncpy(offset, newstr, size);
  printf("final string: \n(%s)\n", original);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe9b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
mov    0xe70(%rip),%eax
mov    %eax,-0x1d(%rbp)
mov    0xe6b(%rip),%al
mov    %al,-0x19(%rbp)
mov    0xe63(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xe5e(%rip),%al
mov    %al,-0x1e(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1d(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
lea    -0x22(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x22(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe1c(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb0(%rip),%rdi
lea    0xe99(%rip),%r14
mov    %r14,%rsi
call   1050 <printf@plt>
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x8(,%rax,8),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
lea    0xe91(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
movb   $0x0,0xc(%rsp)
movl   $0x64636261,0x8(%rsp)
64
lea    0xe86(%rip),%rsi
mov    %r15,%rdi
call   1070 <strstr@plt>
mov    %rax,%rbx
lea    0xe7e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    0xe63(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"replaceoriginal\00"
@.str.1 = [30 x i8] c"address of string literal %p\0A\00"
@.str.2 = [17 x i8] c"original: \0A(%s)\0A\00"
@__const.main.substr = [5 x i8] c"orig\00"
@__const.main.newstr = [5 x i8] c"abcd\00"
@.str.3 = [14 x i8] c"offset: (%s)\0A\00"
@.str.4 = [21 x i8] c"final string: \0A(%s)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [5 x i8]
%5 = alloca [5 x i8]
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = call i64 @malloc(i32 50)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = load i8*, i8** %2
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 8
%19 = trunc i64 %18 to i32
%20 = call i32 @strncpy(i8* %12, i8* %13, i32 %19)
%21 = load i8*, i8** %3
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = bitcast [5 x i8]* %4 to i8*
%24 = bitcast [5 x i8]* %5 to i8*
%25 = load i8*, i8** %3
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%27 = call i8* @strstr(i8* %25, i8* %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 1
%34 = trunc i64 %33 to i32
store i32 %34, i32* %7
%35 = load i8*, i8** %6
%36 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%37 = load i32, i32* %7
%38 = call i32 @strncpy(i8* %35, i8* %36, i32 %37)
%39 = load i8*, i8** %3
%40 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"replaceoriginal\00"
@.str.1 = [30 x i8] c"address of string literal %p\0A\00"
@.str.2 = [17 x i8] c"original: \0A(%s)\0A\00"
@__const.main.substr = [5 x i8] c"orig\00"
@__const.main.newstr = [5 x i8] c"abcd\00"
@.str.3 = [14 x i8] c"offset: (%s)\0A\00"
@.str.4 = [21 x i8] c"final string: \0A(%s)\0A\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%3 = tail call i64 @malloc(i32 50)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%6 = shl i32 %5, 3
%7 = add i32 %6, 8
%8 = tail call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%11 = tail call i8* @strstr(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @__const.main.substr, i64 0, i64 0))
%12 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = call i32 @strlen(i8* %10)
%14 = call i32 @strncpy(i8* %11, i8* %10, i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %4)
ret i32 0
}