#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 /*<<< orphan*/ rul__mem_free(char *);
char *rul__mem_malloc(int);
int strlen(char *);
char toupper(char);
char *rul__dbg_toupper(char *str) {
  int i;
  int str_len = strlen(str);
  static char *upstr;
  static long upstr_len;
  if (!upstr || (upstr_len <= str_len)) {
    if (upstr)
      rul__mem_free(upstr);
    upstr = rul__mem_malloc(str_len + 1);
  }
  for (i = 0; i < str_len; i++)
    upstr[i] = toupper(str[i]);
  upstr[str_len] = '\0';
  return (upstr);
}


x86 assembly (clang, -O0):
<rul__dbg_toupper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
cmpq   $0x0,0x2ee8(%rip)
je     117a <rul__dbg_toupper+0x3a>
mov    0x2ee3(%rip),%rax
movslq -0x10(%rbp),%rcx
cmp    %rcx,%rax
jg     11a6 <rul__dbg_toupper+0x66>
cmpq   $0x0,0x2ec6(%rip)
je     1194 <rul__dbg_toupper+0x54>
mov    0x2eb9(%rip),%rdi
call   1060 <rul__mem_free@plt>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <rul__mem_malloc@plt>
mov    %rax,0x2ea2(%rip)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11e8 <rul__dbg_toupper+0xa8>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    0x2e75(%rip),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ad <rul__dbg_toupper+0x6d>
mov    0x2e59(%rip),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e4a(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rul__dbg_toupper>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x2eee(%rip),%rax
test   %rax,%rax
je     1164 <rul__dbg_toupper+0x24>
test   %r14d,%r14d
js     11af <rul__dbg_toupper+0x6f>
test   %rax,%rax
je     1171 <rul__dbg_toupper+0x31>
mov    %rax,%rdi
call   1060 <rul__mem_free@plt>
lea    0x1(%r14),%edi
call   1050 <rul__mem_malloc@plt>
mov    %rax,0x2ec7(%rip)
test   %r14d,%r14d
jle    11af <rul__dbg_toupper+0x6f>
mov    %r14d,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %eax,%ecx
mov    0x2ea5(%rip),%rax
mov    %cl,(%rax,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1190 <rul__dbg_toupper+0x50>
movslq %r14d,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@rul__dbg_toupper.upstr = internal global i8* null
@rul__dbg_toupper.upstr_len = internal global i64 0
define i8* @rul__dbg_toupper(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
store i32 %6, i32* %4
%7 = load i8*, i8** @rul__dbg_toupper.upstr
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i64, i64* @rul__dbg_toupper.upstr_len
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = icmp sle i64 %10, %12
br i1 %13, label %14, label %24
14: ; preds = %9, %1
%15 = load i8*, i8** @rul__dbg_toupper.upstr
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** @rul__dbg_toupper.upstr
%19 = call i32 @rul__mem_free(i8* %18)
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
%23 = call i8* @rul__mem_malloc(i32 %22)
store i8* %23, i8** @rul__dbg_toupper.upstr
br label %24
24: ; preds = %20, %9
store i32 0, i32* %3
br label %25
25: ; preds = %40, %24
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = load i8*, i8** %2
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = call signext i8 @toupper(i8 signext %34)
%36 = load i8*, i8** @rul__dbg_toupper.upstr
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
br label %40
40: ; preds = %29
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %25
43: ; preds = %25
%44 = load i8*, i8** @rul__dbg_toupper.upstr
%45 = load i32, i32* %4
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** @rul__dbg_toupper.upstr
ret i8* %48
}

LLVM IR (-O2):
@rul__dbg_toupper.upstr = internal unnamed_addr global i8* null
define i8* @rul__dbg_toupper(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = load i8*, i8** @rul__dbg_toupper.upstr
%4 = icmp eq i8* %3, null
%5 = icmp sgt i32 %2, -1
%6 = select i1 %4, i1 true, i1 %5
br i1 %6, label %7, label %25
7: ; preds = %1
br i1 %4, label %10, label %8
8: ; preds = %7
%9 = tail call i32 @rul__mem_free(i8* %3)
br label %10
10: ; preds = %7, %8
%11 = add nsw i32 %2, 1
%12 = tail call i8* @rul__mem_malloc(i32 %11)
store i8* %12, i8** @rul__dbg_toupper.upstr
%13 = icmp sgt i32 %2, 0
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = zext i32 %2 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %23, %16 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = tail call signext i8 @toupper(i8 signext %19)
%21 = load i8*, i8** @rul__dbg_toupper.upstr
%22 = getelementptr inbounds i8, i8* %21, i64 %17
store i8 %20, i8* %22
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, %15
br i1 %24, label %25, label %16
25: ; preds = %16, %1, %10
%26 = phi i8* [ %12, %10 ], [ %3, %1 ], [ %21, %16 ]
%27 = sext i32 %2 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
ret i8* %26
}