#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*/ buf;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ atoi(char *const);
int /*<<< orphan*/ close(int);
unsigned short get_lfsr(int /*<<< orphan*/, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *const, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, unsigned short *, int);
int main(int argc, char *const argv[]) {
  unsigned short lfsr, buf;
  int fd, i;
  if (argc != 4) {
    printf("Usage : %s target_file isdb tuner_no\n", argv[0]);
    printf("\tisdb\t\tS=0 T=1\n");
    printf("\ttuner_no\t0 or 1\n");
    return EXIT_SUCCESS;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd <= 0) {
    printf("can not open file %s.\n", argv[1]);
    return EXIT_SUCCESS;
  }
  lfsr = get_lfsr(atoi(argv[2]), atoi(argv[3]));
  while (read(fd, &buf, sizeof(buf))) {
    if (buf != lfsr) {
      printf("check NG! 0x%x\n", lseek(fd, 0, SEEK_CUR));
      goto last;
    }
    lfsr = (lfsr >> 1) ^ (-(lfsr & 1) & 0xb400);
  }
  printf("check OK.\n");
last:
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11d2 <main+0x62>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12df <main+0x16f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     121c <main+0xac>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12df <main+0x16f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
call   1070 <get_lfsr@plt>
mov    %ax,-0x12(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
je     12bd <main+0x14d>
movzwl -0x14(%rbp),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
je     129a <main+0x12a>
mov    -0x18(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12cb <main+0x15b>
movzwl -0x12(%rbp),%eax
sar    $0x1,%eax
movzwl -0x12(%rbp),%edx
and    $0x1,%edx
xor    %ecx,%ecx
sub    %edx,%ecx
and    $0xb400,%ecx
xor    %ecx,%eax
mov    %ax,-0x12(%rbp)
jmp    1247 <main+0xd7>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x4,%edi
jne    122b <main+0xab>
mov    0x8(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
jle    1257 <main+0xd7>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    0x18(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1080 <get_lfsr@plt>
mov    %eax,%ebp
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1070 <read@plt>
test   %rax,%rax
je     121d <main+0x9d>
lea    0xe(%rsp),%r14
nopl   0x0(%rax)
cmp    %bp,0xe(%rsp)
jne    126b <main+0xeb>
movzwl %bp,%eax
shr    %eax
and    $0x1,%ebp
neg    %ebp
and    $0xffffb400,%ebp
xor    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1070 <read@plt>
test   %rax,%rax
jne    11f0 <main+0x70>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
jmp    128d <main+0x10d>
mov    0x0(%rbp),%rsi
lea    0xdca(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe13(%rip),%rdi
call   1030 <puts@plt>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
jmp    1294 <main+0x114>
mov    0x8(%rbp),%rsi
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1294 <main+0x114>
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xdb9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Usage : %s target_file isdb tuner_no\0A\00"
@.str.1 = [16 x i8] c"\09isdb\09\09S=0 T=1\0A\00"
@.str.2 = [18 x i8] c"\09tuner_no\090 or 1\0A\00"
@EXIT_SUCCESS = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [23 x i8] c"can not open file %s.\0A\00"
@.str.4 = [16 x i8] c"check NG! 0x%x\0A\00"
@SEEK_CUR = global i32 0
@.str.5 = [11 x i8] c"check OK.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i16
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 4
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_SUCCESS
store i32 %19, i32* %3
br label %76
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_SUCCESS
store i32 %33, i32* %3
br label %76
34: ; preds = %20
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
%43 = call zeroext i16 @get_lfsr(i32 %38, i32 %42)
store i16 %43, i16* %6
br label %44
44: ; preds = %59, %34
%45 = load i32, i32* %8
%46 = call i64 @read(i32 %45, i16* %7, i32 2)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %70
48: ; preds = %44
%49 = load i16, i16* %7
%50 = zext i16 %49 to i32
%51 = load i16, i16* %6
%52 = zext i16 %51 to i32
%53 = icmp ne i32 %50, %52
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i32, i32* %8
%56 = load i32, i32* @SEEK_CUR
%57 = call i32 @lseek(i32 %55, i32 0, i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %57)
br label %72
59: ; preds = %48
%60 = load i16, i16* %6
%61 = zext i16 %60 to i32
%62 = ashr i32 %61, 1
%63 = load i16, i16* %6
%64 = zext i16 %63 to i32
%65 = and i32 %64, 1
%66 = sub nsw i32 0, %65
%67 = and i32 %66, 46080
%68 = xor i32 %62, %67
%69 = trunc i32 %68 to i16
store i16 %69, i16* %6
br label %44
70: ; preds = %44
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %72
72: ; preds = %70, %54
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* @EXIT_SUCCESS
store i32 %75, i32* %3
br label %76
76: ; preds = %72, %28, %12
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [38 x i8] c"Usage : %s target_file isdb tuner_no\0A\00"
@EXIT_SUCCESS = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [23 x i8] c"can not open file %s.\0A\00"
@.str.4 = [16 x i8] c"check NG! 0x%x\0A\00"
@SEEK_CUR = global i32 0
@str = [10 x i8] c"check OK.\00"
@str.6 = [15 x i8] c"\09isdb\09\09S=0 T=1\00"
@str.7 = [17 x i8] c"\09tuner_no\090 or 1\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
%5 = icmp eq i32 %0, 4
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
br label %50
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i8*, i8** %12
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %18)
br label %50
20: ; preds = %11
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i32 @atoi(i8* %25)
%27 = tail call zeroext i16 @get_lfsr(i32 %23, i32 %26)
%28 = call i64 @read(i32 %15, i16* %3, i32 2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %46, label %30
30: ; preds = %20, %38
%31 = phi i16 [ %43, %38 ], [ %27, %20 ]
%32 = load i16, i16* %3
%33 = icmp eq i16 %32, %31
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i32, i32* @SEEK_CUR
%36 = tail call i32 @lseek(i32 %15, i32 0, i32 %35)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %48
38: ; preds = %30
%39 = lshr i16 %31, 1
%40 = and i16 %31, 1
%41 = sub nsw i16 0, %40
%42 = and i16 %41, -19456
%43 = xor i16 %42, %39
%44 = call i64 @read(i32 %15, i16* %3, i32 2)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %30
46: ; preds = %38, %20
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %46, %34
%49 = tail call i32 @close(i32 %15)
br label %50
50: ; preds = %48, %17, %6
%51 = load i32, i32* @EXIT_SUCCESS
ret i32 %51
}