#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 */
scalar_t__ calloc(int, int);
size_t strlen(char *);
char toupper(char);
char *toUpper(char *text) {
  char *ret = (char *)calloc(strlen(text) + 1, sizeof(char));
  int i;
  for (i = 0; i < strlen(text); i++)
    ret[i] = toupper(text[i]);
  ret[strlen(text)] = 0;
  return ret;
}


x86 assembly (clang, -O0):
<toUpper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jae    11ad <toUpper+0x7d>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <toUpper+0x30>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toUpper>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
cmpb   $0x0,(%r15)
je     1181 <toUpper+0x51>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl (%r15,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %rbx,%rax
ja     1160 <toUpper+0x30>
jmp    1183 <toUpper+0x53>
xor    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @toUpper(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
%7 = add i64 %6, 1
%8 = trunc i64 %7 to i32
%9 = call i64 @calloc(i32 %8, i32 1)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
store i32 0, i32* %4
br label %11
11: ; preds = %28, %1
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = load i8*, i8** %2
%15 = call i64 @strlen(i8* %14)
%16 = icmp ult i64 %13, %15
br i1 %16, label %17, label %31
17: ; preds = %11
%18 = load i8*, i8** %2
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = call signext i8 @toupper(i8 signext %22)
%24 = load i8*, i8** %3
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
br label %28
28: ; preds = %17
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %11
31: ; preds = %11
%32 = load i8*, i8** %3
%33 = load i8*, i8** %2
%34 = call i64 @strlen(i8* %33)
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %3
ret i8* %36
}

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