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


x86 assembly (clang, -O0):
<fetch_update_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a7 <fetch_update_str+0x37>
mov    $0x0,%al
call   1070 <fetch_str_dup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1248 <fetch_update_str+0xd8>
mov    $0x0,%al
call   1030 <prefetch_strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <fetch_str@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11f2 <fetch_update_str+0x82>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1241 <fetch_update_str+0xd1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <tfree_str@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1080 <talloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
jmp    1248 <fetch_update_str+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetch_update_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpq   $0x0,(%rdi)
je     11da <fetch_update_str+0x6a>
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <prefetch_strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <fetch_str@plt>
mov    %rax,%r14
mov    (%rbx),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <memcmp@plt>
mov    (%rbx),%rdi
movslq %ebp,%rbp
test   %rax,%rax
jne    11b4 <fetch_update_str+0x44>
cmpb   $0x0,(%rdi,%rbp,1)
je     11ea <fetch_update_str+0x7a>
call   1090 <tfree_str@plt>
lea    0x1(%rbp),%edi
call   1080 <talloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    (%rbx),%rax
movb   $0x0,(%rax,%rbp,1)
jmp    11e4 <fetch_update_str+0x74>
xor    %eax,%eax
call   1070 <fetch_str_dup@plt>
mov    %rax,(%rbx)
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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