#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*/ memcpy(char *, char const *, int);
scalar_t__ mhs_malloc(int);
int strlen(char const *);
char *mhs_strdup(const char *str) {
  int len;
  char *new_str;
  if (str) {
    len = strlen(str) + 1;
    new_str = (char *)mhs_malloc(len);
    if (!new_str) {
      return (NULL);
    }
    memcpy(new_str, str, len);
    return (new_str);
  } else {
    return (NULL);
  }
}


x86 assembly (clang, -O0):
<mhs_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1197 <mhs_strdup+0x67>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <mhs_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117a <mhs_strdup+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    119f <mhs_strdup+0x6f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <mhs_strdup+0x6f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mhs_strdup>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1164 <mhs_strdup+0x34>
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <mhs_malloc@plt>
test   %rax,%rax
je     1164 <mhs_strdup+0x34>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
jmp    1166 <mhs_strdup+0x36>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @mhs_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%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 %24
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i64 @mhs_malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %8
store i8* null, i8** %2
br label %25
18: ; preds = %8
%19 = load i8*, i8** %5
%20 = load i8*, i8** %3
%21 = load i32, i32* %4
%22 = call i32 @memcpy(i8* %19, i8* %20, i32 %21)
%23 = load i8*, i8** %5
store i8* %23, i8** %2
br label %25
24: ; preds = %1
store i8* null, i8** %2
br label %25
25: ; preds = %24, %18, %17
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
define i8* @mhs_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %11, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i64 @mhs_malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @memcpy(i8* %7, i8* %0, i32 %5)
br label %11
11: ; preds = %1, %3, %9
%12 = phi i8* [ %7, %9 ], [ null, %3 ], [ null, %1 ]
ret i8* %12
}