#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 /*<<< orphan*/ die(char *);
int *malloc(int);
int *copy_array(int x[], int size) {
  int i;
  int *y;
  y = malloc(size * sizeof(int));
  if (y == NULL) {
    die("malloc fail.");
  }
  for (i = 0; i < size; i++) {
    y[i] = x[i];
  }
  return y;
}


x86 assembly (clang, -O0):
<copy_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1159 <copy_array+0x39>
lea    0xeac(%rip),%rdi
call   1040 <die@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1190 <copy_array+0x70>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1160 <copy_array+0x40>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_array>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x0(,%rbx,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     116c <copy_array+0x3c>
test   %ebx,%ebx
jle    1163 <copy_array+0x33>
mov    %ebx,%edx
shl    $0x2,%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <memcpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe8d(%rip),%rdi
call   1050 <die@plt>
test   %ebx,%ebx
jg     1152 <copy_array+0x22>
jmp    1163 <copy_array+0x33>

LLVM IR (-O0):
@.str = [13 x i8] c"malloc fail.\00"
define i32* @copy_array(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 4
%10 = trunc i64 %9 to i32
%11 = call i32* @malloc(i32 %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
store i32 0, i32* %5
br label %17
17: ; preds = %31, %16
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = load i32*, i32** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = load i32, i32* %25
%27 = load i32*, i32** %6
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 %26, i32* %30
br label %31
31: ; preds = %21
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %17
34: ; preds = %17
%35 = load i32*, i32** %6
ret i32* %35
}

LLVM IR (-O2):
@.str = [13 x i8] c"malloc fail.\00"
define noalias i32* @copy_array(i32* nocapture %0, i32 %1) {
%3 = bitcast i32* %0 to i8*
%4 = shl i32 %1, 2
%5 = tail call i32* @malloc(i32 %4)
%6 = bitcast i32* %5 to i8*
%7 = icmp eq i32* %5, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = icmp sgt i32 %1, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = zext i32 %1 to i64
%14 = shl nuw nsw i64 %13, 2
br label %15
15: ; preds = %12, %10
ret i32* %5
}