#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
int /*<<< orphan*/ toupper(int /*<<< orphan*/);
char *upcase(const char *str) {
  char *ptr, *hptr;
  ptr = malloc(strlen(str) + 1);
  if (ptr == NULL) {
    (void)fprintf(stderr, "malloc failed\n");
    exit(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   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119f <upcase+0x4f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e6 <upcase+0x96>
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    11a7 <upcase+0x57>
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   1060 <malloc@plt>
test   %rax,%rax
je     11a6 <upcase+0x56>
mov    %rax,%r14
mov    (%r15),%al
mov    %r14,%rbx
test   %al,%al
je     119a <upcase+0x4a>
add    $0x1,%r15
mov    %r14,%rbx
nop
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
jne    1180 <upcase+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"malloc failed\0A\00"
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 i8* @malloc(i64 %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i8*, i8** %3
store i8* %16, i8** %4
br label %17
17: ; preds = %22, %15
%18 = load i8*, i8** %2
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %2
%25 = load i8, i8* %23
%26 = sext i8 %25 to i32
%27 = call i32 @toupper(i32 %26)
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %3
store i8 %28, i8* %29
br label %17
31: ; preds = %17
%32 = load i8*, i8** %3
store i8 0, i8* %32
%33 = load i8*, i8** %4
ret i8* %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"malloc failed\0A\00"
define noalias i8* @upcase(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %24, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6, %13
%14 = phi i8 [ %22, %13 ], [ %7, %6 ]
%15 = phi i8* [ %18, %13 ], [ %0, %6 ]
%16 = phi i8* [ %21, %13 ], [ %4, %6 ]
%17 = sext i8 %14 to i32
%18 = getelementptr inbounds i8, i8* %15, i64 1
%19 = tail call i32 @toupper(i32 %17)
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %20, i8* %16
%22 = load i8, i8* %18
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %13
24: ; preds = %13, %6
%25 = phi i8* [ %4, %6 ], [ %21, %13 ]
store i8 0, i8* %25
ret i8* %4
}