#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#define BLOCK_SIZE 256

int main(void) {

	FILE* d1;
	FILE* d3;
	FILE* d4;
	FILE* dParity;
	d1 = fopen("d1.img", "rb");
	d3 = fopen("d3.img", "rb");
	d4 = fopen("d4.img", "rb");
	dParity = fopen("parity.img", "rb");

	uint8_t data1[BLOCK_SIZE];
	uint8_t data3[BLOCK_SIZE];
	uint8_t data4[BLOCK_SIZE];
	uint8_t dataParity[BLOCK_SIZE];

	FILE* d2_even;
	FILE* d2_odd;
	d2_even = fopen("d2_even.img", "wb");
	d2_odd = fopen("d2_odd.img", "wb");

	uint8_t data2_even[BLOCK_SIZE];
	uint8_t data2_odd[BLOCK_SIZE];

	for (int size = 0; size < 32; size++) {
		
		fread(data1, sizeof(uint8_t), BLOCK_SIZE, d1);
		fread(data3, sizeof(uint8_t), BLOCK_SIZE, d3);
		fread(data4, sizeof(uint8_t), BLOCK_SIZE, d4);
		fread(dataParity, sizeof(uint8_t), BLOCK_SIZE, dParity);

		for (int i = 0; i < BLOCK_SIZE; i++) {
			
			data2_even[i] = (data1[i] ^ data3[i] ^ data4[i] ^ dataParity[i]);
			data2_odd[i] ^= (data1[i] ^ data3[i] ^ data4[i] ^ dataParity[i]);

		}

		fwrite(data2_even, sizeof(uint8_t), BLOCK_SIZE, d2_even);
		fwrite(data2_odd, sizeof(uint8_t), BLOCK_SIZE, d2_odd);

	}

	return 0;

}