#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 */
int hjstrlen(char const *);
scalar_t__ malloc(int);
char *hjstrdup(const char *src) {
  if (src == NULL) {
    return NULL;
  }
  char *pstr = (char *)malloc(sizeof(char) * hjstrlen(src) + 1);
  char *presult = pstr;
  while ((*pstr++ = *src++) != '\0')
    ;
  *pstr = '\0';
  return presult;
}


x86 assembly (clang, -O0):
<hjstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1144 <hjstrdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11ac <hjstrdup+0x8c>
mov    -0x10(%rbp),%rdi
call   1030 <hjstrlen@plt>
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
je     119d <hjstrdup+0x7d>
jmp    116a <hjstrdup+0x4a>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hjstrdup>:
test   %rdi,%rdi
je     1155 <hjstrdup+0x35>
push   %rbx
mov    %rdi,%rbx
call   1030 <hjstrlen@plt>
lea    0x1(%rax),%edi
call   1040 <malloc@plt>
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
movzbl (%rbx,%rcx,1),%edx
mov    %dl,(%rax,%rcx,1)
add    $0x1,%rcx
test   %dl,%dl
jne    1140 <hjstrdup+0x20>
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
ret
xor    %eax,%eax
ret

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

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