#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 */
unsigned char *malloc(int);
int my_strlen(char *);
unsigned char *my_strdup(char *src) {
  int len;
  int lp;
  unsigned char *dest;
  len = my_strlen(src);
  lp = 0;
  dest = malloc(sizeof(unsigned char) * len + 1);
  if (dest == NULL || src == NULL)
    return (NULL);
  while (lp < len) {
    dest[lp] = src[lp];
    ++lp;
  }
  dest[lp] = 255;
  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)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     116c <my_strdup+0x4c>
cmpq   $0x0,-0x10(%rbp)
jne    1179 <my_strdup+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <my_strdup+0xa2>
jmp    117e <my_strdup+0x5e>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ae <my_strdup+0x8e>
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    117e <my_strdup+0x5e>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0xff,(%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,%r15
call   1030 <my_strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %eax,%eax
test   %r15,%r15
je     1176 <my_strdup+0x46>
test   %r14,%r14
je     1176 <my_strdup+0x46>
test   %ebx,%ebx
jle    116c <my_strdup+0x3c>
mov    %ebx,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    116e <my_strdup+0x3e>
xor    %ebx,%ebx
movb   $0xff,(%r14,%rbx,1)
mov    %r14,%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 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = add i64 %11, 1
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17, %1
store i8* null, i8** %2
br label %44
21: ; preds = %17
br label %22
22: ; preds = %26, %21
%23 = load i32, i32* %5
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%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 %31, i8* %35
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %22
38: ; preds = %22
%39 = load i8*, i8** %6
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 -1, i8* %42
%43 = load i8*, i8** %6
store i8* %43, i8** %2
br label %44
44: ; preds = %38, %20
%45 = load i8*, i8** %2
ret i8* %45
}

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