#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 /*<<< orphan*/ assert(int /*<<< orphan*/);
char *heap_alloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
const char *string_dup(const char *val) {
  int len = strlen(val) + 2;
  assert(len < 127 || "don't support long string yet");
  char *d = heap_alloc(len);
  d[0] = len;
  memcpy(d + 1, val, len + 1);
  return d + 1;
}


x86 assembly (clang, -O0):
<string_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    $0x1,%al
cmpl   $0x7f,-0xc(%rbp)
mov    %al,-0x19(%rbp)
jl     1174 <string_dup+0x34>
mov    $0x1,%al
mov    %al,-0x19(%rbp)
jmp    1174 <string_dup+0x34>
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%edi
call   1060 <heap_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_dup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    0x2(%rbx),%r15d
mov    $0x1,%edi
call   1040 <assert@plt>
mov    %r15d,%edi
call   1060 <heap_alloc@plt>
mov    %rax,%rbp
mov    %r15b,(%rax)
add    $0x1,%rbp
add    $0x3,%ebx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"don't support long string yet\00"
define i8* @string_dup(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = add nsw i32 %6, 2
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 127
br i1 %9, label %11, label %10
10: ; preds = %1
br label %11
11: ; preds = %10, %1
%12 = phi i1 [ true, %1 ], [ true, %10 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %3
%16 = call i8* @heap_alloc(i32 %15)
store i8* %16, i8** %4
%17 = load i32, i32* %3
%18 = trunc i32 %17 to i8
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 %18, i8* %20
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8*, i8** %2
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
%26 = call i32 @memcpy(i8* %22, i8* %23, i32 %25)
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 1
ret i8* %28
}

LLVM IR (-O2):
define i8* @string_dup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 2
%4 = tail call i32 @assert(i32 1)
%5 = tail call i8* @heap_alloc(i32 %3)
%6 = trunc i32 %3 to i8
store i8 %6, i8* %5
%7 = getelementptr inbounds i8, i8* %5, i64 1
%8 = add nsw i32 %2, 3
%9 = tail call i32 @memcpy(i8* %7, i8* %0, i32 %8)
ret i8* %7
}