#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__ isupper(char const);
int strlen(char const *);
int countupper(const char *str) {
  int strlength = strlen(str);
  int numberUpper = 0;
  for (int idx = 0; idx < strlength; idx++) {
    if (isupper(str[idx]))
      numberUpper++;
  }
  return numberUpper;
}


x86 assembly (clang, -O0):
<countupper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1189 <countupper+0x69>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isupper@plt>
cmp    $0x0,%rax
je     1176 <countupper+0x56>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    117b <countupper+0x5b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <countupper+0x26>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countupper>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    115c <countupper+0x3c>
mov    %eax,%r15d
xor    %ebx,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1040 <isupper@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1140 <countupper+0x20>
jmp    115e <countupper+0x3e>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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