#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 */
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *duplicate(const char *str) {
  char *result = NULL;
  if (str != NULL) {
    result = (char *)malloc(strlen(str) + 1);
    if (result == NULL) {
      perror(NULL);
    } else {
      strcpy(result, str);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<duplicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a3 <duplicate+0x63>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <duplicate+0x51>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <perror@plt>
jmp    119e <duplicate+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11a3 <duplicate+0x63>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<duplicate>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     116f <duplicate+0x2f>
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     117d <duplicate+0x3d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1172 <duplicate+0x32>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %r14d,%r14d
xor    %edi,%edi
call   1060 <perror@plt>
jmp    1172 <duplicate+0x32>

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

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