#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 */
size_t lmc_valloc(void *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t lmc_ht_strdup(void *base, const char *s, size_t l) {
  size_t va_s = lmc_valloc(base, l + sizeof(size_t) + 1);
  if (!va_s) {
    return 0;
  }
  char *p = base + va_s;
  *(size_t *)p = l;
  p += sizeof(size_t);
  memcpy(p, s, l + 1);
  return va_s;
}


x86 assembly (clang, -O0):
<lmc_ht_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x8,%rsi
add    $0x1,%rsi
call   1030 <lmc_valloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1165 <lmc_ht_strdup+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11a5 <lmc_ht_strdup+0x85>
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lmc_ht_strdup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x9(%rdx),%rsi
call   1030 <lmc_valloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     115d <lmc_ht_strdup+0x3d>
mov    %r15,(%r12,%rbx,1)
lea    (%r12,%rbx,1),%rdi
add    $0x8,%rdi
add    $0x1,%r15
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @lmc_ht_strdup(i8* %0, i8* %1, i64 %2) {
%4 = add i64 %2, 9
%5 = tail call i64 @lmc_valloc(i8* %0, i64 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = getelementptr i8, i8* %0, i64 %5
%9 = bitcast i8* %8 to i64*
store i64 %2, i64* %9
%10 = getelementptr inbounds i8, i8* %8, i64 8
%11 = add i64 %2, 1
%12 = tail call i32 @memcpy(i8* %10, i8* %1, i64 %11)
br label %13
13: ; preds = %3, %7
ret i64 %5
}