#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 */
char *strdup(char const *);
size_t strlen(char *);
char toupper(char);
char *str2upper(const char *lower) {
  char *upper;
  size_t i;
  upper = strdup(lower);
  for (i = 0; i < strlen(upper); i++) {
    upper[i] = toupper(upper[i]);
  }
  return upper;
}


x86 assembly (clang, -O0):
<str2upper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jae    11a1 <str2upper+0x71>
mov    -0x10(%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    1151 <str2upper+0x21>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2upper>:
push   %r14
push   %rbx
push   %rax
call   1050 <strdup@plt>
mov    %rax,%r14
mov    (%rax),%al
test   %al,%al
je     117f <str2upper+0x4f>
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %al,(%r14)
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%rax
jb     117f <str2upper+0x4f>
mov    $0x1,%ebx
movsbl (%r14,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
jb     1160 <str2upper+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @str2upper(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @strdup(i8* %5)
store i8* %6, i8** %3
store i64 0, i64* %4
br label %7
7: ; preds = %21, %1
%8 = load i64, i64* %4
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
%11 = icmp ult i64 %8, %10
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = load i64, i64* %4
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i8, i8* %15
%17 = call signext i8 @toupper(i8 signext %16)
%18 = load i8*, i8** %3
%19 = load i64, i64* %4
%20 = getelementptr inbounds i8, i8* %18, i64 %19
store i8 %17, i8* %20
br label %21
21: ; preds = %12
%22 = load i64, i64* %4
%23 = add i64 %22, 1
store i64 %23, i64* %4
br label %7
24: ; preds = %7
%25 = load i8*, i8** %3
ret i8* %25
}

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