#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
char *estrdup(const char *s) {
  char *d;
  size_t n = strlen(s) + 1;
  d = malloc(n);
  if (d == NULL)
    error(EXIT_FAILURE, errno, NULL);
  memcpy(d, s, n);
  return d;
}


x86 assembly (clang, -O0):
<estrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <estrdup+0x51>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <error@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<estrdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    117c <estrdup+0x3c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <error@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
define i8* @estrdup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @EXIT_FAILURE
%9 = load i32, i32* @errno
%10 = tail call i32 @error(i32 %8, i32 %9, i32* null)
br label %11
11: ; preds = %7, %1
%12 = tail call i32 @memcpy(i8* %5, i8* %0, i64 %4)
ret i8* %5
}