#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 */
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
size_t len(char const *);
char *malloc(size_t);
char *my_strdup(const char *src) {
  char *buffer;
  ssize_t size;
  ssize_t i;
  i = 0;
  size = len(src);
  if ((buffer = malloc(size + 1)) == NULL)
    exit(84);
  while (src[i] != '\0') {
    buffer[i] = src[i];
    i++;
  }
  buffer[i] = '\0';
  return (buffer);
}


x86 assembly (clang, -O0):
<my_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <len@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1176 <my_strdup+0x46>
mov    $0x54,%edi
call   1050 <exit@plt>
jmp    117b <my_strdup+0x4b>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11b7 <my_strdup+0x87>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    117b <my_strdup+0x4b>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strdup>:
push   %rbx
mov    %rdi,%rbx
call   1030 <len@plt>
lea    0x1(%rax),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1166 <my_strdup+0x36>
mov    (%rbx),%dl
xor    %ecx,%ecx
test   %dl,%dl
je     1160 <my_strdup+0x30>
nop
mov    %dl,(%rax,%rcx,1)
movzbl 0x1(%rbx,%rcx,1),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    1150 <my_strdup+0x20>
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
ret
mov    $0x54,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
define noalias i8* @my_strdup(i8* %0) {
%2 = tail call i64 @len(i8* %0)
%3 = add i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %19, label %11
9: ; preds = %1
%10 = tail call i32 @exit(i32 84)
unreachable
11: ; preds = %6, %11
%12 = phi i8 [ %17, %11 ], [ %7, %6 ]
%13 = phi i64 [ %15, %11 ], [ 0, %6 ]
%14 = getelementptr inbounds i8, i8* %4, i64 %13
store i8 %12, i8* %14
%15 = add i64 %13, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %11
19: ; preds = %11, %6
%20 = phi i64 [ 0, %6 ], [ %15, %11 ]
%21 = getelementptr inbounds i8, i8* %4, i64 %20
store i8 0, i8* %21
ret i8* %4
}