#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 */
unsigned char sm4Sbox(unsigned char);
unsigned long sm4CalciRK(unsigned long ka) {
  unsigned long bb = 0;
  unsigned long rk = 0;
  unsigned char a[4];
  unsigned char b[4];
  {
    (a)[(0)] = (unsigned char)((ka) >> 24);
    (a)[(0) + 1] = (unsigned char)((ka) >> 16);
    (a)[(0) + 2] = (unsigned char)((ka) >> 8);
    (a)[(0) + 3] = (unsigned char)((ka));
  }
  b[0] = sm4Sbox(a[0]);
  b[1] = sm4Sbox(a[1]);
  b[2] = sm4Sbox(a[2]);
  b[3] = sm4Sbox(a[3]);
  {
    (bb) = ((unsigned long)(b)[(0)] << 24) |
           ((unsigned long)(b)[(0) + 1] << 16) |
           ((unsigned long)(b)[(0) + 2] << 8) | ((unsigned long)(b)[(0) + 3]);
  }
  rk = bb ^ ((((((bb)) & 0xFFFFFFFF) << 13) | ((bb) >> (32 - 13)))) ^
       ((((((bb)) & 0xFFFFFFFF) << 23) | ((bb) >> (32 - 23))));
  return rk;
}


x86 assembly (clang, -O0):
<sm4CalciRK>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x18,%rax
mov    %al,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x10,%rax
mov    %al,-0x1b(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x8,%rax
mov    %al,-0x1a(%rbp)
mov    -0x8(%rbp),%rax
mov    %al,-0x19(%rbp)
movzbl -0x1c(%rbp),%edi
call   1030 <sm4Sbox@plt>
mov    %al,-0x20(%rbp)
movzbl -0x1b(%rbp),%edi
call   1030 <sm4Sbox@plt>
mov    %al,-0x1f(%rbp)
movzbl -0x1a(%rbp),%edi
call   1030 <sm4Sbox@plt>
mov    %al,-0x1e(%rbp)
movzbl -0x19(%rbp),%edi
call   1030 <sm4Sbox@plt>
mov    %al,-0x1d(%rbp)
movzbl -0x20(%rbp),%eax
shl    $0x18,%rax
movzbl -0x1f(%rbp),%ecx
shl    $0x10,%rcx
or     %rcx,%rax
movzbl -0x1e(%rbp),%ecx
shl    $0x8,%rcx
or     %rcx,%rax
movzbl -0x1d(%rbp),%ecx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movabs $0xffffffff,%rcx
and    -0x10(%rbp),%rcx
shl    $0xd,%rcx
mov    -0x10(%rbp),%rdx
shr    $0x13,%rdx
or     %rdx,%rcx
xor    %rcx,%rax
movabs $0xffffffff,%rcx
and    -0x10(%rbp),%rcx
shl    $0x17,%rcx
mov    -0x10(%rbp),%rdx
shr    $0x9,%rdx
or     %rdx,%rcx
xor    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sm4CalciRK>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %ebx,%eax
shr    $0x18,%eax
mov    %eax,%edi
call   1030 <sm4Sbox@plt>
mov    %eax,%ebp
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%edi
call   1030 <sm4Sbox@plt>
mov    %eax,%r14d
movzbl %bh,%edi
call   1030 <sm4Sbox@plt>
mov    %eax,%r15d
movzbl %bl,%edi
call   1030 <sm4Sbox@plt>
movzbl %bpl,%ecx
shl    $0x18,%rcx
movzbl %r14b,%edx
shl    $0x10,%rdx
or     %rcx,%rdx
movzbl %r15b,%ecx
shl    $0x8,%rcx
or     %rdx,%rcx
movzbl %al,%eax
or     %rcx,%rax
mov    %rax,%rsi
shl    $0xd,%rsi
shr    $0x13,%rdx
or     %rsi,%rdx
xor    %rax,%rdx
shl    $0x17,%rax
shr    $0x9,%rcx
or     %rax,%rcx
xor    %rdx,%rcx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @sm4CalciRK(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca [4 x i8]
%6 = alloca [4 x i8]
store i64 %0, i64* %2
store i64 0, i64* %3
store i64 0, i64* %4
%7 = load i64, i64* %2
%8 = lshr i64 %7, 24
%9 = trunc i64 %8 to i8
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
store i8 %9, i8* %10
%11 = load i64, i64* %2
%12 = lshr i64 %11, 16
%13 = trunc i64 %12 to i8
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
store i8 %13, i8* %14
%15 = load i64, i64* %2
%16 = lshr i64 %15, 8
%17 = trunc i64 %16 to i8
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
store i8 %17, i8* %18
%19 = load i64, i64* %2
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
store i8 %20, i8* %21
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%23 = load i8, i8* %22
%24 = call zeroext i8 @sm4Sbox(i8 zeroext %23)
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
store i8 %24, i8* %25
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
%27 = load i8, i8* %26
%28 = call zeroext i8 @sm4Sbox(i8 zeroext %27)
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
store i8 %28, i8* %29
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
%31 = load i8, i8* %30
%32 = call zeroext i8 @sm4Sbox(i8 zeroext %31)
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
store i8 %32, i8* %33
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
%35 = load i8, i8* %34
%36 = call zeroext i8 @sm4Sbox(i8 zeroext %35)
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
store i8 %36, i8* %37
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%39 = load i8, i8* %38
%40 = zext i8 %39 to i64
%41 = shl i64 %40, 24
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%43 = load i8, i8* %42
%44 = zext i8 %43 to i64
%45 = shl i64 %44, 16
%46 = or i64 %41, %45
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%48 = load i8, i8* %47
%49 = zext i8 %48 to i64
%50 = shl i64 %49, 8
%51 = or i64 %46, %50
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%53 = load i8, i8* %52
%54 = zext i8 %53 to i64
%55 = or i64 %51, %54
store i64 %55, i64* %3
%56 = load i64, i64* %3
%57 = load i64, i64* %3
%58 = and i64 %57, 4294967295
%59 = shl i64 %58, 13
%60 = load i64, i64* %3
%61 = lshr i64 %60, 19
%62 = or i64 %59, %61
%63 = xor i64 %56, %62
%64 = load i64, i64* %3
%65 = and i64 %64, 4294967295
%66 = shl i64 %65, 23
%67 = load i64, i64* %3
%68 = lshr i64 %67, 9
%69 = or i64 %66, %68
%70 = xor i64 %63, %69
store i64 %70, i64* %4
%71 = load i64, i64* %4
ret i64 %71
}

LLVM IR (-O2):
define i64 @sm4CalciRK(i64 %0) {
%2 = lshr i64 %0, 24
%3 = trunc i64 %2 to i8
%4 = lshr i64 %0, 16
%5 = trunc i64 %4 to i8
%6 = lshr i64 %0, 8
%7 = trunc i64 %6 to i8
%8 = trunc i64 %0 to i8
%9 = tail call zeroext i8 @sm4Sbox(i8 zeroext %3)
%10 = tail call zeroext i8 @sm4Sbox(i8 zeroext %5)
%11 = tail call zeroext i8 @sm4Sbox(i8 zeroext %7)
%12 = tail call zeroext i8 @sm4Sbox(i8 zeroext %8)
%13 = zext i8 %9 to i64
%14 = shl nuw nsw i64 %13, 24
%15 = zext i8 %10 to i64
%16 = shl nuw nsw i64 %15, 16
%17 = or i64 %16, %14
%18 = zext i8 %11 to i64
%19 = shl nuw nsw i64 %18, 8
%20 = or i64 %17, %19
%21 = zext i8 %12 to i64
%22 = or i64 %20, %21
%23 = shl nuw nsw i64 %22, 13
%24 = lshr i64 %17, 19
%25 = or i64 %23, %24
%26 = xor i64 %25, %22
%27 = shl nuw nsw i64 %22, 23
%28 = lshr i64 %20, 9
%29 = or i64 %27, %28
%30 = xor i64 %26, %29
ret i64 %30
}