#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 */
int check_case(char const *, size_t);
char toupper(char);
void recase(char *str, size_t len, const char *tmpl, size_t tmpl_len) {
  size_t i;
  int tmpl_case = check_case(tmpl, tmpl_len);
  if (tmpl_case >= 1)
    *str = toupper(*str);
  if (tmpl_case == 2)
    for (i = 1; i < len; i++)
      str[i] = toupper(str[i]);
}


x86 assembly (clang, -O0):
<recase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <check_case@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jl     1166 <recase+0x46>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
cmpl   $0x2,-0x2c(%rbp)
jne    11ba <recase+0x9a>
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11b5 <recase+0x95>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1178 <recase+0x58>
jmp    11ba <recase+0x9a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recase>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,%rdi
mov    %rcx,%rsi
call   1040 <check_case@plt>
test   %eax,%eax
jle    1175 <recase+0x55>
mov    %eax,%ebp
movsbl (%rbx),%edi
call   1030 <toupper@plt>
mov    %al,(%rbx)
cmp    $0x2,%ebp
jne    1175 <recase+0x55>
cmp    $0x2,%r14
jb     1175 <recase+0x55>
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movsbl (%rbx,%rbp,1),%edi
call   1030 <toupper@plt>
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1160 <recase+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @recase(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%11 = load i8*, i8** %7
%12 = load i64, i64* %8
%13 = call i32 @check_case(i8* %11, i64 %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp sge i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %4
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = call signext i8 @toupper(i8 signext %18)
%20 = load i8*, i8** %5
store i8 %19, i8* %20
br label %21
21: ; preds = %16, %4
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %42
24: ; preds = %21
store i64 1, i64* %9
br label %25
25: ; preds = %38, %24
%26 = load i64, i64* %9
%27 = load i64, i64* %6
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %41
29: ; preds = %25
%30 = load i8*, i8** %5
%31 = load i64, i64* %9
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i8, i8* %32
%34 = call signext i8 @toupper(i8 signext %33)
%35 = load i8*, i8** %5
%36 = load i64, i64* %9
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 %34, i8* %37
br label %38
38: ; preds = %29
%39 = load i64, i64* %9
%40 = add i64 %39, 1
store i64 %40, i64* %9
br label %25
41: ; preds = %25
br label %42
42: ; preds = %41, %21
ret void
}

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