#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 *galloc(int);
unsigned int str_length(char const *);
char *str_new(const char *s) {
  char *mem = galloc(str_length(s) + 1);
  for (unsigned i = 0; i <= str_length(s); i++)
    mem[i] = s[i];
  return mem;
}


x86 assembly (clang, -O0):
<str_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <str_length@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1040 <galloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <str_length@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
ja     1188 <str_new+0x68>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114a <str_new+0x2a>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_new>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <str_length@plt>
lea    0x1(%rax),%edi
call   1040 <galloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <str_length@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
movzbl (%rbx,%rax,1),%ecx
mov    %cl,(%r14,%rax,1)
add    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <str_length@plt>
cmp    %eax,%ebp
jbe    1150 <str_new+0x30>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @str_new(i8* %0) {
%2 = tail call i32 @str_length(i8* %0)
%3 = add i32 %2, 1
%4 = tail call i8* @galloc(i32 %3)
%5 = tail call i32 @str_length(i8* %0)
br label %7
6: ; preds = %7
ret i8* %4
7: ; preds = %1, %7
%8 = phi i32 [ 0, %1 ], [ %13, %7 ]
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = getelementptr inbounds i8, i8* %4, i64 %9
store i8 %11, i8* %12
%13 = add i32 %8, 1
%14 = tail call i32 @str_length(i8* %0)
%15 = icmp ugt i32 %13, %14
br i1 %15, label %6, label %7
}