#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 len(char const *);
char *strdup(char const *);
scalar_t__ toupper(char);
char *to_upper(const char *s) {
  char *up = strdup(s);
  char *to = up;
  int i;
  for (i = 0; i < len(s); i++) {
    *to = (char)toupper(*to);
    to++;
  }
  *to = '\0';
  return up;
}


x86 assembly (clang, -O0):
<to_upper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <len@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    11a2 <to_upper+0x72>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1158 <to_upper+0x28>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_upper>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1050 <strdup@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <len@plt>
mov    %r15,%rcx
test   %eax,%eax
jle    1184 <to_upper+0x54>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1040 <toupper@plt>
mov    %al,(%r15,%rbx,1)
mov    %r14,%rdi
call   1030 <len@plt>
add    $0x1,%rbx
cmp    %eax,%ebx
jl     1160 <to_upper+0x30>
mov    %r15,%rcx
add    %rbx,%rcx
movb   $0x0,(%rcx)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define noalias i8* @to_upper(i8* %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i32 @len(i8* %0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %1, %5
%6 = phi i32 [ %12, %5 ], [ 0, %1 ]
%7 = phi i8* [ %11, %5 ], [ %2, %1 ]
%8 = load i8, i8* %7
%9 = tail call i64 @toupper(i8 signext %8)
%10 = trunc i64 %9 to i8
store i8 %10, i8* %7
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = add nuw nsw i32 %6, 1
%13 = tail call i32 @len(i8* %0)
%14 = icmp slt i32 %12, %13
br i1 %14, label %5, label %15
15: ; preds = %5, %1
%16 = phi i8* [ %2, %1 ], [ %11, %5 ]
store i8 0, i8* %16
ret i8* %2
}