#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 */
scalar_t__ e_malloc(int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *find_ship_name(char *file) {
  int i;
  char *t, *name;
  char *s = strstr(file, "<A NAME=");
  s += 8;
  if (*s == '"') {
    s++;
  }
  t = strchr(s, '>');
  t--;
  if (*t != '"') {
    t++;
  }
  i = t - s;
  name = (char *)e_malloc(i + 1);
  strncpy(name, s, i);
  name[i] = '\0';
  return name;
}


x86 assembly (clang, -O0):
<find_ship_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1188 <find_ship_name+0x48>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3e,%esi
call   1050 <strchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
je     11c2 <find_ship_name+0x82>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1040 <e_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_ship_name>:
push   %r15
push   %r14
push   %rbx
lea    0xeb4(%rip),%rsi
call   1060 <strstr@plt>
lea    0x8(%rax),%r14
lea    0x9(%rax),%rcx
cmpb   $0x22,0x8(%rax)
cmove  %rcx,%r14
mov    %r14,%rdi
mov    $0x3e,%esi
call   1050 <strchr@plt>
lea    -0x1(%rax),%rbx
cmpb   $0x22,-0x1(%rax)
cmovne %rax,%rbx
sub    %r14,%rbx
lea    0x1(%rbx),%edi
call   1040 <e_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"<A NAME=\00"
define i8* @find_ship_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i8* @strstr(i8* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = getelementptr inbounds i8, i8* %9, i64 8
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 34
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %6
br label %18
18: ; preds = %15, %1
%19 = load i8*, i8** %6
%20 = call i8* @strchr(i8* %19, i8 signext 62)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i32 -1
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 34
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %4
br label %30
30: ; preds = %27, %18
%31 = load i8*, i8** %4
%32 = load i8*, i8** %6
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = trunc i64 %35 to i32
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
%39 = call i64 @e_malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %5
%41 = load i8*, i8** %5
%42 = load i8*, i8** %6
%43 = load i32, i32* %3
%44 = call i32 @strncpy(i8* %41, i8* %42, i32 %43)
%45 = load i8*, i8** %5
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
@.str = [9 x i8] c"<A NAME=\00"
define i8* @find_ship_name(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds i8, i8* %2, i64 8
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 34
%6 = getelementptr inbounds i8, i8* %2, i64 9
%7 = select i1 %5, i8* %6, i8* %3
%8 = tail call i8* @strchr(i8* %7, i8 signext 62)
%9 = getelementptr inbounds i8, i8* %8, i64 -1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 34
%12 = select i1 %11, i8* %9, i8* %8
%13 = ptrtoint i8* %12 to i64
%14 = ptrtoint i8* %7 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
%17 = add nsw i32 %16, 1
%18 = tail call i64 @e_malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @strncpy(i8* %19, i8* %7, i32 %16)
%21 = shl i64 %15, 32
%22 = ashr exact i64 %21, 32
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
ret i8* %19
}