#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 */
scalar_t__ mem_malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char *);
char *mem_strdup(char *str) {
  register char *new;
  register int nchr;
  if (str == 0)
    return 0;
  nchr = strlen(str) + 1;
  new = (char *)mem_malloc(sizeof(char) * nchr);
  if (new != 0)
    memcpy(new, str, nchr);
  return new;
}


x86 assembly (clang, -O0):
<mem_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <mem_strdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1199 <mem_strdup+0x69>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <mem_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1191 <mem_strdup+0x61>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

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

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

LLVM IR (-O2):
define i8* @mem_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 @mem_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 = %3, %9, %1
%12 = phi i8* [ null, %1 ], [ %7, %9 ], [ %7, %3 ]
ret i8* %12
}