double compute_average_word_length(const char *sentence) {
  int i = 0;
  int chars = 0.0f;
  int words = 0.0f;
  while (sentence[i] != '\n') {
    switch (sentence[i]) {
    case ' ':
      words += 1;
      break;
    default:
      chars += 1;
      break;
    }
    i += 1;
  }
  return (chars / words);
}


x86 assembly (clang, -O0):
<compute_average_word_length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     116f <compute_average_word_length+0x6f>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%al
sub    $0x20,%al
jne    1158 <compute_average_word_length+0x58>
jmp    114a <compute_average_word_length+0x4a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1161 <compute_average_word_length+0x61>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    111d <compute_average_word_length+0x1d>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x14(%rbp)
cvtsi2sd %eax,%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_average_word_length>:
xor    %eax,%eax
xor    %ecx,%ecx
jmp    1117 <compute_average_word_length+0x17>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ecx
add    $0x1,%rdi
movzbl (%rdi),%edx
cmp    $0x20,%dl
je     1110 <compute_average_word_length+0x10>
cmp    $0xa,%dl
je     112d <compute_average_word_length+0x2d>
add    $0x1,%eax
add    $0x1,%rdi
jmp    1117 <compute_average_word_length+0x17>
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
ret

LLVM IR (-O0):
define double @compute_average_word_length(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %27, %1
%7 = load i8*, i8** %2
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %30
14: ; preds = %6
%15 = load i8*, i8** %2
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
switch i32 %20, label %24 [
i32 32, label %21
]
21: ; preds = %14
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
br label %27
24: ; preds = %14
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %21
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %6
30: ; preds = %6
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = sdiv i32 %31, %32
%34 = sitofp i32 %33 to double
ret double %34
}

LLVM IR (-O2):
define double @compute_average_word_length(i8* nocapture %0) {
br label %2
2: ; preds = %12, %1
%3 = phi i64 [ %15, %12 ], [ 0, %1 ]
%4 = phi i32 [ %13, %12 ], [ 0, %1 ]
%5 = phi i32 [ %14, %12 ], [ 0, %1 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %3
%7 = load i8, i8* %6
switch i8 %7, label %10 [
i8 10, label %16
i8 32, label %8
]
8: ; preds = %2
%9 = add nsw i32 %5, 1
br label %12
10: ; preds = %2
%11 = add nsw i32 %4, 1
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %4, %8 ], [ %11, %10 ]
%14 = phi i32 [ %9, %8 ], [ %5, %10 ]
%15 = add nuw i64 %3, 1
br label %2
16: ; preds = %2
%17 = sdiv i32 %4, %5
%18 = sitofp i32 %17 to double
ret double %18
}