#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__ isalpha(char const);
int strlen(char const *);
int lengthOfLastWord(const char *s) {
  if (!s || !*s)
    return 0;
  int len = strlen(s);
  int sum = 0;
  int i = len - 1;
  while (i >= 0 && !isalpha(s[i]))
    i--;
  while (isalpha(s[i])) {
    ++sum;
    i--;
  }
  return sum;
}


x86 assembly (clang, -O0):
<lengthOfLastWord>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1144 <lengthOfLastWord+0x24>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1150 <lengthOfLastWord+0x30>
movl   $0x0,-0x4(%rbp)
jmp    11f3 <lengthOfLastWord+0xd3>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jl     1198 <lengthOfLastWord+0x78>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11a8 <lengthOfLastWord+0x88>
jmp    11b6 <lengthOfLastWord+0x96>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    116c <lengthOfLastWord+0x4c>
jmp    11bb <lengthOfLastWord+0x9b>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%rax
je     11ed <lengthOfLastWord+0xcd>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11bb <lengthOfLastWord+0x9b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lengthOfLastWord>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     11a5 <lengthOfLastWord+0x85>
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     11a5 <lengthOfLastWord+0x85>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    $0xffffffff,%eax
cltq
lea    (%rax,%r14,1),%rcx
add    $0xffffffffffffffff,%rcx
xchg   %ax,%ax
mov    %rcx,%rbx
test   %ebp,%ebp
jle    1170 <lengthOfLastWord+0x50>
add    $0xffffffffffffffff,%rbp
mov    %ebp,%eax
movsbl (%r14,%rax,1),%edi
call   1040 <isalpha@plt>
lea    -0x1(%rbx),%rcx
test   %rax,%rax
je     1150 <lengthOfLastWord+0x30>
movsbl 0x1(%rbx),%edi
call   1040 <isalpha@plt>
test   %rax,%rax
je     11a5 <lengthOfLastWord+0x85>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
movsbl (%rbx),%edi
call   1040 <isalpha@plt>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1190 <lengthOfLastWord+0x70>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @lengthOfLastWord(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %36, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %36, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = zext i32 %7 to i64
br label %9
9: ; preds = %14, %6
%10 = phi i64 [ %11, %14 ], [ %8, %6 ]
%11 = add nsw i64 %10, -1
%12 = trunc i64 %10 to i32
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = and i64 %11, 4294967295
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i64 @isalpha(i8 signext %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %9, label %20
20: ; preds = %9, %14
%21 = shl i64 %11, 32
%22 = ashr exact i64 %21, 32
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = load i8, i8* %23
%25 = tail call i64 @isalpha(i8 signext %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %20, %27
%28 = phi i64 [ %31, %27 ], [ %22, %20 ]
%29 = phi i32 [ %30, %27 ], [ 0, %20 ]
%30 = add nuw nsw i32 %29, 1
%31 = add i64 %28, -1
%32 = getelementptr inbounds i8, i8* %0, i64 %31
%33 = load i8, i8* %32
%34 = tail call i64 @isalpha(i8 signext %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %27
36: ; preds = %27, %20, %1, %3
%37 = phi i32 [ 0, %3 ], [ 0, %1 ], [ 0, %20 ], [ %30, %27 ]
ret i32 %37
}