#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(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *_string_duplicate(const char *string) {
  char *copy = malloc(sizeof(*copy) * (strlen(string) + 1));
  if (copy == NULL) {
    strcpy(copy, "Not enough memory for error message");
    return (copy);
  }
  strcpy(copy, string);
  return copy;
}


x86 assembly (clang, -O0):
<_string_duplicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <_string_duplicate+0x51>
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1196 <_string_duplicate+0x66>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_string_duplicate>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1154 <_string_duplicate+0x24>
mov    %rbx,%rdi
mov    %r14,%rsi
jmp    115d <_string_duplicate+0x2d>
lea    0xea5(%rip),%rsi
xor    %edi,%edi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Not enough memory for error message\00"
define i8* @_string_duplicate(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @strlen(i8* %5)
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = mul i64 1, %8
%10 = trunc i64 %9 to i32
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %4
store i8* %17, i8** %2
br label %23
18: ; preds = %1
%19 = load i8*, i8** %4
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %4
store i8* %22, i8** %2
br label %23
23: ; preds = %18, %14
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@.str = [36 x i8] c"Not enough memory for error message\00"
define i8* @_string_duplicate(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @strcpy(i8* null, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %10
8: ; preds = %1
%9 = tail call i32 @strcpy(i8* %4, i8* %0)
br label %10
10: ; preds = %8, %6
ret i8* %4
}