#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 */
char *malloc(size_t);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *_strdup(const char *s) {
  size_t l = 0;
  while (l < 8192 && s[l]) {
    l++;
  }
  char *r = malloc(l + 1);
  strncpy(r, s, l);
  r[l] = '\0';
  return r;
}


x86 assembly (clang, -O0):
<_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x2000,-0x10(%rbp)
mov    %al,-0x19(%rbp)
jae    115c <_strdup+0x3c>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116c <_strdup+0x4c>
jmp    117d <_strdup+0x5d>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1134 <_strdup+0x14>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_strdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x3,%ebx
nopl   (%rax)
cmpb   $0x0,-0x3(%r14,%rbx,1)
je     1163 <_strdup+0x43>
cmpb   $0x0,-0x2(%r14,%rbx,1)
je     1169 <_strdup+0x49>
cmpb   $0x0,-0x1(%r14,%rbx,1)
je     116f <_strdup+0x4f>
cmpb   $0x0,(%r14,%rbx,1)
je     1173 <_strdup+0x53>
add    $0x4,%rbx
cmp    $0x2003,%rbx
jne    1130 <_strdup+0x10>
mov    $0x2000,%ebx
jmp    1173 <_strdup+0x53>
add    $0xfffffffffffffffd,%rbx
jmp    1173 <_strdup+0x53>
add    $0xfffffffffffffffe,%rbx
jmp    1173 <_strdup+0x53>
add    $0xffffffffffffffff,%rbx
lea    0x1(%rbx),%rdi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* %3
br label %5
5: ; preds = %17, %1
%6 = load i64, i64* %3
%7 = icmp ult i64 %6, 8192
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i8*, i8** %2
%10 = load i64, i64* %3
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br label %15
15: ; preds = %8, %5
%16 = phi i1 [ false, %5 ], [ %14, %8 ]
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i64, i64* %3
%19 = add i64 %18, 1
store i64 %19, i64* %3
br label %5
20: ; preds = %15
%21 = load i64, i64* %3
%22 = add i64 %21, 1
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i8*, i8** %2
%26 = load i64, i64* %3
%27 = call i32 @strncpy(i8* %24, i8* %25, i64 %26)
%28 = load i8*, i8** %4
%29 = load i64, i64* %3
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
define i8* @_strdup(i8* %0) {
br label %2
2: ; preds = %22, %1
%3 = phi i64 [ 0, %1 ], [ %23, %22 ]
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = or i64 %3, 1
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %7
%13 = or i64 %3, 2
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %12
%18 = or i64 %3, 3
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = add nuw nsw i64 %3, 4
%24 = icmp eq i64 %23, 8192
br i1 %24, label %25, label %2
25: ; preds = %22, %17, %12, %7, %2
%26 = phi i64 [ %3, %2 ], [ %8, %7 ], [ %13, %12 ], [ %18, %17 ], [ 8192, %22 ]
%27 = add nuw i64 %26, 1
%28 = tail call i8* @malloc(i64 %27)
%29 = tail call i32 @strncpy(i8* %28, i8* %0, i64 %26)
%30 = getelementptr inbounds i8, i8* %28, i64 %26
store i8 0, i8* %30
ret i8* %28
}