#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 const *);
char *my_strdup(const char *str) {
  char *dup;
  int i;
  int size;
  i = -1;
  size = my_strlen(str);
  if (!(dup = malloc(size + 2)))
    return (NULL);
  while (i++ < size)
    dup[i] = str[i];
  dup[i] = '\0';
  return (dup);
}


x86 assembly (clang, -O0):
<my_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1165 <my_strdup+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11ad <my_strdup+0x8d>
jmp    116a <my_strdup+0x4a>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    -0x20(%rbp),%eax
jge    1199 <my_strdup+0x79>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    116a <my_strdup+0x4a>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%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,%ebx
lea    0x2(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1173 <my_strdup+0x43>
test   %ebx,%ebx
js     116c <my_strdup+0x3c>
mov    %ebx,%ebx
add    $0x1,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movslq %ebx,%rax
jmp    116e <my_strdup+0x3e>
xor    %eax,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%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 i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
%7 = load i8*, i8** %3
%8 = call i32 @my_strlen(i8* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = add nsw i32 %9, 2
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* null, i8** %2
br label %36
14: ; preds = %1
br label %15
15: ; preds = %20, %14
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
store i32 %17, i32* %5
%18 = load i32, i32* %6
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %30
20: ; preds = %15
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
br label %15
30: ; preds = %15
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %4
store i8* %35, i8** %2
br label %36
36: ; preds = %30, %13
%37 = load i8*, i8** %2
ret i8* %37
}

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