#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 *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int /*<<< orphan*/ toupper(int /*<<< orphan*/);
char *upcase(char *str) {
  char *ptr, *hptr;
  ptr = (char *)malloc(strlen(str) + 1);
  if (ptr == (char *)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     11ab <upcase+0x5b>
mov    %rax,%r14
mov    (%r15),%al
test   %al,%al
je     119c <upcase+0x4c>
add    $0x1,%r15
mov    %r14,%rbx
nopl   0x0(%rax)
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>
jmp    119f <upcase+0x4f>
mov    %r14,%rbx
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

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