#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 *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ private_copy_check(char *);
int /*<<< orphan*/ private_copy_init();
int strlen(char *);
void copy_string() {
  char string[] = "This is a string that I will copy.";
  char *copy;
  int i;
  private_copy_init();
  copy = malloc(strlen(string) + 1);
  for (i = 0; i < strlen(string); i++) {
    copy[i] = string[i];
  }
  printf("Copied string: %s\n", copy);
  private_copy_check(copy);
  free(copy);
}


x86 assembly (clang, -O0):
<copy_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x30(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x23,%edx
call   1080 <memcpy@plt>
mov    $0x0,%al
call   1030 <private_copy_init@plt>
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
cmp    %ecx,%eax
jge    11ef <copy_string+0x7f>
movslq -0x3c(%rbp),%rax
mov    -0x30(%rbp,%rax,1),%dl
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11b2 <copy_string+0x42>
mov    -0x38(%rbp),%rsi
lea    0xe29(%rip),%rdi
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <private_copy_check@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
movaps 0xea0(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe84(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x2e7970,0x1f(%rsp)
xor    %eax,%eax
call   1030 <private_copy_init@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11db <copy_string+0x7b>
xor    %ebx,%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
movzbl (%rsp,%rbx,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11c0 <copy_string+0x60>
lea    0xe41(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <private_copy_check@plt>
mov    %r15,%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.copy_string.string = [35 x i8] c"This is a string that I will copy.\00"
@.str = [19 x i8] c"Copied string: %s\0A\00"
define void @copy_string() {
%1 = alloca [35 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = bitcast [35 x i8]* %1 to i8*
%5 = call i32 (...) @private_copy_init()
%6 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 0
%7 = call i32 @strlen(i8* %6)
%8 = add nsw i32 %7, 1
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %2
store i32 0, i32* %3
br label %10
10: ; preds = %24, %0
%11 = load i32, i32* %3
%12 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %27
15: ; preds = %10
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = load i8*, i8** %2
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 %19, i8* %23
br label %24
24: ; preds = %15
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %10
27: ; preds = %10
%28 = load i8*, i8** %2
%29 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %2
%31 = call i32 @private_copy_check(i8* %30)
%32 = load i8*, i8** %2
%33 = call i32 @free(i8* %32)
ret void
}

LLVM IR (-O2):
@__const.copy_string.string = [35 x i8] c"This is a string that I will copy.\00"
@.str = [19 x i8] c"Copied string: %s\0A\00"
define void @copy_string() {
%1 = alloca [35 x i8]
%2 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @private_copy_init()
%4 = call i32 @strlen(i8* %2)
%5 = add nsw i32 %4, 1
%6 = call i8* @malloc(i32 %5)
%7 = call i32 @strlen(i8* %2)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %0, %9
%10 = phi i64 [ %14, %9 ], [ 0, %0 ]
%11 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 %12, i8* %13
%14 = add nuw nsw i64 %10, 1
%15 = call i32 @strlen(i8* %2)
%16 = sext i32 %15 to i64
%17 = icmp slt i64 %14, %16
br i1 %17, label %9, label %18
18: ; preds = %9, %0
%19 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %6)
%20 = call i32 @private_copy_check(i8* %6)
%21 = call i32 @free(i8* %6)
ret void
}