#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
int ClockFSM(int /*<<< orphan*/ *);
int /*<<< orphan*/ ClockLFSRKeyStreamMode(int /*<<< orphan*/ *);
void Encrypt(uint8_t *state, uint8_t *stream, uint16_t length) {
  uint32_t *LFSR = (uint32_t *)state;
  uint32_t t;
  uint32_t F;
  uint32_t ks;
  uint32_t *stream32 = (uint32_t *)stream;
  uint32_t *FSM_R1 = ((uint32_t *)state) + 16;
  uint32_t *FSM_R2 = FSM_R1 + 1;
  ClockFSM(state);
  ClockLFSRKeyStreamMode(state);
  for (t = 0; t < length / 4 - 1; t++) {
    F = ClockFSM(state);
    ks = (F ^ LFSR[0]);
    *(stream32 + t) = ks;
    ClockLFSRKeyStreamMode(state);
  }
  F = ((LFSR[15] + *FSM_R1)) ^ *FSM_R2;
  ks = (F ^ LFSR[0]);
  *(stream32 + t) = ks;
}


x86 assembly (clang, -O0):
<Encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <ClockFSM@plt>
mov    -0x8(%rbp),%rdi
call   1040 <ClockLFSRKeyStreamMode@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11d2 <Encrypt+0xb2>
mov    -0x8(%rbp),%rdi
call   1030 <ClockFSM@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
xor    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
call   1040 <ClockLFSRKeyStreamMode@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1174 <Encrypt+0x54>
mov    -0x20(%rbp),%rax
mov    0x3c(%rax),%eax
mov    -0x40(%rbp),%rcx
add    (%rcx),%eax
mov    -0x48(%rbp),%rcx
xor    (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
xor    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Encrypt>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <ClockFSM@plt>
mov    %r12,%rdi
call   1040 <ClockLFSRKeyStreamMode@plt>
cmp    $0x8,%r15d
jl     1173 <Encrypt+0x53>
shr    $0x2,%r15d
add    $0xffffffff,%r15d
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r12,%rdi
call   1030 <ClockFSM@plt>
xor    (%r12),%eax
mov    %eax,(%r14,%rbx,4)
mov    %r12,%rdi
call   1040 <ClockLFSRKeyStreamMode@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <Encrypt+0x30>
jmp    1176 <Encrypt+0x56>
xor    %r15d,%r15d
mov    0x40(%r12),%eax
add    0x3c(%r12),%eax
xor    0x44(%r12),%eax
xor    (%r12),%eax
mov    %eax,(%r14,%r15,4)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @Encrypt(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%14 = load i32*, i32** %4
store i32* %14, i32** %7
%15 = load i32*, i32** %5
store i32* %15, i32** %11
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i64 16
store i32* %17, i32** %12
%18 = load i32*, i32** %12
%19 = getelementptr inbounds i32, i32* %18, i64 1
store i32* %19, i32** %13
%20 = load i32*, i32** %4
%21 = call i32 @ClockFSM(i32* %20)
%22 = load i32*, i32** %4
%23 = call i32 @ClockLFSRKeyStreamMode(i32* %22)
store i32 0, i32* %8
br label %24
24: ; preds = %45, %3
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = sdiv i32 %26, 4
%28 = sub nsw i32 %27, 1
%29 = icmp slt i32 %25, %28
br i1 %29, label %30, label %48
30: ; preds = %24
%31 = load i32*, i32** %4
%32 = call i32 @ClockFSM(i32* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = load i32*, i32** %7
%35 = getelementptr inbounds i32, i32* %34, i64 0
%36 = load i32, i32* %35
%37 = xor i32 %33, %36
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = load i32*, i32** %11
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
store i32 %38, i32* %42
%43 = load i32*, i32** %4
%44 = call i32 @ClockLFSRKeyStreamMode(i32* %43)
br label %45
45: ; preds = %30
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %24
48: ; preds = %24
%49 = load i32*, i32** %7
%50 = getelementptr inbounds i32, i32* %49, i64 15
%51 = load i32, i32* %50
%52 = load i32*, i32** %12
%53 = load i32, i32* %52
%54 = add nsw i32 %51, %53
%55 = load i32*, i32** %13
%56 = load i32, i32* %55
%57 = xor i32 %54, %56
store i32 %57, i32* %9
%58 = load i32, i32* %9
%59 = load i32*, i32** %7
%60 = getelementptr inbounds i32, i32* %59, i64 0
%61 = load i32, i32* %60
%62 = xor i32 %58, %61
store i32 %62, i32* %10
%63 = load i32, i32* %10
%64 = load i32*, i32** %11
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i32, i32* %64, i64 %66
store i32 %63, i32* %67
ret void
}

LLVM IR (-O2):
define void @Encrypt(i32* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = tail call i32 @ClockFSM(i32* %0)
%5 = tail call i32 @ClockLFSRKeyStreamMode(i32* %0)
%6 = icmp sgt i32 %2, 7
br i1 %6, label %7, label %22
7: ; preds = %3
%8 = lshr i32 %2, 2
%9 = add nsw i32 %8, -1
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %7, %11
%12 = phi i64 [ 0, %7 ], [ %18, %11 ]
%13 = tail call i32 @ClockFSM(i32* %0)
%14 = load i32, i32* %0
%15 = xor i32 %14, %13
%16 = getelementptr inbounds i32, i32* %1, i64 %12
store i32 %15, i32* %16
%17 = tail call i32 @ClockLFSRKeyStreamMode(i32* %0)
%18 = add nuw nsw i64 %12, 1
%19 = icmp eq i64 %18, %10
br i1 %19, label %20, label %11
20: ; preds = %11
%21 = zext i32 %9 to i64
br label %22
22: ; preds = %3, %20
%23 = phi i64 [ %21, %20 ], [ 0, %3 ]
%24 = getelementptr inbounds i32, i32* %0, i64 16
%25 = getelementptr inbounds i32, i32* %0, i64 17
%26 = getelementptr inbounds i32, i32* %0, i64 15
%27 = load i32, i32* %26
%28 = load i32, i32* %24
%29 = add nsw i32 %28, %27
%30 = load i32, i32* %25
%31 = xor i32 %29, %30
%32 = load i32, i32* %0
%33 = xor i32 %31, %32
%34 = getelementptr inbounds i32, i32* %1, i64 %23
store i32 %33, i32* %34
ret void
}