#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 */
typedef int /*<<< orphan*/ __m128i;

/* Variables and functions */
int /*<<< orphan*/ _mm_and_si128(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_cmpgt_epi8(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_cmplt_epi8(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_loadu_si128(int /*<<< orphan*/ *);
int /*<<< orphan*/ _mm_set1_epi8(int);
int /*<<< orphan*/ _mm_storeu_si128(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_sub_epi8(int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
void toupper_optimized(char *text) {
  size_t offset = 0;
  int len = 0;
  __m128i b1, mask1, mask2, upper1, upper2, delta;
  upper1 = _mm_set1_epi8(0x60);
  upper2 = _mm_set1_epi8(0x7B);
  delta = _mm_set1_epi8(0x20);
  len = strlen(text);
  do {
    b1 = _mm_loadu_si128((__m128i *)text);
    mask1 = _mm_cmpgt_epi8(b1, upper1);
    mask2 = _mm_cmplt_epi8(b1, upper2);
    mask1 = _mm_and_si128(delta, _mm_and_si128(mask1, mask2));
    _mm_storeu_si128((__m128i *)(text), _mm_sub_epi8(b1, mask1));
    offset += 16;
    text += 16;
  } while (len > offset);
  return;
}


x86 assembly (clang, -O0):
<toupper_optimized>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x60,%edi
call   1040 <_mm_set1_epi8@plt>
mov    %eax,-0x24(%rbp)
mov    $0x7b,%edi
call   1040 <_mm_set1_epi8@plt>
mov    %eax,-0x28(%rbp)
mov    $0x20,%edi
call   1040 <_mm_set1_epi8@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <_mm_loadu_si128@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1030 <_mm_cmpgt_epi8@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1080 <_mm_cmplt_epi8@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10a0 <_mm_and_si128@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
call   10a0 <_mm_and_si128@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <_mm_sub_epi8@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1070 <_mm_storeu_si128@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x8(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     11ce <toupper_optimized+0x4e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toupper_optimized>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    $0x60,%edi
call   1040 <_mm_set1_epi8@plt>
mov    %eax,0x14(%rsp)
mov    $0x7b,%edi
call   1040 <_mm_set1_epi8@plt>
mov    %eax,0x10(%rsp)
mov    $0x20,%edi
call   1040 <_mm_set1_epi8@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%r13
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%r15
xor    %r12d,%r12d
nopl   0x0(%rax)
lea    (%r12,%r13,1),%rbx
mov    %rbx,%rdi
call   1090 <_mm_loadu_si128@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    0x14(%rsp),%esi
call   1030 <_mm_cmpgt_epi8@plt>
mov    %eax,%r14d
mov    %ebp,%edi
mov    0x10(%rsp),%esi
call   1080 <_mm_cmplt_epi8@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   10a0 <_mm_and_si128@plt>
mov    0xc(%rsp),%edi
mov    %eax,%esi
call   10a0 <_mm_and_si128@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <_mm_sub_epi8@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <_mm_storeu_si128@plt>
add    $0x10,%r12
cmp    %r15,%r12
jb     11d0 <toupper_optimized+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @toupper_optimized(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %2
store i64 0, i64* %3
store i32 0, i32* %4
%11 = call i32 @_mm_set1_epi8(i32 96)
store i32 %11, i32* %8
%12 = call i32 @_mm_set1_epi8(i32 123)
store i32 %12, i32* %9
%13 = call i32 @_mm_set1_epi8(i32 32)
store i32 %13, i32* %10
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %4
br label %16
16: ; preds = %41, %1
%17 = load i8*, i8** %2
%18 = bitcast i8* %17 to i32*
%19 = call i32 @_mm_loadu_si128(i32* %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = load i32, i32* %8
%22 = call i32 @_mm_cmpgt_epi8(i32 %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %5
%24 = load i32, i32* %9
%25 = call i32 @_mm_cmplt_epi8(i32 %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %10
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = call i32 @_mm_and_si128(i32 %27, i32 %28)
%30 = call i32 @_mm_and_si128(i32 %26, i32 %29)
store i32 %30, i32* %6
%31 = load i8*, i8** %2
%32 = bitcast i8* %31 to i32*
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = call i32 @_mm_sub_epi8(i32 %33, i32 %34)
%36 = call i32 @_mm_storeu_si128(i32* %32, i32 %35)
%37 = load i64, i64* %3
%38 = add i64 %37, 16
store i64 %38, i64* %3
%39 = load i8*, i8** %2
%40 = getelementptr inbounds i8, i8* %39, i64 16
store i8* %40, i8** %2
br label %41
41: ; preds = %16
%42 = load i32, i32* %4
%43 = sext i32 %42 to i64
%44 = load i64, i64* %3
%45 = icmp ugt i64 %43, %44
br i1 %45, label %16, label %46
46: ; preds = %41
ret void
}

LLVM IR (-O2):
define void @toupper_optimized(i8* %0) {
%2 = tail call i32 @_mm_set1_epi8(i32 96)
%3 = tail call i32 @_mm_set1_epi8(i32 123)
%4 = tail call i32 @_mm_set1_epi8(i32 32)
%5 = tail call i32 @strlen(i8* %0)
%6 = sext i32 %5 to i64
br label %7
7: ; preds = %7, %1
%8 = phi i64 [ 0, %1 ], [ %18, %7 ]
%9 = phi i8* [ %0, %1 ], [ %19, %7 ]
%10 = bitcast i8* %9 to i32*
%11 = tail call i32 @_mm_loadu_si128(i32* %10)
%12 = tail call i32 @_mm_cmpgt_epi8(i32 %11, i32 %2)
%13 = tail call i32 @_mm_cmplt_epi8(i32 %11, i32 %3)
%14 = tail call i32 @_mm_and_si128(i32 %12, i32 %13)
%15 = tail call i32 @_mm_and_si128(i32 %4, i32 %14)
%16 = tail call i32 @_mm_sub_epi8(i32 %11, i32 %15)
%17 = tail call i32 @_mm_storeu_si128(i32* %10, i32 %16)
%18 = add i64 %8, 16
%19 = getelementptr inbounds i8, i8* %9, i64 16
%20 = icmp ult i64 %18, %6
br i1 %20, label %7, label %21
21: ; preds = %7
ret void
}