#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 encrypt_sku(void) {
  FILE *sku17;
  FILE *skutestout;
  char sku_buffer[1024];
  int num_bytes_read;
  if ((sku17 = fopen("sku.testout", "r")) == NULL) {
    printf("couldn't open input file sku.17\n");
    return;
  }
  if ((skutestout = fopen("sku.test17out", "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;
    sku_buffer[0] = reverse_bits(sku_buffer[0], 8);
    for (current_byte = num_bytes_read - 1; current_byte > 0; --current_byte) {
      sku_buffer[current_byte] = sku_buffer[current_byte - 1] ^
                                 sku_buffer[current_byte] ^
                                 (fib(current_byte) & 0xFF);
    }
    fwrite(sku_buffer, num_bytes_read, 1, skutestout);
  }
  fclose(sku17);
  fclose(skutestout);
  return;
}


x86 assembly (clang, -O0):
<encrypt_sku>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11ad <encrypt_sku+0x3d>
lea    0xe6b(%rip),%rdi
call   1060 <printf@plt>
jmp    12db <encrypt_sku+0x16b>
lea    0xe7b(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11df <encrypt_sku+0x6f>
lea    0xe6a(%rip),%rdi
call   1060 <printf@plt>
jmp    12db <encrypt_sku+0x16b>
jmp    11e4 <encrypt_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 <encrypt_sku+0x159>
mov    $0x8,%esi
movsbl -0x410(%rbp),%edi
call   1050 <reverse_bits@plt>
mov    %al,-0x410(%rbp)
mov    -0x414(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jle    12a9 <encrypt_sku+0x139>
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    $0xffffffff,%eax
mov    %eax,-0x418(%rbp)
jmp    1233 <encrypt_sku+0xc3>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    11e4 <encrypt_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):
<encrypt_sku>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xea4(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1293 <encrypt_sku+0x123>
lea    0xe5a(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     129c <encrypt_sku+0x12c>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    (%rsp),%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     126f <encrypt_sku+0xff>
mov    %eax,%r13d
lea    0x10(%rsp),%r12
jmp    1222 <encrypt_sku+0xb2>
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    0x8(%rsp),%rcx
call   1090 <fwrite@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    (%rsp),%rcx
call   1040 <fread@plt>
mov    %eax,%r13d
test   %eax,%eax
je     126f <encrypt_sku+0xff>
movsbl 0x10(%rsp),%edi
mov    $0x8,%esi
call   1060 <reverse_bits@plt>
mov    %al,0x10(%rsp)
cmp    $0x2,%r13d
jl     11f0 <encrypt_sku+0x80>
mov    %r13d,%ebx
xchg   %ax,%ax
lea    -0x1(%rbx),%rbp
lea    -0x2(%rbx),%eax
mov    %ebp,%r14d
movzbl 0x10(%rsp,%r14,1),%r15d
xor    0x10(%rsp,%rax,1),%r15b
mov    %ebp,%edi
call   1070 <fib@plt>
xor    %r15b,%al
mov    %al,0x10(%rsp,%r14,1)
cmp    $0x2,%rbx
mov    %rbp,%rbx
ja     1240 <encrypt_sku+0xd0>
jmp    11f0 <encrypt_sku+0x80>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xd9e(%rip),%rdi
jmp    12a3 <encrypt_sku+0x133>
lea    0xd6f(%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 = [12 x i8] c"sku.testout\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 = [14 x i8] c"sku.test17out\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 @encrypt_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 ([12 x i8], [12 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 %65
10: ; preds = %0
%11 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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 %65
15: ; preds = %10
br label %16
16: ; preds = %55, %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 %60
21: ; preds = %16
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = load i8, i8* %22
%24 = call signext i8 @reverse_bits(i8 signext %23, i32 8)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 %24, i8* %25
%26 = load i32, i32* %4
%27 = sub nsw i32 %26, 1
store i32 %27, i32* %5
br label %28
28: ; preds = %52, %21
%29 = load i32, i32* %5
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %55
31: ; preds = %28
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 1
%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 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = xor i32 %37, %42
%44 = load i32, i32* %5
%45 = call i32 @fib(i32 %44)
%46 = and i32 %45, 255
%47 = xor i32 %43, %46
%48 = trunc i32 %47 to i8
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %50
store i8 %48, i8* %51
br label %52
52: ; preds = %31
%53 = load i32, i32* %5
%54 = add nsw i32 %53, -1
store i32 %54, i32* %5
br label %28
55: ; preds = %28
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%57 = load i32, i32* %4
%58 = load i32*, i32** %2
%59 = call i32 @fwrite(i8* %56, i32 %57, i32 1, i32* %58)
br label %16
60: ; preds = %16
%61 = load i32*, i32** %1
%62 = call i32 @fclose(i32* %61)
%63 = load i32*, i32** %2
%64 = call i32 @fclose(i32* %63)
br label %65
65: ; preds = %60, %13, %8
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"sku.testout\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [14 x i8] c"sku.test17out\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 @encrypt_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 ([12 x i8], [12 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 %45
7: ; preds = %0
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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 %42, 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 %45
15: ; preds = %10, %38
%16 = phi i32 [ %40, %38 ], [ %11, %10 ]
%17 = load i8, i8* %2
%18 = tail call signext i8 @reverse_bits(i8 signext %17, i32 8)
store i8 %18, i8* %2
%19 = icmp sgt i32 %16, 1
br i1 %19, label %20, label %38
20: ; preds = %15
%21 = zext i32 %16 to i64
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ %21, %20 ], [ %24, %22 ]
%24 = add nsw i64 %23, -1
%25 = trunc i64 %24 to i32
%26 = add nuw i64 %23, 4294967294
%27 = and i64 %26, 4294967295
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = and i64 %24, 4294967295
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = xor i8 %32, %29
%34 = tail call i32 @fib(i32 %25)
%35 = trunc i32 %34 to i8
%36 = xor i8 %33, %35
store i8 %36, i8* %31
%37 = icmp ugt i64 %23, 2
br i1 %37, label %22, label %38
38: ; preds = %22, %15
%39 = call i32 @fwrite(i8* %2, i32 %16, i32 1, i32* %8)
%40 = call i32 @fread(i8* %2, i32 1, i32 1024, i32* %3)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %15
42: ; preds = %38, %10
%43 = tail call i32 @fclose(i32* %3)
%44 = tail call i32 @fclose(i32* %8)
br label %45
45: ; preds = %42, %13, %5
ret void
}