#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 short prbs15_next_7_bytes(unsigned short, unsigned char *, int);
unsigned short reverse_bit16(unsigned short);
unsigned short prbs15_gen(unsigned short polynomial, unsigned short seed,
                          unsigned char *buf, int length, int reverse_output) {
  unsigned short seed_rev;
  if ((0) == polynomial) {
    polynomial = 0xC001;
  };
  seed_rev = reverse_bit16(seed);
  seed_rev >>= 1;
  while (length > 0) {
    if (length >= (7)) {
      seed_rev = prbs15_next_7_bytes(seed_rev, buf, reverse_output);
      buf += (7);
      length -= (7);
    } else {
      int i;
      unsigned char cur_prbs_buf[(7)];
      seed_rev = prbs15_next_7_bytes(seed_rev, cur_prbs_buf, reverse_output);
      for (i = 0; i < length; i++) {
        buf[i] = cur_prbs_buf[i];
      }
      length = 0;
    }
  }
  seed = reverse_bit16(seed_rev);
  seed >>= 1;
  return seed;
}


x86 assembly (clang, -O0):
<prbs15_gen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %di,%si
mov    %si,-0x2(%rbp)
mov    %ax,-0x4(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
movzwl -0x2(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1155 <prbs15_gen+0x35>
movw   $0xc001,-0x2(%rbp)
movzwl -0x4(%rbp),%edi
call   1040 <reverse_bit16@plt>
mov    %ax,-0x1a(%rbp)
movzwl -0x1a(%rbp),%eax
sar    $0x1,%eax
mov    %ax,-0x1a(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1209 <prbs15_gen+0xe9>
cmpl   $0x7,-0x14(%rbp)
jl     11b2 <prbs15_gen+0x92>
mov    -0x1a(%rbp),%ax
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
movzwl %ax,%edi
call   1030 <prbs15_next_7_bytes@plt>
mov    %ax,-0x1a(%rbp)
mov    -0x10(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x7,%eax
mov    %eax,-0x14(%rbp)
jmp    1204 <prbs15_gen+0xe4>
mov    -0x1a(%rbp),%ax
lea    -0x27(%rbp),%rsi
mov    -0x18(%rbp),%edx
movzwl %ax,%edi
call   1030 <prbs15_next_7_bytes@plt>
mov    %ax,-0x1a(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11fd <prbs15_gen+0xdd>
movslq -0x20(%rbp),%rax
mov    -0x27(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11d0 <prbs15_gen+0xb0>
movl   $0x0,-0x14(%rbp)
jmp    116d <prbs15_gen+0x4d>
movzwl -0x1a(%rbp),%edi
call   1040 <reverse_bit16@plt>
mov    %ax,-0x4(%rbp)
movzwl -0x4(%rbp),%eax
sar    $0x1,%eax
mov    %ax,-0x4(%rbp)
movzwl -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prbs15_gen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%edi
call   1040 <reverse_bit16@plt>
movzwl %ax,%eax
shr    %eax
test   %ebp,%ebp
jle    11a3 <prbs15_gen+0x83>
mov    %ebp,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzwl %ax,%edi
cmp    $0x6,%ebp
jbe    1172 <prbs15_gen+0x52>
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <prbs15_next_7_bytes@plt>
add    $0x7,%r15
add    $0xfffffffffffffff9,%rbx
add    $0xfffffff9,%ebp
jne    1150 <prbs15_gen+0x30>
jmp    11a3 <prbs15_gen+0x83>
lea    0x1(%rsp),%rsi
mov    %r14d,%edx
call   1030 <prbs15_next_7_bytes@plt>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rsp,%rcx,1),%edx
mov    %dl,(%r15,%rcx,1)
add    $0x1,%rcx
add    $0xffffffffffffffff,%rbx
jne    1190 <prbs15_gen+0x70>
movzwl %ax,%edi
call   1040 <reverse_bit16@plt>
movzwl %ax,%eax
shr    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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