#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*/ die(char *, ...);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *sc_strdup(const char *str) {
  size_t len;
  char *copy;
  if (str == NULL) {
    die("cannot duplicate NULL string");
  }
  len = strlen(str);
  copy = malloc(len + 1);
  if (copy == NULL) {
    die("cannot allocate string copy (len: %zd)", len);
  }
  memcpy(copy, str, len + 1);
  return copy;
}


x86 assembly (clang, -O0):
<sc_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1165 <sc_strdup+0x25>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <sc_strdup+0x60>
mov    -0x10(%rbp),%rsi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_strdup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
jne    115e <sc_strdup+0x1e>
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1060 <die@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
lea    0x1(%rax),%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118e <sc_strdup+0x4e>
lea    0xe99(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <die@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [29 x i8] c"cannot duplicate NULL string\00"
@.str.1 = [39 x i8] c"cannot allocate string copy (len: %zd)\00"
define i8* @sc_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i8*, i8** %2
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = add i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i64, i64* %3
%19 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %18)
br label %20
20: ; preds = %17, %9
%21 = load i8*, i8** %4
%22 = load i8*, i8** %2
%23 = load i64, i64* %3
%24 = add i64 %23, 1
%25 = call i32 @memcpy(i8* %21, i8* %22, i64 %24)
%26 = load i8*, i8** %4
ret i8* %26
}

LLVM IR (-O2):
@.str = [29 x i8] c"cannot duplicate NULL string\00"
@.str.1 = [39 x i8] c"cannot allocate string copy (len: %zd)\00"
define i8* @sc_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %3, %1
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = add i64 %6, 1
%8 = tail call i8* @malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %12
12: ; preds = %10, %5
%13 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %7)
ret i8* %8
}