#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(unsigned int);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
unsigned int strlen(char const *);
char *strdup(const char *string) {
  char *palaute;
  unsigned int length;
  if (!string)
    return NULL;
  length = strlen(string);
  palaute = malloc(length + 1);
  if (palaute) {
    memcpy(palaute, string, length);
    palaute[length] = '\0';
  } else
    errno = ENOMEM;
  return palaute;
}


x86 assembly (clang, -O0):
<strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <strdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11b4 <strdup+0x84>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119a <strdup+0x6a>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    11ac <strdup+0x7c>
mov    0x2e3f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     117f <strdup+0x4f>
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     116d <strdup+0x3d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    %r15d,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    1181 <strdup+0x51>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    0x2e5b(%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* @strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* null, i8** %2
br label %30
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = add i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %26
17: ; preds = %9
%18 = load i8*, i8** %4
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = call i32 @memcpy(i8* %18, i8* %19, i32 %20)
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
br label %28
26: ; preds = %9
%27 = load i32, i32* @ENOMEM
store i32 %27, i32* @errno
br label %28
28: ; preds = %26, %17
%29 = load i8*, i8** %4
store i8* %29, i8** %2
br label %30
30: ; preds = %28, %8
%31 = load i8*, i8** %2
ret i8* %31
}

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