unsigned short *generate_cube(x, y, z) {
  unsigned short *map;
  int i;
  map = (unsigned short *)malloc(16 * 16 * 16 * 2);
  for (i = 0; i < 16 * 16 * 16; i++) {
    map[i] = (i % 3);
  }
  return map;
}


x86 assembly (clang, -O0):
<generate_cube>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x2000,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1000,-0x1c(%rbp)
jge    1168 <generate_cube+0x58>
mov    -0x1c(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1136 <generate_cube+0x26>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_cube>:
push   %rax
mov    $0x2000,%edi
call   1030 <malloc@plt>
movdqa 0xedd(%rip),%xmm1
xor    %ecx,%ecx
movdqa 0xee2(%rip),%xmm8
movdqa 0xeea(%rip),%xmm2
movdqa 0xef2(%rip),%xmm3
movdqa 0xef9(%rip),%xmm9
movdqa 0xf01(%rip),%xmm5
movdqa 0xf09(%rip),%xmm6
nopw   0x0(%rax,%rax,1)
movdqa %xmm1,%xmm7
paddw  %xmm8,%xmm7
movdqa %xmm1,%xmm0
pmulhuw %xmm2,%xmm0
psrlw  $0x1,%xmm0
pmullw %xmm3,%xmm0
movdqa %xmm1,%xmm4
psubw  %xmm0,%xmm4
movdqa %xmm7,%xmm0
pmulhuw %xmm2,%xmm0
psrlw  $0x1,%xmm0
pmullw %xmm3,%xmm0
psubw  %xmm0,%xmm7
movdqu %xmm4,(%rax,%rcx,2)
movdqu %xmm7,0x10(%rax,%rcx,2)
movdqa %xmm1,%xmm0
paddw  %xmm9,%xmm0
movdqa %xmm1,%xmm4
paddw  %xmm5,%xmm4
movdqa %xmm0,%xmm7
pmulhuw %xmm2,%xmm7
psrlw  $0x1,%xmm7
pmullw %xmm3,%xmm7
psubw  %xmm7,%xmm0
movdqa %xmm4,%xmm7
pmulhuw %xmm2,%xmm7
psrlw  $0x1,%xmm7
pmullw %xmm3,%xmm7
psubw  %xmm7,%xmm4
movdqu %xmm0,0x20(%rax,%rcx,2)
movdqu %xmm4,0x30(%rax,%rcx,2)
add    $0x20,%rcx
paddw  %xmm6,%xmm1
cmp    $0x1000,%rcx
jne    1160 <generate_cube+0x50>
pop    %rcx
ret

LLVM IR (-O0):
define i16* @generate_cube(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16*
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = call i8* @malloc(i64 8192)
%10 = bitcast i8* %9 to i16*
store i16* %10, i16** %7
store i32 0, i32* %8
br label %11
11: ; preds = %22, %3
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 4096
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = load i32, i32* %8
%16 = srem i32 %15, 3
%17 = trunc i32 %16 to i16
%18 = load i16*, i16** %7
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i16, i16* %18, i64 %20
store i16 %17, i16* %21
br label %22
22: ; preds = %14
%23 = load i32, i32* %8
%24 = add nsw i32 %23, 1
store i32 %24, i32* %8
br label %11
25: ; preds = %11
%26 = load i16*, i16** %7
ret i16* %26
}

LLVM IR (-O2):
define noalias i16* @generate_cube(i32 %0, i32 %1, i32 %2) {
%4 = tail call dereferenceable_or_null(8192) i8* @malloc(i64 8192)
%5 = bitcast i8* %4 to i16*
br label %6
6: ; preds = %6, %3
%7 = phi i64 [ 0, %3 ], [ %25, %6 ]
%8 = phi <8 x i16> [ <i16 0, i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7>, %3 ], [ %26, %6 ]
%9 = add <8 x i16> %8, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%10 = urem <8 x i16> %8, <i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3>
%11 = urem <8 x i16> %9, <i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3>
%12 = getelementptr inbounds i16, i16* %5, i64 %7
%13 = bitcast i16* %12 to <8 x i16>*
store <8 x i16> %10, <8 x i16>* %13
%14 = getelementptr inbounds i16, i16* %12, i64 8
%15 = bitcast i16* %14 to <8 x i16>*
store <8 x i16> %11, <8 x i16>* %15
%16 = or i64 %7, 16
%17 = add <8 x i16> %8, <i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16>
%18 = add <8 x i16> %8, <i16 24, i16 24, i16 24, i16 24, i16 24, i16 24, i16 24, i16 24>
%19 = urem <8 x i16> %17, <i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3>
%20 = urem <8 x i16> %18, <i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3, i16 3>
%21 = getelementptr inbounds i16, i16* %5, i64 %16
%22 = bitcast i16* %21 to <8 x i16>*
store <8 x i16> %19, <8 x i16>* %22
%23 = getelementptr inbounds i16, i16* %21, i64 8
%24 = bitcast i16* %23 to <8 x i16>*
store <8 x i16> %20, <8 x i16>* %24
%25 = add nuw nsw i64 %7, 32
%26 = add <8 x i16> %8, <i16 32, i16 32, i16 32, i16 32, i16 32, i16 32, i16 32, i16 32>
%27 = icmp eq i64 %25, 4096
br i1 %27, label %28, label %6
28: ; preds = %6
ret i16* %5
}