#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*/ memcpy(char *, char *, int);
int strlen(char *);
char *main_fatal_error_copy_string(char *from, char *to) {
  int from_len = 0;
  if (to != NULL) {
    free(to);
  }
  from_len = strlen(from);
  if (from_len <= 0)
    return NULL;
  to = malloc(from_len + 1);
  memcpy(to, from, from_len);
  to[from_len] = '\0';
  return to;
}


x86 assembly (clang, -O0):
<main_fatal_error_copy_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116b <main_fatal_error_copy_string+0x2b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     118e <main_fatal_error_copy_string+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <main_fatal_error_copy_string+0x81>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main_fatal_error_copy_string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1155 <main_fatal_error_copy_string+0x15>
mov    %rsi,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1184 <main_fatal_error_copy_string+0x44>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
jmp    1187 <main_fatal_error_copy_string+0x47>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @main_fatal_error_copy_string(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = call i32 @free(i8* %10)
br label %12
12: ; preds = %9, %2
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i8* null, i8** %3
br label %31
18: ; preds = %12
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = call i32 @memcpy(i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %5
store i8* %30, i8** %3
br label %31
31: ; preds = %18, %17
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
define i8* @main_fatal_error_copy_string(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @free(i8* %1)
br label %6
6: ; preds = %4, %2
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp slt i32 %7, 1
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = add nuw nsw i32 %7, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %7)
%13 = zext i32 %7 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %6, %9
%16 = phi i8* [ %11, %9 ], [ null, %6 ]
ret i8* %16
}