#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 */
char *dumb_malloc(size_t);
int /*<<< orphan*/ ehnlc_strcpy(char *, char const *);
size_t ehnlc_strlen(char const *);
char *ehnlc_strdup(const char *s) {
  size_t len;
  char *dup;
  len = s ? ehnlc_strlen(s) : 0;
  dup = dumb_malloc(len + 1);
  if (!dup) {
    return NULL;
  }
  dup[len] = '\0';
  ehnlc_strcpy(dup, s);
  return dup;
}


x86 assembly (clang, -O0):
<ehnlc_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1159 <ehnlc_strdup+0x29>
mov    -0x10(%rbp),%rdi
call   1030 <ehnlc_strlen@plt>
mov    %rax,-0x28(%rbp)
jmp    1164 <ehnlc_strdup+0x34>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    1164 <ehnlc_strdup+0x34>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <dumb_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1195 <ehnlc_strdup+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11b6 <ehnlc_strdup+0x86>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <ehnlc_strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ehnlc_strdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     114a <ehnlc_strdup+0x1a>
mov    %r14,%rdi
call   1030 <ehnlc_strlen@plt>
mov    %rax,%r15
jmp    114d <ehnlc_strdup+0x1d>
xor    %r15d,%r15d
lea    0x1(%r15),%rdi
call   1040 <dumb_malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116e <ehnlc_strdup+0x3e>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <ehnlc_strcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @ehnlc_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %5, label %3
3: ; preds = %1
%4 = tail call i64 @ehnlc_strlen(i8* %0)
br label %5
5: ; preds = %1, %3
%6 = phi i64 [ %4, %3 ], [ 0, %1 ]
%7 = add i64 %6, 1
%8 = tail call i8* @dumb_malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %8, i64 %6
store i8 0, i8* %11
%12 = tail call i32 @ehnlc_strcpy(i8* %8, i8* %0)
br label %13
13: ; preds = %5, %10
ret i8* %8
}