#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ copy_block_buffer(long, long, long const, long const, int,
                                     int, int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ decode_etc2a1_block(int /*<<< orphan*/ const *,
                                       int /*<<< orphan*/ *);
int decode_etc2a1(const uint8_t *data, const long w, const long h,
                  uint32_t *image) {
  long num_blocks_x = (w + 3) / 4;
  long num_blocks_y = (h + 3) / 4;
  uint32_t buffer[16];
  for (long by = 0; by < num_blocks_y; by++) {
    for (long bx = 0; bx < num_blocks_x; bx++, data += 8) {
      decode_etc2a1_block(data, buffer);
      copy_block_buffer(bx, by, w, h, 4, 4, buffer, image);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<decode_etc2a1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    $0x4,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    $0x4,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    1202 <decode_etc2a1+0xe2>
movq   $0x0,-0x80(%rbp)
mov    -0x80(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    11ec <decode_etc2a1+0xcc>
mov    -0x8(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1040 <decode_etc2a1_block@plt>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x70(%rbp),%r10
mov    -0x20(%rbp),%rax
mov    $0x4,%r9d
mov    %r9d,%r8d
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <copy_block_buffer@plt>
mov    -0x80(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x80(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x8(%rbp)
jmp    1185 <decode_etc2a1+0x65>
jmp    11f1 <decode_etc2a1+0xd1>
mov    -0x78(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
jmp    116f <decode_etc2a1+0x4f>
mov    $0x1,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_etc2a1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
test   %rdx,%rdx
jle    11e3 <decode_etc2a1+0xc3>
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x3(%rdx),%rax
shr    $0x2,%rax
mov    %rax,0x8(%rsp)
lea    0x3(%rsi),%rax
lea    0x6(%rsi),%rdi
test   %rax,%rax
cmovns %rax,%rdi
sar    $0x2,%rdi
cmp    $0x2,%rdi
mov    $0x1,%r13d
cmovge %rdi,%r13
xor    %r12d,%r12d
lea    0x20(%rsp),%r15
mov    %rdx,0x18(%rsp)
mov    %rcx,0x10(%rsp)
jmp    118b <decode_etc2a1+0x6b>
nop
add    $0x1,%r12
cmp    0x8(%rsp),%r12
je     11e3 <decode_etc2a1+0xc3>
test   %rbp,%rbp
jle    1180 <decode_etc2a1+0x60>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <decode_etc2a1_block@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
mov    0x18(%rsp),%rcx
mov    $0x4,%r8d
mov    $0x4,%r9d
push   0x10(%rsp)
push   %r15
call   1030 <copy_block_buffer@plt>
add    $0x10,%rsp
add    $0x1,%r14
add    $0x20,%rbx
cmp    %r14,%r13
jne    11a0 <decode_etc2a1+0x80>
jmp    1180 <decode_etc2a1+0x60>
mov    $0x1,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @decode_etc2a1(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca [16 x i32]
%12 = alloca i64
%13 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%14 = load i64, i64* %6
%15 = add nsw i64 %14, 3
%16 = sdiv i64 %15, 4
store i64 %16, i64* %9
%17 = load i64, i64* %7
%18 = add nsw i64 %17, 3
%19 = sdiv i64 %18, 4
store i64 %19, i64* %10
store i64 0, i64* %12
br label %20
20: ; preds = %46, %4
%21 = load i64, i64* %12
%22 = load i64, i64* %10
%23 = icmp slt i64 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %20
store i64 0, i64* %13
br label %25
25: ; preds = %40, %24
%26 = load i64, i64* %13
%27 = load i64, i64* %9
%28 = icmp slt i64 %26, %27
br i1 %28, label %29, label %45
29: ; preds = %25
%30 = load i32*, i32** %5
%31 = getelementptr inbounds [16 x i32], [16 x i32]* %11, i64 0, i64 0
%32 = call i32 @decode_etc2a1_block(i32* %30, i32* %31)
%33 = load i64, i64* %13
%34 = load i64, i64* %12
%35 = load i64, i64* %6
%36 = load i64, i64* %7
%37 = getelementptr inbounds [16 x i32], [16 x i32]* %11, i64 0, i64 0
%38 = load i32*, i32** %8
%39 = call i32 @copy_block_buffer(i64 %33, i64 %34, i64 %35, i64 %36, i32 4, i32 4, i32* %37, i32* %38)
br label %40
40: ; preds = %29
%41 = load i64, i64* %13
%42 = add nsw i64 %41, 1
store i64 %42, i64* %13
%43 = load i32*, i32** %5
%44 = getelementptr inbounds i32, i32* %43, i64 8
store i32* %44, i32** %5
br label %25
45: ; preds = %25
br label %46
46: ; preds = %45
%47 = load i64, i64* %12
%48 = add nsw i64 %47, 1
store i64 %48, i64* %12
br label %20
49: ; preds = %20
ret i32 1
}

LLVM IR (-O2):
define i32 @decode_etc2a1(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca [16 x i32]
%6 = bitcast [16 x i32]* %5 to i8*
%7 = icmp sgt i64 %2, 0
br i1 %7, label %8, label %19
8: ; preds = %4
%9 = add nuw nsw i64 %2, 3
%10 = lshr i64 %9, 2
%11 = add nsw i64 %1, 3
%12 = sdiv i64 %11, 4
%13 = icmp sgt i64 %1, 0
%14 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 0
%15 = call i64 @llvm.smax.i64(i64 %12, i64 1)
br label %16
16: ; preds = %8, %20
%17 = phi i64 [ 0, %8 ], [ %22, %20 ]
%18 = phi i32* [ %0, %8 ], [ %21, %20 ]
br i1 %13, label %24, label %20
19: ; preds = %20, %4
ret i32 1
20: ; preds = %24, %16
%21 = phi i32* [ %18, %16 ], [ %30, %24 ]
%22 = add nuw nsw i64 %17, 1
%23 = icmp eq i64 %22, %10
br i1 %23, label %19, label %16
24: ; preds = %16, %24
%25 = phi i64 [ %29, %24 ], [ 0, %16 ]
%26 = phi i32* [ %30, %24 ], [ %18, %16 ]
%27 = call i32 @decode_etc2a1_block(i32* %26, i32* %14)
%28 = call i32 @copy_block_buffer(i64 %25, i64 %17, i64 %1, i64 %2, i32 4, i32 4, i32* %14, i32* %3)
%29 = add nuw nsw i64 %25, 1
%30 = getelementptr inbounds i32, i32* %26, i64 8
%31 = icmp eq i64 %29, %15
br i1 %31, label %20, label %24
}