#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 *malloc(size_t);
size_t strlen(char const *);
scalar_t__ toupper(int);
void sf_strdup(char **out, const char *in) {
  size_t name_len = strlen(in);
  char *dup = malloc(name_len + 1);
  if (dup) {
    for (size_t i = 0; i < name_len; i += 1) {
      dup[i] = (char)toupper((int)in[i]);
    }
    dup[name_len] = '\0';
  }
  *out = dup;
}


x86 assembly (clang, -O0):
<sf_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ba <sf_strdup+0x8a>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ae <sf_strdup+0x7e>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1171 <sf_strdup+0x41>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sf_strdup>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     118e <sf_strdup+0x5e>
test   %r12,%r12
je     1188 <sf_strdup+0x58>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r15,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1170 <sf_strdup+0x40>
movb   $0x0,0x0(%r13,%r12,1)
mov    %r13,(%r14)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @sf_strdup(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8** %0, i8*** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %38
15: ; preds = %2
store i64 0, i64* %7
br label %16
16: ; preds = %31, %15
%17 = load i64, i64* %7
%18 = load i64, i64* %5
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = load i64, i64* %7
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = call i64 @toupper(i32 %25)
%27 = trunc i64 %26 to i8
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 %27, i8* %30
br label %31
31: ; preds = %20
%32 = load i64, i64* %7
%33 = add i64 %32, 1
store i64 %33, i64* %7
br label %16
34: ; preds = %16
%35 = load i8*, i8** %6
%36 = load i64, i64* %5
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %34, %2
%39 = load i8*, i8** %6
%40 = load i8**, i8*** %3
store i8* %39, i8** %40
ret void
}

LLVM IR (-O2):
define void @sf_strdup(i8** nocapture writeonly %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = add i64 %3, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %21, label %7
7: ; preds = %2
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %11
9: ; preds = %11, %7
%10 = getelementptr inbounds i8, i8* %5, i64 %3
store i8 0, i8* %10
br label %21
11: ; preds = %7, %11
%12 = phi i64 [ %19, %11 ], [ 0, %7 ]
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = tail call i64 @toupper(i32 %15)
%17 = trunc i64 %16 to i8
%18 = getelementptr inbounds i8, i8* %5, i64 %12
store i8 %17, i8* %18
%19 = add nuw i64 %12, 1
%20 = icmp eq i64 %19, %3
br i1 %20, label %9, label %11
21: ; preds = %9, %2
store i8* %5, i8** %0
ret void
}