#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 *_Memnew(size_t);
size_t strlen(char const *);
char *string_dup(const char *s) {
  char *p;
  char *q;
  size_t len = strlen(s);
  p = _Memnew((len + 1));
  if (p == NULL)
    return NULL;
  q = p;
  while (*s)
    *p++ = *s++;
  *p = '\0';
  return q;
}


x86 assembly (clang, -O0):
<string_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1040 <_Memnew@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1162 <string_dup+0x42>
movq   $0x0,-0x8(%rbp)
jmp    11ad <string_dup+0x8d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     119e <string_dup+0x7e>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    116a <string_dup+0x4a>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_dup>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1040 <_Memnew@plt>
test   %rax,%rax
je     1164 <string_dup+0x44>
mov    (%rbx),%cl
mov    %rax,%rdx
test   %cl,%cl
je     1161 <string_dup+0x41>
add    $0x1,%rbx
mov    %rax,%rdx
nopw   0x0(%rax,%rax,1)
mov    %cl,(%rdx)
add    $0x1,%rdx
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1150 <string_dup+0x30>
movb   $0x0,(%rdx)
pop    %rbx
ret

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

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