#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
char toupper(char);
char *strtoupper(char *s) {
  char *t;
  size_t i, l;
  l = strlen(s);
  if ((t = (char *)malloc(l + 1)) == NULL) {
    fputs("strtoupper: out of memory\n", stderr);
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < l; i++)
    t[i] = toupper(s[i]);
  t[i] = '\0';
  return t;
}


x86 assembly (clang, -O0):
<strtoupper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a7 <strtoupper+0x57>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11ec <strtoupper+0x9c>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11af <strtoupper+0x5f>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoupper>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ab <strtoupper+0x5b>
mov    %rax,%r12
test   %r15,%r15
je     1197 <strtoupper+0x47>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1180 <strtoupper+0x30>
movb   $0x0,(%r12,%r15,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"strtoupper: out of memory\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @strtoupper(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = add i64 %8, 1
%10 = call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
store i64 0, i64* %4
br label %19
19: ; preds = %32, %18
%20 = load i64, i64* %4
%21 = load i64, i64* %5
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = load i8*, i8** %2
%25 = load i64, i64* %4
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8, i8* %26
%28 = call signext i8 @toupper(i8 signext %27)
%29 = load i8*, i8** %3
%30 = load i64, i64* %4
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 %28, i8* %31
br label %32
32: ; preds = %23
%33 = load i64, i64* %4
%34 = add i64 %33, 1
store i64 %34, i64* %4
br label %19
35: ; preds = %19
%36 = load i8*, i8** %3
%37 = load i64, i64* %4
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@.str = [27 x i8] c"strtoupper: out of memory\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @strtoupper(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add 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 %9, label %7
7: ; preds = %1
%8 = icmp eq i64 %2, 0
br i1 %8, label %22, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7, %14
%15 = phi i64 [ %20, %14 ], [ 0, %7 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call signext i8 @toupper(i8 signext %17)
%19 = getelementptr inbounds i8, i8* %5, i64 %15
store i8 %18, i8* %19
%20 = add nuw i64 %15, 1
%21 = icmp eq i64 %20, %2
br i1 %21, label %22, label %14
22: ; preds = %14, %7
%23 = getelementptr inbounds i8, i8* %5, i64 %2
store i8 0, i8* %23
ret i8* %5
}