#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 *malloc(int);
int my_strlen(char *);
char *my_strdup(char *src) {
  int len;
  int lp;
  char *dest;
  len = my_strlen(src);
  lp = 0;
  if ((dest = malloc(len + 1)) == NULL)
    return (NULL);
  while (lp < len) {
    dest[lp] = src[lp];
    ++lp;
  }
  dest[lp] = '\0';
  return (dest);
}


x86 assembly (clang, -O0):
<my_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1165 <my_strdup+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11ae <my_strdup+0x8e>
jmp    116a <my_strdup+0x4a>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119a <my_strdup+0x7a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116a <my_strdup+0x4a>
mov    -0x20(%rbp),%rax
movslq -0x18(%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
mov    %rdi,%r14
call   1030 <my_strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1171 <my_strdup+0x41>
test   %r15d,%r15d
jle    1169 <my_strdup+0x39>
mov    %r15d,%r15d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
jmp    116c <my_strdup+0x3c>
xor    %r15d,%r15d
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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