#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 *brain_alloc_(long, int /*<<< orphan*/);
long strlen(char const *);
char toupper(char const);
char *brain_strdup_upper(char const *in_string) {
  if (!in_string)
    in_string = "";
  long length = strlen(in_string);
  char *result = brain_alloc_(length + 1, 0);
  if (!result)
    return NULL;
  result[length] = 0;
  for (int i = 0; i < length; i++) {
    result[i] = toupper(in_string[i]);
  }
  return result;
}


x86 assembly (clang, -O0):
<brain_strdup_upper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1152 <brain_strdup_upper+0x22>
lea    0xeb2(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
xor    %esi,%esi
call   1050 <brain_alloc_@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118a <brain_strdup_upper+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    11df <brain_strdup_upper+0xaf>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11d7 <brain_strdup_upper+0xa7>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119d <brain_strdup_upper+0x6d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brain_strdup_upper>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
lea    0xebe(%rip),%r14
cmovne %rdi,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
xor    %esi,%esi
call   1050 <brain_alloc_@plt>
mov    %rax,%r12
test   %rax,%rax
je     1187 <brain_strdup_upper+0x57>
movb   $0x0,(%r12,%r15,1)
test   %r15,%r15
jle    1187 <brain_strdup_upper+0x57>
xor    %ebx,%ebx
movsbl (%r14,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1170 <brain_strdup_upper+0x40>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @brain_strdup_upper(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %3
br label %10
10: ; preds = %9, %1
%11 = load i8*, i8** %3
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = add nsw i64 %13, 1
%15 = call i8* @brain_alloc_(i64 %14, i32 0)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %10
store i8* null, i8** %2
br label %44
19: ; preds = %10
%20 = load i8*, i8** %5
%21 = load i64, i64* %4
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
store i32 0, i32* %6
br label %23
23: ; preds = %39, %19
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = load i64, i64* %4
%27 = icmp slt i64 %25, %26
br i1 %27, label %28, label %42
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = call signext i8 @toupper(i8 signext %33)
%35 = load i8*, i8** %5
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
br label %39
39: ; preds = %28
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %23
42: ; preds = %23
%43 = load i8*, i8** %5
store i8* %43, i8** %2
br label %44
44: ; preds = %42, %18
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @brain_strdup_upper(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = add nsw i64 %4, 1
%6 = tail call i8* @brain_alloc_(i64 %5, i32 0)
%7 = icmp eq i8* %6, null
br i1 %7, label %19, label %8
8: ; preds = %1
%9 = getelementptr inbounds i8, i8* %6, i64 %4
store i8 0, i8* %9
%10 = icmp sgt i64 %4, 0
br i1 %10, label %11, label %19
11: ; preds = %8, %11
%12 = phi i64 [ %17, %11 ], [ 0, %8 ]
%13 = getelementptr inbounds i8, i8* %3, i64 %12
%14 = load i8, i8* %13
%15 = tail call signext i8 @toupper(i8 signext %14)
%16 = getelementptr inbounds i8, i8* %6, i64 %12
store i8 %15, i8* %16
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %4
br i1 %18, label %19, label %11
19: ; preds = %11, %8, %1
ret i8* %6
}