#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 *skipwhitespace(char *, char *, int);
char *skipword(char *, char *, int);
int countwords(char *buf, int bufsize) {
  int count;
  char *p;
  count = 0;
  for (p = buf;;) {
    if (!(p = skipwhitespace(p, buf, bufsize)))
      break;
    count++;
    if (!(p = skipword(p, buf, bufsize)))
      break;
  }
  return count;
}


x86 assembly (clang, -O0):
<countwords>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <skipwhitespace@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1161 <countwords+0x41>
jmp    1192 <countwords+0x72>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <skipword@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    118d <countwords+0x6d>
jmp    1192 <countwords+0x72>
jmp    113e <countwords+0x1e>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countwords>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
xor    %ebx,%ebx
mov    %rdi,%rax
nop
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <skipwhitespace@plt>
test   %rax,%rax
je     1159 <countwords+0x39>
add    $0x1,%ebx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <skipword@plt>
test   %rax,%rax
jne    1130 <countwords+0x10>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @countwords(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = load i8*, i8** %3
store i8* %7, i8** %6
br label %8
8: ; preds = %24, %2
%9 = load i8*, i8** %6
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = call i8* @skipwhitespace(i8* %9, i8* %10, i32 %11)
store i8* %12, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %8
br label %25
15: ; preds = %8
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
store i32 %17, i32* %5
%18 = load i8*, i8** %6
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = call i8* @skipword(i8* %18, i8* %19, i32 %20)
store i8* %21, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %15
br label %25
24: ; preds = %15
br label %8
25: ; preds = %23, %14
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
define i32 @countwords(i8* %0, i32 %1) {
br label %3
3: ; preds = %8, %2
%4 = phi i32 [ 0, %2 ], [ %9, %8 ]
%5 = phi i8* [ %0, %2 ], [ %10, %8 ]
%6 = tail call i8* @skipwhitespace(i8* %5, i8* %0, i32 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = add nuw nsw i32 %4, 1
%10 = tail call i8* @skipword(i8* %6, i8* %0, i32 %1)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %3
12: ; preds = %8, %3
%13 = phi i32 [ %9, %8 ], [ %4, %3 ]
ret i32 %13
}