#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(char *, int);
int strlen(char *);
void copy_string() {
  char string[] = "This is a string that I will copy.";
  char *copy;
  int i;
  int n = strlen(string) + 1;
  copy = malloc(n);
  private_copy_init(copy, n);
  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    $0x50,%rsp
lea    -0x30(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x23,%edx
call   1080 <memcpy@plt>
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%esi
call   1030 <private_copy_init@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jge    11f8 <copy_string+0x88>
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    11bb <copy_string+0x4b>
mov    -0x38(%rbp),%rsi
lea    0xe20(%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    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_string>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movaps 0xea1(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe85(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x2e7970,0x1f(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <private_copy_init@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11da <copy_string+0x7a>
xor    %ebp,%ebp
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
movzbl (%rsp,%rbp,1),%eax
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     11c0 <copy_string+0x60>
lea    0xe42(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <private_copy_check@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
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 = alloca i32
%5 = bitcast [35 x i8]* %1 to i8*
%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
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = load i32, i32* %4
%13 = call i32 @private_copy_init(i8* %11, i32 %12)
store i32 0, i32* %3
br label %14
14: ; preds = %28, %0
%15 = load i32, i32* %3
%16 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %31
19: ; preds = %14
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = load i8*, i8** %2
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
br label %28
28: ; preds = %19
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %14
31: ; preds = %14
%32 = load i8*, i8** %2
%33 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %2
%35 = call i32 @private_copy_check(i8* %34)
%36 = load i8*, i8** %2
%37 = call i32 @free(i8* %36)
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 = call i32 @strlen(i8* %2)
%4 = add nsw i32 %3, 1
%5 = call i8* @malloc(i32 %4)
%6 = call i32 @private_copy_init(i8* %5, i32 %4)
%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* %5, 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* %5)
%20 = call i32 @private_copy_check(i8* %5)
%21 = call i32 @free(i8* %5)
ret void
}