#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(scalar_t__);
scalar_t__ mystrlen(char *);
char *mystrdup(char *s) {
  char *new = (char *)malloc(mystrlen(s) + 1);
  char *n = new;
  char *s1 = s;
  while (*s1 != '\0') {
    *n = *s1;
    n++;
    s1++;
  }
  *n = '\0';
  return new;
}


x86 assembly (clang, -O0):
<mystrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <mystrlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118e <mystrdup+0x6e>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1155 <mystrdup+0x35>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrdup>:
push   %rbx
mov    %rdi,%rbx
call   1040 <mystrlen@plt>
lea    0x1(%rax),%rdi
call   1030 <malloc@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     1153 <mystrdup+0x33>
add    $0x1,%rbx
mov    %rax,%rdx
nop
mov    %cl,(%rdx)
add    $0x1,%rdx
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1140 <mystrdup+0x20>
jmp    1156 <mystrdup+0x36>
mov    %rax,%rdx
movb   $0x0,(%rdx)
pop    %rbx
ret

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

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