#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 */
char *malloc(size_t);
char *strcpy(char *, char const *);
int strlen(char const *);
char *copystring(const char *src) {
  if (!src)
    return 0;
  size_t len = strlen(src) + 1;
  char *dest = 0;
  if (len > 0)
    dest = malloc(len);
  if (!dest)
    return 0;
  return strcpy(dest, src);
}


x86 assembly (clang, -O0):
<copystring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <copystring+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11af <copystring+0x7f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1186 <copystring+0x56>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119e <copystring+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11af <copystring+0x7f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copystring>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1162 <copystring+0x32>
mov    %rdi,%rbx
call   1040 <strlen@plt>
inc    %eax
je     1162 <copystring+0x32>
movslq %eax,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1162 <copystring+0x32>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1165 <copystring+0x35>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @copystring(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = sext i32 %5 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i8* @strcpy(i8* %9, i8* %0)
br label %13
13: ; preds = %3, %11, %7, %1
%14 = phi i8* [ null, %1 ], [ %9, %11 ], [ null, %7 ], [ null, %3 ]
ret i8* %14
}