#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 */
void *malloc(unsigned long);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned long);
int /*<<< orphan*/ mhandle_hash_table_insert(char *, unsigned long, char *,
                                             int);
int /*<<< orphan*/ mhandle_init();
int /*<<< orphan*/ mhandle_mark_corrupt(void *, unsigned long);
int /*<<< orphan*/ mhandle_out_of_memory(char *);
int strlen(char const *);
char *mhandle_strdup(const char *s, char *at) {
  char *ptr;
  void *eff_ptr;
  unsigned long size = strlen(s) + 1;
  unsigned long eff_size;
  mhandle_init();
  size = strlen(s) + 1;
  eff_size = size + (2 * 2);
  eff_ptr = malloc(eff_size);
  if (!eff_ptr)
    mhandle_out_of_memory(at);
  ptr = eff_ptr + 2;
  memcpy(ptr, s, size);
  mhandle_mark_corrupt(eff_ptr, eff_size);
  mhandle_hash_table_insert(ptr, size, at, 1);
  return ptr;
}


x86 assembly (clang, -O0):
<mhandle_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <mhandle_init@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <mhandle_strdup+0x68>
mov    -0x10(%rbp),%rdi
call   1050 <mhandle_out_of_memory@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <mhandle_mark_corrupt@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x1,%ecx
call   1090 <mhandle_hash_table_insert@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mhandle_strdup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
xor    %eax,%eax
call   1040 <mhandle_init@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
lea    0x1(%rbx),%r15
add    $0x5,%rbx
mov    %rbx,%rdi
call   1080 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11b8 <mhandle_strdup+0x48>
mov    %r14,%rdi
call   1050 <mhandle_out_of_memory@plt>
mov    %r13,%rbp
add    $0x2,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1070 <memcpy@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <mhandle_mark_corrupt@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1090 <mhandle_hash_table_insert@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @mhandle_strdup(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %7
%13 = call i32 (...) @mhandle_init()
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = add i64 %18, 4
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = call i32 @mhandle_out_of_memory(i8* %25)
br label %27
27: ; preds = %24, %2
%28 = load i8*, i8** %6
%29 = getelementptr i8, i8* %28, i64 2
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = load i8*, i8** %3
%32 = load i64, i64* %7
%33 = call i32 @memcpy(i8* %30, i8* %31, i64 %32)
%34 = load i8*, i8** %6
%35 = load i64, i64* %8
%36 = call i32 @mhandle_mark_corrupt(i8* %34, i64 %35)
%37 = load i8*, i8** %5
%38 = load i64, i64* %7
%39 = load i8*, i8** %4
%40 = call i32 @mhandle_hash_table_insert(i8* %37, i64 %38, i8* %39, i32 1)
%41 = load i8*, i8** %5
ret i8* %41
}

LLVM IR (-O2):
define i8* @mhandle_strdup(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 (...) @mhandle_init()
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = add nsw i64 %7, 4
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @mhandle_out_of_memory(i8* %1)
br label %13
13: ; preds = %11, %2
%14 = getelementptr i8, i8* %9, i64 2
%15 = tail call i32 @memcpy(i8* %14, i8* %0, i64 %7)
%16 = tail call i32 @mhandle_mark_corrupt(i8* %9, i64 %8)
%17 = tail call i32 @mhandle_hash_table_insert(i8* %14, i64 %7, i8* %1, i32 1)
ret i8* %14
}