#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 *find_word_end_termchar(char const *, char const *, char);
char *find_word_start(char const *, char const *);
int find_completion_word_start(const char *str, int pos) {
  const char *maxend = str + pos;
  const char *start = str;
  const char *end = str;
  int index;
  for (index = 0;; index++) {
    end = find_word_end_termchar(end, maxend, '\0');
    if (end >= maxend || *end == '\0') {
      start = find_word_start(start, maxend);
      if (start >= maxend || *start == '\0')
        return pos;
      return start - str;
    }
    start = end;
  }
}


x86 assembly (clang, -O0):
<find_completion_word_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1040 <find_word_end_termchar@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1186 <find_completion_word_start+0x66>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11d3 <find_completion_word_start+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <find_word_start@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11b5 <find_completion_word_start+0x95>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11c0 <find_completion_word_start+0xa0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e9 <find_completion_word_start+0xc9>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    11e9 <find_completion_word_start+0xc9>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1155 <find_completion_word_start+0x35>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_completion_word_start>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
movslq %esi,%rbp
add    %rdi,%rbp
mov    %rdi,%rax
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1040 <find_word_end_termchar@plt>
cmp    %rbp,%rax
jae    115a <find_completion_word_start+0x3a>
cmpb   $0x0,(%rax)
jne    1140 <find_completion_word_start+0x20>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <find_word_start@plt>
cmp    %rbp,%rax
jae    1175 <find_completion_word_start+0x55>
cmpb   $0x0,(%rax)
je     1175 <find_completion_word_start+0x55>
sub    %r15d,%eax
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_completion_word_start(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8* %13, i8** %6
%14 = load i8*, i8** %4
store i8* %14, i8** %7
%15 = load i8*, i8** %4
store i8* %15, i8** %8
store i32 0, i32* %9
br label %16
16: ; preds = %51, %2
%17 = load i8*, i8** %8
%18 = load i8*, i8** %6
%19 = call i8* @find_word_end_termchar(i8* %17, i8* %18, i8 signext 0)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = load i8*, i8** %6
%22 = icmp uge i8* %20, %21
br i1 %22, label %28, label %23
23: ; preds = %16
%24 = load i8*, i8** %8
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %49
28: ; preds = %23, %16
%29 = load i8*, i8** %7
%30 = load i8*, i8** %6
%31 = call i8* @find_word_start(i8* %29, i8* %30)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = load i8*, i8** %6
%34 = icmp uge i8* %32, %33
br i1 %34, label %40, label %35
35: ; preds = %28
%36 = load i8*, i8** %7
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35, %28
%41 = load i32, i32* %5
store i32 %41, i32* %3
br label %54
42: ; preds = %35
%43 = load i8*, i8** %7
%44 = load i8*, i8** %4
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
%48 = trunc i64 %47 to i32
store i32 %48, i32* %3
br label %54
49: ; preds = %23
%50 = load i8*, i8** %8
store i8* %50, i8** %7
br label %51
51: ; preds = %49
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %16
54: ; preds = %42, %40
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
define i32 @find_completion_word_start(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
br label %5
5: ; preds = %9, %2
%6 = phi i8* [ %0, %2 ], [ %7, %9 ]
%7 = tail call i8* @find_word_end_termchar(i8* %6, i8* %4, i8 signext 0)
%8 = icmp ult i8* %7, %4
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i8, i8* %7
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %5
12: ; preds = %9, %5
%13 = tail call i8* @find_word_start(i8* %6, i8* %4)
%14 = icmp ult i8* %13, %4
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i8, i8* %13
%17 = icmp eq i8 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = ptrtoint i8* %13 to i64
%20 = ptrtoint i8* %0 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
br label %23
23: ; preds = %12, %15, %18
%24 = phi i32 [ %22, %18 ], [ %1, %15 ], [ %1, %12 ]
ret i32 %24
}