#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 uint8_t;

/* Variables and functions */
int READ_ROUND_KEY_BYTE(int);
void AddKey(uint8_t state[4][4], uint8_t *keyBytes, uint8_t half) {
  uint8_t i, j;
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      state[i][j] ^=
          READ_ROUND_KEY_BYTE(keyBytes[(4 * i + j + half * 16) % 20]);
    }
  }
}


x86 assembly (clang, -O0):
<AddKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    11b1 <AddKey+0xa1>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    119e <AddKey+0x8e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x2,%eax
add    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x4,%ecx
add    %ecx,%eax
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <READ_ROUND_KEY_BYTE@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
movslq -0x1c(%rbp),%rcx
xor    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113b <AddKey+0x2b>
jmp    11a3 <AddKey+0x93>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112a <AddKey+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AddKey>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
shl    $0x4,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%r12,%rbx,1),%rax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
shl    $0x2,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cltq
mov    (%r14,%rax,4),%edi
call   1030 <READ_ROUND_KEY_BYTE@plt>
xor    %eax,(%r15,%rbx,4)
lea    (%r12,%rbx,1),%rax
add    $0x1,%rax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
shl    $0x2,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cltq
mov    (%r14,%rax,4),%edi
call   1030 <READ_ROUND_KEY_BYTE@plt>
xor    %eax,0x4(%r15,%rbx,4)
lea    (%r12,%rbx,1),%rax
add    $0x2,%rax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
shl    $0x2,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cltq
mov    (%r14,%rax,4),%edi
call   1030 <READ_ROUND_KEY_BYTE@plt>
xor    %eax,0x8(%r15,%rbx,4)
lea    (%r12,%rbx,1),%rax
add    $0x3,%rax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
shl    $0x2,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cltq
mov    (%r14,%rax,4),%edi
call   1030 <READ_ROUND_KEY_BYTE@plt>
xor    %eax,0xc(%r15,%rbx,4)
add    $0x4,%rbx
cmp    $0x10,%rbx
jne    1130 <AddKey+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @AddKey([4 x i32]* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = shl nsw i32 %2, 4
br label %5
5: ; preds = %3, %5
%6 = phi i64 [ 0, %3 ], [ %45, %5 ]
%7 = trunc i64 %6 to i32
%8 = shl i32 %7, 2
%9 = add i32 %8, %4
%10 = srem i32 %9, 20
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %1, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 @READ_ROUND_KEY_BYTE(i32 %13)
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %0, i64 %6, i64 0
%16 = load i32, i32* %15
%17 = xor i32 %16, %14
store i32 %17, i32* %15
%18 = or i32 %9, 1
%19 = srem i32 %18, 20
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %1, i64 %20
%22 = load i32, i32* %21
%23 = tail call i32 @READ_ROUND_KEY_BYTE(i32 %22)
%24 = getelementptr inbounds [4 x i32], [4 x i32]* %0, i64 %6, i64 1
%25 = load i32, i32* %24
%26 = xor i32 %25, %23
store i32 %26, i32* %24
%27 = or i32 %9, 2
%28 = srem i32 %27, 20
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %1, i64 %29
%31 = load i32, i32* %30
%32 = tail call i32 @READ_ROUND_KEY_BYTE(i32 %31)
%33 = getelementptr inbounds [4 x i32], [4 x i32]* %0, i64 %6, i64 2
%34 = load i32, i32* %33
%35 = xor i32 %34, %32
store i32 %35, i32* %33
%36 = or i32 %9, 3
%37 = srem i32 %36, 20
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %1, i64 %38
%40 = load i32, i32* %39
%41 = tail call i32 @READ_ROUND_KEY_BYTE(i32 %40)
%42 = getelementptr inbounds [4 x i32], [4 x i32]* %0, i64 %6, i64 3
%43 = load i32, i32* %42
%44 = xor i32 %43, %41
store i32 %44, i32* %42
%45 = add nuw nsw i64 %6, 1
%46 = icmp eq i64 %45, 4
br i1 %46, label %47, label %5
47: ; preds = %5
ret void
}