#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);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
int /*<<< orphan*/ stderr;
unsigned int strlen(char const *);
char *mystrdup(const char *s) {
  static unsigned int used = 65520;
  static char *pool = NULL;
  unsigned int len = strlen(s);
  char *t;
  assert(len < 65520 - 1);
  if (used + len + 1 >= 65520) {
    pool = malloc(65520);
    if (pool == NULL) {
      fprintf(stderr, "Out of memory trying to alloc string pool.\n");
      exit(1);
    }
    used = 0;
  }
  t = &pool[used];
  memcpy(t, s, len + 1);
  used += len + 1;
  return t;
}


x86 assembly (clang, -O0):
<mystrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffef,-0xc(%rbp)
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2ebe(%rip),%eax
add    -0xc(%rbp),%eax
add    $0x1,%eax
cmp    $0xfff0,%eax
jb     11eb <mystrdup+0x8b>
mov    $0xfff0,%edi
call   1070 <malloc@plt>
mov    %rax,0x2eac(%rip)
cmpq   $0x0,0x2ea4(%rip)
jne    11e1 <mystrdup+0x81>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,0x2e65(%rip)
mov    0x2e6e(%rip),%rax
mov    0x2e58(%rip),%ecx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
add    0x2e32(%rip),%eax
mov    %eax,0x2e2c(%rip)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrdup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
xor    %edi,%edi
mov    %eax,%ebp
sub    $0xffef,%ebp
setb   %dil
call   1040 <assert@plt>
mov    0x2ec5(%rip),%ebx
lea    (%rbx,%rbp,1),%eax
cmp    $0xffff0010,%eax
jae    11b9 <mystrdup+0x59>
mov    $0xfff0,%edi
call   1070 <malloc@plt>
mov    %rax,0x2eba(%rip)
test   %rax,%rax
je     11ea <mystrdup+0x8a>
movl   $0x0,0x2e9b(%rip)
xor    %ebx,%ebx
jmp    11c0 <mystrdup+0x60>
mov    0x2ea0(%rip),%rax
add    %rax,%rbx
add    $0x1,%r15d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
add    %r15d,0x2e74(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@mystrdup.used = internal global i32 65520
@mystrdup.pool = internal global i8* null
@stderr = global i32 0
@.str = [44 x i8] c"Out of memory trying to alloc string pool.\0A\00"
define i8* @mystrdup(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)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp ult i32 %7, 65519
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* @mystrdup.used
%12 = load i32, i32* %3
%13 = add i32 %11, %12
%14 = add i32 %13, 1
%15 = icmp uge i32 %14, 65520
br i1 %15, label %16, label %25
16: ; preds = %1
%17 = call i8* @malloc(i32 65520)
store i8* %17, i8** @mystrdup.pool
%18 = load i8*, i8** @mystrdup.pool
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
store i32 0, i32* @mystrdup.used
br label %25
25: ; preds = %24, %1
%26 = load i8*, i8** @mystrdup.pool
%27 = load i32, i32* @mystrdup.used
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8* %29, i8** %4
%30 = load i8*, i8** %4
%31 = load i8*, i8** %2
%32 = load i32, i32* %3
%33 = add i32 %32, 1
%34 = call i32 @memcpy(i8* %30, i8* %31, i32 %33)
%35 = load i32, i32* %3
%36 = add i32 %35, 1
%37 = load i32, i32* @mystrdup.used
%38 = add i32 %37, %36
store i32 %38, i32* @mystrdup.used
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@mystrdup.used = internal unnamed_addr global i32 65520
@mystrdup.pool = internal unnamed_addr global i8* null
@stderr = global i32 0
@.str = [44 x i8] c"Out of memory trying to alloc string pool.\0A\00"
define i8* @mystrdup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp ult i32 %2, 65519
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i32, i32* @mystrdup.used
%7 = add i32 %2, -65519
%8 = add i32 %7, %6
%9 = icmp ult i32 %8, -65520
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i8*, i8** @mystrdup.pool
%12 = zext i32 %6 to i64
br label %21
13: ; preds = %1
%14 = tail call dereferenceable_or_null(65520) i8* @malloc(i32 65520)
store i8* %14, i8** @mystrdup.pool
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
store i32 0, i32* @mystrdup.used
br label %21
21: ; preds = %10, %20
%22 = phi i64 [ %12, %10 ], [ 0, %20 ]
%23 = phi i8* [ %11, %10 ], [ %14, %20 ]
%24 = getelementptr inbounds i8, i8* %23, i64 %22
%25 = add i32 %2, 1
%26 = tail call i32 @memcpy(i8* %24, i8* %0, i32 %25)
%27 = load i32, i32* @mystrdup.used
%28 = add i32 %27, %25
store i32 %28, i32* @mystrdup.used
ret i8* %24
}