#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(int);
char *alloc_and_copy(const char *src, int alloc_size) {
  int i;
  char *dest;
  dest = malloc(alloc_size);
  i = -1;
  while (src[++i] != '\0')
    dest[i] = src[i];
  dest[i] = '\0';
  return (dest);
}


x86 assembly (clang, -O0):
<alloc_and_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x10(%rbp)
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     116a <alloc_and_copy+0x5a>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1132 <alloc_and_copy+0x22>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_and_copy>:
push   %rbx
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <malloc@plt>
mov    (%rbx),%dl
xor    %ecx,%ecx
test   %dl,%dl
je     1140 <alloc_and_copy+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %dl,(%rax,%rcx,1)
movzbl 0x1(%rbx,%rcx,1),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    1130 <alloc_and_copy+0x20>
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
ret

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

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