#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*/ ENOMEM;
int /*<<< orphan*/ errno;
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlenb(char const *);
char *strdupb(const char *str) {
  size_t len = strlenb(str);
  char *buf = (len) ? malloc(len + 1) : NULL;
  if (buf) {
    memcpy(buf, str, len);
    buf[len] = '\0';
  } else {
    errno = ENOMEM;
  }
  return buf;
}


x86 assembly (clang, -O0):
<strdupb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlenb@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116a <strdupb+0x3a>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1175 <strdupb+0x45>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    1175 <strdupb+0x45>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11aa <strdupb+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11bc <strdupb+0x8c>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdupb>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1050 <strlenb@plt>
test   %rax,%rax
je     116b <strdupb+0x3b>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     116b <strdupb+0x3b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    117f <strdupb+0x4f>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    0x2e5d(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @strdupb(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlenb(i8* %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i64, i64* %3
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
br label %14
13: ; preds = %1
br label %14
14: ; preds = %13, %9
%15 = phi i8* [ %12, %9 ], [ null, %13 ]
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i8*, i8** %2
%21 = load i64, i64* %3
%22 = call i32 @memcpy(i8* %19, i8* %20, i64 %21)
%23 = load i8*, i8** %4
%24 = load i64, i64* %3
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
br label %28
26: ; preds = %14
%27 = load i32, i32* @ENOMEM
store i32 %27, i32* @errno
br label %28
28: ; preds = %26, %18
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @strdupb(i8* %0) {
%2 = tail call i64 @strlenb(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = add i64 %2, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i32 @memcpy(i8* %6, i8* %0, i64 %2)
%10 = getelementptr inbounds i8, i8* %6, i64 %2
store i8 0, i8* %10
br label %13
11: ; preds = %1, %4
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %13
13: ; preds = %11, %8
%14 = phi i8* [ null, %11 ], [ %6, %8 ]
ret i8* %14
}