#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*/ free(char *);
char *malloc(scalar_t__);
scalar_t__ strlen(char *);
char toupper(char);
char *strtoupper(char *str) {
  static char *upper = NULL;
  char *up;
  if (upper)
    free(upper);
  up = upper = malloc(strlen(str) + 1);
  while (*up++ = toupper(*str)) {
    str++;
  }
  return upper;
}


x86 assembly (clang, -O0):
<strtoupper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,0x2ef4(%rip)
je     1166 <strtoupper+0x26>
mov    0x2ee7(%rip),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,0x2ec6(%rip)
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %al,(%rcx)
cmp    $0x0,%al
je     11bc <strtoupper+0x7c>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1186 <strtoupper+0x46>
mov    0x2e85(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoupper>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2ef9(%rip),%rdi
test   %rdi,%rdi
je     1159 <strtoupper+0x19>
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,0x2ed4(%rip)
movsbl (%r14),%edi
call   1040 <toupper@plt>
mov    %al,(%r15)
test   %al,%al
je     11a6 <strtoupper+0x66>
mov    $0x1,%ebx
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edi
call   1040 <toupper@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
test   %al,%al
jne    1190 <strtoupper+0x50>
mov    0x2e9b(%rip),%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@strtoupper.upper = internal global i8* null
define i8* @strtoupper(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** @strtoupper.upper
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i8*, i8** @strtoupper.upper
%8 = call i32 @free(i8* %7)
br label %9
9: ; preds = %6, %1
%10 = load i8*, i8** %2
%11 = call i64 @strlen(i8* %10)
%12 = add nsw i64 %11, 1
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** @strtoupper.upper
store i8* %13, i8** %3
br label %14
14: ; preds = %21, %9
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = call signext i8 @toupper(i8 signext %16)
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
store i8 %17, i8* %18
%20 = icmp ne i8 %17, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %2
br label %14
24: ; preds = %14
%25 = load i8*, i8** @strtoupper.upper
ret i8* %25
}

LLVM IR (-O2):
@strtoupper.upper = internal unnamed_addr global i8* null
define i8* @strtoupper(i8* nocapture %0) {
%2 = load i8*, i8** @strtoupper.upper
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @free(i8* %2)
br label %6
6: ; preds = %4, %1
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
store i8* %9, i8** @strtoupper.upper
%10 = load i8, i8* %0
%11 = tail call signext i8 @toupper(i8 signext %10)
store i8 %11, i8* %9
%12 = icmp eq i8 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %6, %13
%14 = phi i8* [ %16, %13 ], [ %9, %6 ]
%15 = phi i8* [ %17, %13 ], [ %0, %6 ]
%16 = getelementptr inbounds i8, i8* %14, i64 1
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = load i8, i8* %17
%19 = tail call signext i8 @toupper(i8 signext %18)
store i8 %19, i8* %16
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %13
21: ; preds = %13, %6
%22 = load i8*, i8** @strtoupper.upper
ret i8* %22
}