#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*/ v;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ decrypt(unsigned int *, unsigned int *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned int *);
int /*<<< orphan*/ fwrite(unsigned int *, int, int, int /*<<< orphan*/ *);
unsigned int *malloc(int);
int /*<<< orphan*/ memcpy(unsigned int *, unsigned int *, int);
int /*<<< orphan*/ printf(char *);
int main() {
  unsigned int v[2], k[4] = {128, 129, 130, 131};
  int n_bloques, tam;
  FILE *f_encriptado, *f_desencriptado;
  unsigned int *ptr2, *ptr2_ref;
  f_encriptado = fopen("imgno8.enc", "rb");
  f_desencriptado = fopen("imagen2.png", "wb");
  if (f_encriptado == NULL || f_desencriptado == NULL) {
    printf("Error: No se pudo abrir el fichero\n");
  }
  fread(v, 1, sizeof(int), f_encriptado);
  tam = *((int *)v);
  n_bloques = tam / 8;
  ptr2 = malloc(sizeof(v) * n_bloques);
  ptr2_ref = ptr2;
  for (int i = 0; i < n_bloques; i++) {
    fread(v, 1, 8, f_encriptado);
    decrypt(v, k);
    memcpy(ptr2, v, 8);
    ptr2 += 2;
  }
  fwrite(ptr2_ref, 8, n_bloques, f_desencriptado);
  if (tam % 8 != 0) {
    memcpy(ptr2, v, 8);
    decrypt(v, k);
    fwrite(ptr2_ref, 8, tam % 8, f_desencriptado);
  }
  fclose(f_encriptado);
  fclose(f_desencriptado);
  free(ptr2_ref);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe54(%rip),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
lea    0xe4b(%rip),%rdi
lea    0xe50(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f9 <main+0x69>
cmpq   $0x0,-0x38(%rbp)
jne    1205 <main+0x75>
lea    0xe2d(%rip),%rdi
call   1070 <printf@plt>
lea    -0xc(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1050 <fread@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12ae <main+0x11e>
lea    -0xc(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1050 <fread@plt>
lea    -0xc(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <decrypt@plt>
mov    -0x40(%rbp),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1252 <main+0xc2>
mov    -0x48(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x8,%esi
call   10b0 <fwrite@plt>
mov    -0x28(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1313 <main+0x183>
mov    -0x40(%rbp),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
lea    -0xc(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <decrypt@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%rcx
mov    $0x8,%esi
call   10b0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movaps 0xe5b(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe5f(%rip),%rdi
lea    0xe63(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
lea    0xe57(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %r15,%r15
je     11e0 <main+0x50>
test   %rbx,%rbx
jne    11ec <main+0x5c>
lea    0xe46(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,0x18(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r15,%rcx
call   1060 <fread@plt>
mov    0x8(%rsp),%ebx
lea    0x7(%rbx),%edi
test   %ebx,%ebx
cmovns %ebx,%edi
mov    %edi,%r14d
sar    $0x3,%r14d
and    $0xfffffff8,%edi
mov    %ebx,%eax
sub    %edi,%eax
mov    %eax,0x4(%rsp)
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %rax,0x10(%rsp)
cmp    $0x8,%ebx
mov    %r14d,%ebx
jl     128a <main+0xfa>
lea    0x8(%rsp),%r14
lea    0x20(%rsp),%r12
mov    %ebx,%r13d
mov    0x10(%rsp),%rbp
nopl   (%rax)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r15,%rcx
call   1060 <fread@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <decrypt@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
add    $0x8,%rbp
add    $0xffffffff,%r13d
jne    1250 <main+0xc0>
mov    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x8,%esi
mov    %ebx,%edx
mov    0x18(%rsp),%rbx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    0x4(%rsp),%r13d
test   %r13d,%r13d
je     12e5 <main+0x155>
lea    0x8(%rsp),%r14
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
lea    0x20(%rsp),%rsi
mov    %r14,%rdi
call   1030 <decrypt@plt>
mov    %r12,%rdi
mov    $0x8,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %r12,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.k = [4 x i32] [i32 128, i32 129, i32 130, i32 131]
@.str = [11 x i8] c"imgno8.enc\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [12 x i8] c"imagen2.png\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [36 x i8] c"Error: No se pudo abrir el fichero\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [4 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
store i32 0, i32* %1
%11 = bitcast [4 x i32]* %3 to i8*
%12 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %0
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16, %0
%20 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%23 = load i32*, i32** %6
%24 = call i32 @fread(i32* %22, i32 1, i32 4, i32* %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = sdiv i32 %27, 8
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = mul i64 8, %30
%32 = trunc i64 %31 to i32
%33 = call i32* @malloc(i32 %32)
store i32* %33, i32** %8
%34 = load i32*, i32** %8
store i32* %34, i32** %9
store i32 0, i32* %10
br label %35
35: ; preds = %51, %21
%36 = load i32, i32* %10
%37 = load i32, i32* %4
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %54
39: ; preds = %35
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%41 = load i32*, i32** %6
%42 = call i32 @fread(i32* %40, i32 1, i32 8, i32* %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%44 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%45 = call i32 @decrypt(i32* %43, i32* %44)
%46 = load i32*, i32** %8
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%48 = call i32 @memcpy(i32* %46, i32* %47, i32 8)
%49 = load i32*, i32** %8
%50 = getelementptr inbounds i32, i32* %49, i64 2
store i32* %50, i32** %8
br label %51
51: ; preds = %39
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %35
54: ; preds = %35
%55 = load i32*, i32** %9
%56 = load i32, i32* %4
%57 = load i32*, i32** %7
%58 = call i32 @fwrite(i32* %55, i32 8, i32 %56, i32* %57)
%59 = load i32, i32* %5
%60 = srem i32 %59, 8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %74
62: ; preds = %54
%63 = load i32*, i32** %8
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%65 = call i32 @memcpy(i32* %63, i32* %64, i32 8)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%67 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%68 = call i32 @decrypt(i32* %66, i32* %67)
%69 = load i32*, i32** %9
%70 = load i32, i32* %5
%71 = srem i32 %70, 8
%72 = load i32*, i32** %7
%73 = call i32 @fwrite(i32* %69, i32 8, i32 %71, i32* %72)
br label %74
74: ; preds = %62, %54
%75 = load i32*, i32** %6
%76 = call i32 @fclose(i32* %75)
%77 = load i32*, i32** %7
%78 = call i32 @fclose(i32* %77)
%79 = load i32*, i32** %9
%80 = call i32 @free(i32* %79)
ret i32 0
}

LLVM IR (-O2):
@__const.main.k = [4 x i32] [i32 128, i32 129, i32 130, i32 131]
@.str = [11 x i8] c"imgno8.enc\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [12 x i8] c"imagen2.png\00"
@.str.3 = [3 x i8] c"wb\00"
@str = [35 x i8] c"Error: No se pudo abrir el fichero\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [4 x i32]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = bitcast [4 x i32]* %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%7 = icmp eq i32* %5, null
%8 = icmp eq i32* %6, null
%9 = or i1 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %0, %10
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%14 = call i32 @fread(i32* %13, i32 1, i32 4, i32* %5)
%15 = load i32, i32* %13
%16 = sdiv i32 %15, 8
%17 = srem i32 %15, 8
%18 = shl nsw i32 %16, 3
%19 = tail call i32* @malloc(i32 %18)
%20 = icmp sgt i32 %15, 7
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
br label %27
23: ; preds = %27, %12
%24 = phi i32* [ %19, %12 ], [ %33, %27 ]
%25 = call i32 @fwrite(i32* %19, i32 8, i32 %16, i32* %6)
%26 = icmp eq i32 %17, 0
br i1 %26, label %41, label %36
27: ; preds = %21, %27
%28 = phi i32 [ 0, %21 ], [ %34, %27 ]
%29 = phi i32* [ %19, %21 ], [ %33, %27 ]
%30 = call i32 @fread(i32* %13, i32 1, i32 8, i32* %5)
%31 = call i32 @decrypt(i32* %13, i32* %22)
%32 = call i32 @memcpy(i32* %29, i32* %13, i32 8)
%33 = getelementptr inbounds i32, i32* %29, i64 2
%34 = add nuw nsw i32 %28, 1
%35 = icmp eq i32 %34, %16
br i1 %35, label %23, label %27
36: ; preds = %23
%37 = call i32 @memcpy(i32* %24, i32* %13, i32 8)
%38 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%39 = call i32 @decrypt(i32* %13, i32* %38)
%40 = call i32 @fwrite(i32* %19, i32 8, i32 %17, i32* %6)
br label %41
41: ; preds = %36, %23
%42 = call i32 @fclose(i32* %5)
%43 = call i32 @fclose(i32* %6)
%44 = call i32 @free(i32* %19)
ret i32 0
}