#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__ malloc(unsigned int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *my_strdup(const char *str, int len) {
  char *s;
  if (str == NULL)
    return NULL;
  s = (char *)malloc((unsigned)(len + 1));
  if (s != NULL) {
    memcpy(s, str, len);
    s[len] = 0;
  }
  return s;
}


x86 assembly (clang, -O0):
<my_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1147 <my_strdup+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1185 <my_strdup+0x65>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117d <my_strdup+0x5d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1158 <my_strdup+0x38>
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     115a <my_strdup+0x3a>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    115a <my_strdup+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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