#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__ isspace(char const);
int strlen(char const *);
int count_leading_whitespace(const char *str) {
  int len, idx = 0, num = 0;
  if (str != NULL) {
    len = strlen(str);
    while ((idx < len) && isspace(str[idx++])) {
      num++;
    }
  }
  return num;
}


x86 assembly (clang, -O0):
<count_leading_whitespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11aa <count_leading_whitespace+0x8a>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x15(%rbp)
jge    1187 <count_leading_whitespace+0x67>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1197 <count_leading_whitespace+0x77>
jmp    11a5 <count_leading_whitespace+0x85>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1151 <count_leading_whitespace+0x31>
jmp    11aa <count_leading_whitespace+0x8a>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_leading_whitespace>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     115b <count_leading_whitespace+0x3b>
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    115b <count_leading_whitespace+0x3b>
mov    %eax,%r15d
xor    %ebx,%ebx
nopl   (%rax)
movsbl (%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     115b <count_leading_whitespace+0x3b>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1140 <count_leading_whitespace+0x20>
mov    %r15d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @count_leading_whitespace(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %19, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = zext i32 %4 to i64
br label %8
8: ; preds = %6, %14
%9 = phi i64 [ 0, %6 ], [ %15, %14 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i64 @isspace(i8 signext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = add nuw nsw i64 %9, 1
%16 = icmp eq i64 %15, %7
br i1 %16, label %19, label %8
17: ; preds = %8
%18 = trunc i64 %9 to i32
br label %19
19: ; preds = %14, %17, %3, %1
%20 = phi i32 [ 0, %1 ], [ 0, %3 ], [ %18, %17 ], [ %4, %14 ]
ret i32 %20
}