#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__ strlen(char const *);
int /*<<< orphan*/ toupper(int /*<<< orphan*/);
scalar_t__ xmalloc(scalar_t__);
char *upcase(const char *str) {
  char *ptr, *hptr;
  ptr = (char *)xmalloc(strlen(str) + 1);
  hptr = ptr;
  while (*str != '\0')
    *ptr++ = toupper(*str++);
  *ptr = '\0';
  return (hptr);
}


x86 assembly (clang, -O0):
<upcase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119c <upcase+0x6c>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    115d <upcase+0x2d>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upcase>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    (%r15),%al
test   %al,%al
je     117c <upcase+0x4c>
add    $0x1,%r15
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
jne    1160 <upcase+0x30>
jmp    117f <upcase+0x4f>
mov    %r14,%rbx
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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