#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*/ _fatal(int /*<<< orphan*/ *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ _pfatal(int /*<<< orphan*/ *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *xmalloc(size_t);
char *xstrdup(const char *s) {
  size_t len;
  if (!s)
    _pfatal(NULL, 0, "xstrdup");
  ;
  len = strlen(s);
  if (len > 0xffffff)
    _fatal(NULL, 0, "input string too long");
  ;
  char *p = xmalloc(len + 1);
  memcpy(p, s, strlen(s));
  p[len] = 0;
  return p;
}


x86 assembly (clang, -O0):
<xstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1179 <xstrdup+0x29>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe8c(%rip),%rdx
call   1070 <_pfatal@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffff,-0x10(%rbp)
jbe    11a6 <xstrdup+0x56>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe67(%rip),%rdx
call   1030 <_fatal@plt>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
jne    116d <xstrdup+0x1d>
lea    0xe9c(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1070 <_pfatal@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
cmp    $0x1000000,%rax
jb     1190 <xstrdup+0x40>
lea    0xe81(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1030 <_fatal@plt>
mov    %r15,%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"xstrdup\00"
@.str.1 = [22 x i8] c"input string too long\00"
define i8* @xstrdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = call i32 @_pfatal(i32* null, i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i8*, i8** %2
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp ugt i64 %12, 16777215
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @_fatal(i32* null, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i64, i64* %3
%18 = add i64 %17, 1
%19 = call i8* @xmalloc(i64 %18)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = load i8*, i8** %2
%22 = load i8*, i8** %2
%23 = call i64 @strlen(i8* %22)
%24 = call i32 @memcpy(i8* %20, i8* %21, i64 %23)
%25 = load i8*, i8** %4
%26 = load i64, i64* %3
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %4
ret i8* %28
}

LLVM IR (-O2):
@.str = [8 x i8] c"xstrdup\00"
@.str.1 = [22 x i8] c"input string too long\00"
define i8* @xstrdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @_pfatal(i32* null, i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %3, %1
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp ugt i64 %6, 16777215
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @_fatal(i32* null, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = add i64 %6, 1
%12 = tail call i8* @xmalloc(i64 %11)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%14 = tail call i32 @memcpy(i8* %12, i8* %0, i64 %13)
%15 = getelementptr inbounds i8, i8* %12, i64 %6
store i8 0, i8* %15
ret i8* %12
}