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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fib(int);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
char reverse_bits(char, int);
void decrypt_sku(void) {
  FILE *sku17;
  FILE *skutestout;
  char sku_buffer[1024];
  int num_bytes_read;
  if ((sku17 = fopen("sku.17", "r")) == NULL) {
    printf("couldn't open input file sku.17\n");
    return;
  }
  if ((skutestout = fopen("sku.testout", "w")) == NULL) {
    printf("couldn't open output file sku.testout\n");
    return;
  }
  while (num_bytes_read = fread(sku_buffer, 1, 1024, sku17)) {
    int current_byte;
    for (current_byte = 1; current_byte < num_bytes_read; ++current_byte) {
      sku_buffer[current_byte] = sku_buffer[current_byte - 1] ^
                                 sku_buffer[current_byte] ^
                                 (fib(current_byte) & 0xFF);
    }
    sku_buffer[0] = reverse_bits(sku_buffer[0], 8);
    fwrite(sku_buffer, num_bytes_read, 1, skutestout);
  }
  fclose(sku17);
  fclose(skutestout);
  return;
}


x86 assembly (clang, -O0):
<decrypt_sku>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11ad <decrypt_sku+0x3d>
lea    0xe66(%rip),%rdi
call   1060 <printf@plt>
jmp    12db <decrypt_sku+0x16b>
lea    0xe76(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11df <decrypt_sku+0x6f>
lea    0xe63(%rip),%rdi
call   1060 <printf@plt>
jmp    12db <decrypt_sku+0x16b>
jmp    11e4 <decrypt_sku+0x74>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
je     12c9 <decrypt_sku+0x159>
movl   $0x1,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    1292 <decrypt_sku+0x122>
mov    -0x418(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
movslq -0x418(%rbp),%rcx
movsbl -0x410(%rbp,%rcx,1),%ecx
ff
xor    %ecx,%eax
mov    %eax,-0x41c(%rbp)
mov    -0x418(%rbp),%edi
call   1070 <fib@plt>
mov    %eax,%ecx
mov    -0x41c(%rbp),%eax
and    $0xff,%ecx
xor    %ecx,%eax
mov    %al,%cl
movslq -0x418(%rbp),%rax
mov    %cl,-0x410(%rbp,%rax,1)
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    1217 <decrypt_sku+0xa7>
mov    $0x8,%esi
movsbl -0x410(%rbp),%edi
call   1050 <reverse_bits@plt>
mov    %al,-0x410(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    11e4 <decrypt_sku+0x74>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt_sku>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xebb(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     129f <decrypt_sku+0x12f>
mov    %rax,%r12
lea    0xe77(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12a8 <decrypt_sku+0x138>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     127e <decrypt_sku+0x10e>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %r14,0x8(%rsp)
jmp    1234 <decrypt_sku+0xc4>
nopl   0x0(%rax)
mov    0x10(%rsp),%r15b
movsbl %r15b,%edi
mov    $0x8,%esi
call   1060 <reverse_bits@plt>
mov    %al,0x10(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
je     127e <decrypt_sku+0x10e>
cmp    $0x1,%ebp
jle    11f0 <decrypt_sku+0x80>
mov    %ebp,%r14d
mov    $0x1,%ebx
mov    0x10(%rsp),%r15b
mov    %r15d,%r13d
nopl   0x0(%rax)
xor    0x10(%rsp,%rbx,1),%r13b
mov    %ebx,%edi
call   1070 <fib@plt>
xor    %r13b,%al
mov    %al,0x10(%rsp,%rbx,1)
add    $0x1,%rbx
mov    %eax,%r13d
cmp    %rbx,%r14
jne    1250 <decrypt_sku+0xe0>
mov    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
jmp    11f5 <decrypt_sku+0x85>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xd84(%rip),%rdi
jmp    12af <decrypt_sku+0x13f>
lea    0xd55(%rip),%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"sku.17\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"couldn't open input file sku.17\0A\00"
@.str.3 = [12 x i8] c"sku.testout\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [39 x i8] c"couldn't open output file sku.testout\0A\00"
define void @decrypt_sku() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %1
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %64
10: ; preds = %0
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %11, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %64
15: ; preds = %10
br label %16
16: ; preds = %50, %15
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %1
%19 = call i32 @fread(i8* %17, i32 1, i32 1024, i32* %18)
store i32 %19, i32* %4
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %59
21: ; preds = %16
store i32 1, i32* %5
br label %22
22: ; preds = %47, %21
%23 = load i32, i32* %5
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %50
26: ; preds = %22
%27 = load i32, i32* %5
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = xor i32 %32, %37
%39 = load i32, i32* %5
%40 = call i32 @fib(i32 %39)
%41 = and i32 %40, 255
%42 = xor i32 %38, %41
%43 = trunc i32 %42 to i8
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %45
store i8 %43, i8* %46
br label %47
47: ; preds = %26
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
br label %22
50: ; preds = %22
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%52 = load i8, i8* %51
%53 = call signext i8 @reverse_bits(i8 signext %52, i32 8)
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 %53, i8* %54
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%56 = load i32, i32* %4
%57 = load i32*, i32** %2
%58 = call i32 @fwrite(i8* %55, i32 %56, i32 1, i32* %57)
br label %16
59: ; preds = %16
%60 = load i32*, i32** %1
%61 = call i32 @fclose(i32* %60)
%62 = load i32*, i32** %2
%63 = call i32 @fclose(i32* %62)
br label %64
64: ; preds = %59, %13, %8
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"sku.17\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [12 x i8] c"sku.testout\00"
@.str.4 = [2 x i8] c"w\00"
@str = [38 x i8] c"couldn't open output file sku.testout\00"
@str.6 = [32 x i8] c"couldn't open input file sku.17\00"
define void @decrypt_sku() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
br label %44
7: ; preds = %0
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i32 @fread(i8* %2, i32 1, i32 1024, i32* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %41, label %15
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %44
15: ; preds = %10, %35
%16 = phi i32 [ %39, %35 ], [ %11, %10 ]
%17 = icmp sgt i32 %16, 1
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = load i8, i8* %2
br label %35
20: ; preds = %15
%21 = zext i32 %16 to i64
%22 = load i8, i8* %2
br label %23
23: ; preds = %20, %23
%24 = phi i8 [ %22, %20 ], [ %32, %23 ]
%25 = phi i64 [ 1, %20 ], [ %33, %23 ]
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = xor i8 %27, %24
%29 = trunc i64 %25 to i32
%30 = tail call i32 @fib(i32 %29)
%31 = trunc i32 %30 to i8
%32 = xor i8 %28, %31
store i8 %32, i8* %26
%33 = add nuw nsw i64 %25, 1
%34 = icmp eq i64 %33, %21
br i1 %34, label %35, label %23
35: ; preds = %23, %18
%36 = phi i8 [ %19, %18 ], [ %22, %23 ]
%37 = tail call signext i8 @reverse_bits(i8 signext %36, i32 8)
store i8 %37, i8* %2
%38 = call i32 @fwrite(i8* %2, i32 %16, i32 1, i32* %8)
%39 = call i32 @fread(i8* %2, i32 1, i32 1024, i32* %3)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %15
41: ; preds = %35, %10
%42 = tail call i32 @fclose(i32* %3)
%43 = tail call i32 @fclose(i32* %8)
br label %44
44: ; preds = %41, %13, %5
ret void
}