#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);
char *fetch_str(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int prefetch_strlen();
char *talloc(int);
char *fetch_str_dup(void) {
  int l = prefetch_strlen();
  assert(l >= 0);
  int i;
  char *s = fetch_str(l);
  for (i = 0; i < l; i++) {
    if (!s[i]) {
      break;
    }
  }
  char *r = talloc(i + 1);
  memcpy(r, s, i);
  r[i] = 0;
  return r;
}


x86 assembly (clang, -O0):
<fetch_str_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <prefetch_strlen@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x4(%rbp),%edi
call   1040 <fetch_str@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11bc <fetch_str_dup+0x6c>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
jne    11a9 <fetch_str_dup+0x59>
jmp    11bc <fetch_str_dup+0x6c>
jmp    11ae <fetch_str_dup+0x5e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1186 <fetch_str_dup+0x36>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1070 <talloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetch_str_dup>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <prefetch_strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
mov    %ebp,%edi
call   1040 <fetch_str@plt>
mov    %rax,%r14
test   %ebp,%ebp
jle    1192 <fetch_str_dup+0x42>
mov    %ebp,%eax
xor    %ebx,%ebx
nopl   (%rax)
cmpb   $0x0,(%r14,%rbx,1)
je     1192 <fetch_str_dup+0x42>
add    $0x1,%rbx
cmp    %rbx,%rax
jne    1180 <fetch_str_dup+0x30>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1070 <talloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @fetch_str_dup() {
%1 = tail call i32 (...) @prefetch_strlen()
%2 = xor i32 %1, -1
%3 = lshr i32 %2, 31
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i8* @fetch_str(i32 %1)
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %19
7: ; preds = %0
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %7, %14
%10 = phi i64 [ 0, %7 ], [ %15, %14 ]
%11 = getelementptr inbounds i8, i8* %5, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = add nuw nsw i64 %10, 1
%16 = icmp eq i64 %15, %8
br i1 %16, label %19, label %9
17: ; preds = %9
%18 = trunc i64 %10 to i32
br label %19
19: ; preds = %14, %17, %0
%20 = phi i32 [ 0, %0 ], [ %18, %17 ], [ %1, %14 ]
%21 = add nuw nsw i32 %20, 1
%22 = tail call i8* @talloc(i32 %21)
%23 = tail call i32 @memcpy(i8* %22, i8* %5, i32 %20)
%24 = zext i32 %20 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
ret i8* %22
}