#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 toupper(char);
void build(char *str, int *hist) {
  char ch = -1;
  while (*str) {
    if (toupper(*str) != ch) {
      ch = toupper(*str);
      hist[ch - 'A']++;
    }
    ++str;
  }
}


x86 assembly (clang, -O0):
<build>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0xff,-0x11(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1183 <build+0x73>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
movsbl %al,%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
je     1172 <build+0x62>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
movsbl -0x11(%rbp),%ecx
sub    $0x41,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1124 <build+0x14>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%al
test   %al,%al
je     1162 <build+0x52>
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0x1,%rbx
mov    $0xff,%bpl
jmp    113b <build+0x2b>
nopl   0x0(%rax)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1162 <build+0x52>
movsbl %al,%edi
call   1030 <toupper@plt>
cmp    %bpl,%al
je     1130 <build+0x20>
movsbl -0x1(%rbx),%edi
call   1030 <toupper@plt>
mov    %eax,%ebp
movsbq %al,%rax
addl   $0x1,-0x104(%r14,%rax,4)
ff 01
jmp    1130 <build+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define void @build(i8* nocapture %0, i32* nocapture %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %24, label %5
5: ; preds = %2, %19
%6 = phi i8 [ %22, %19 ], [ %3, %2 ]
%7 = phi i8 [ %20, %19 ], [ -1, %2 ]
%8 = phi i8* [ %21, %19 ], [ %0, %2 ]
%9 = tail call signext i8 @toupper(i8 signext %6)
%10 = icmp eq i8 %9, %7
br i1 %10, label %19, label %11
11: ; preds = %5
%12 = load i8, i8* %8
%13 = tail call signext i8 @toupper(i8 signext %12)
%14 = sext i8 %13 to i64
%15 = add nsw i64 %14, -65
%16 = getelementptr inbounds i32, i32* %1, i64 %15
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
store i32 %18, i32* %16
br label %19
19: ; preds = %11, %5
%20 = phi i8 [ %13, %11 ], [ %7, %5 ]
%21 = getelementptr inbounds i8, i8* %8, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %5
24: ; preds = %19, %2
ret void
}