#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 *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *TOUPPER(char *p) {
  static char *buf;
  static int flag = 0;
  int i;
  if (flag == 1) {
    return buf;
  }
  buf = malloc(strlen(p) + 1);
  strcpy(buf, p);
  for (i = 0; i < (strlen(buf)); i++)
    if ((buf[i] >= 'a') && (buf[i] <= 'z'))
      buf[i] -= 32;
  flag = 1;
  return buf;
}


x86 assembly (clang, -O0):
<TOUPPER>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0x1,0x2f05(%rip)
jne    1159 <TOUPPER+0x29>
mov    0x2ef0(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1216 <TOUPPER+0xe6>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ecd(%rip)
mov    0x2ec6(%rip),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2ea9(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    1201 <TOUPPER+0xd1>
mov    0x2e90(%rip),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x61,%eax
jl     11ee <TOUPPER+0xbe>
mov    0x2e78(%rip),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x7a,%eax
jg     11ee <TOUPPER+0xbe>
mov    0x2e60(%rip),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
sub    $0x20,%edx
mov    %dl,(%rax,%rcx,1)
jmp    11f3 <TOUPPER+0xc3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118a <TOUPPER+0x5a>
movl   $0x1,0x2e3d(%rip)
mov    0x2e2e(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TOUPPER>:
push   %rbx
cmpb   $0x0,0x2f10(%rip)
jne    11a1 <TOUPPER+0x71>
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,0x2eef(%rip)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2edd(%rip),%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    119a <TOUPPER+0x6a>
xor    %ebx,%ebx
jmp    1180 <TOUPPER+0x50>
add    $0x1,%rbx
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    119a <TOUPPER+0x6a>
mov    0x2eb9(%rip),%rdi
movzbl (%rdi,%rbx,1),%eax
lea    -0x61(%rax),%ecx
cmp    $0x19,%cl
ja     1170 <TOUPPER+0x40>
add    $0xe0,%al
mov    %al,(%rdi,%rbx,1)
jmp    1170 <TOUPPER+0x40>
movb   $0x1,0x2ea7(%rip)
mov    0x2e98(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
@TOUPPER.buf = internal global i8* null
@TOUPPER.flag = internal global i32 0
define i8* @TOUPPER(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i32, i32* @TOUPPER.flag
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i8*, i8** @TOUPPER.buf
store i8* %8, i8** %2
br label %53
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** @TOUPPER.buf
%14 = load i8*, i8** @TOUPPER.buf
%15 = load i8*, i8** %3
%16 = call i32 @strcpy(i8* %14, i8* %15)
store i32 0, i32* %4
br label %17
17: ; preds = %48, %9
%18 = load i32, i32* %4
%19 = load i8*, i8** @TOUPPER.buf
%20 = call i32 @strlen(i8* %19)
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %51
22: ; preds = %17
%23 = load i8*, i8** @TOUPPER.buf
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp sge i32 %28, 97
br i1 %29, label %30, label %47
30: ; preds = %22
%31 = load i8*, i8** @TOUPPER.buf
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp sle i32 %36, 122
br i1 %37, label %38, label %47
38: ; preds = %30
%39 = load i8*, i8** @TOUPPER.buf
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = sub nsw i32 %44, 32
%46 = trunc i32 %45 to i8
store i8 %46, i8* %42
br label %47
47: ; preds = %38, %30, %22
br label %48
48: ; preds = %47
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
br label %17
51: ; preds = %17
store i32 1, i32* @TOUPPER.flag
%52 = load i8*, i8** @TOUPPER.buf
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %7
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@TOUPPER.buf = internal unnamed_addr global i8* null
@TOUPPER.flag = internal unnamed_addr global i1 false
define i8* @TOUPPER(i8* %0) {
%2 = load i1, i1* @TOUPPER.flag
br i1 %2, label %26, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
store i8* %6, i8** @TOUPPER.buf
%7 = tail call i32 @strcpy(i8* %6, i8* %0)
%8 = load i8*, i8** @TOUPPER.buf
%9 = tail call i32 @strlen(i8* %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %3, %20
%12 = phi i64 [ %21, %20 ], [ 0, %3 ]
%13 = load i8*, i8** @TOUPPER.buf
%14 = getelementptr inbounds i8, i8* %13, i64 %12
%15 = load i8, i8* %14
%16 = add i8 %15, -97
%17 = icmp ult i8 %16, 26
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = add nsw i8 %15, -32
store i8 %19, i8* %14
br label %20
20: ; preds = %11, %18
%21 = add nuw nsw i64 %12, 1
%22 = tail call i32 @strlen(i8* %13)
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %11, label %25
25: ; preds = %20, %3
store i1 true, i1* @TOUPPER.flag
br label %26
26: ; preds = %1, %25
%27 = load i8*, i8** @TOUPPER.buf
ret i8* %27
}