function vystupdekoderu = bcheuklideksim(n, k, m, pp, prijateslovo)
% clc
t=bchnumerr(n,k);         %korekční schopnost
polynom_chyb = zeros(1,n);
prijatepom = gf(zeros(1,n),m,pp);           % převedení slova do prvků GF

for i = 1:n                            % invertace prijateho slova
    prijatepom(i) = prijateslovo(i);
end

prijate_chybne = prijatepom;
prijate_chybn=double(prijate_chybne.x);

alpha = gf(2, m);
zero = gf(0, m);
one = gf(1, m);

alpha_tb=gf(zeros(1, 2*t), m);
for i=1:2*t,
    alpha_tb(i)=alpha^(2*t-i+1);
end;

%Vypocet syndromu
syndrome=gf(zeros(1, 2*t), m);
syndromepom=gf(zeros(1, 2*t), m);
for i=1:n,
    syndrome=syndrome.*alpha_tb+prijate_chybne(i);
end;

syndromy = syndrome;
syndromy = double(syndromy.x);

[field,exp] = gftuple([-1:2^m-2]', de2bi(pp,'left-msb'),2);

for i=1:2^m                         % tabulka pro prepocet z integeru na power representation
c=bi2de(field(i,1:m),'left-msb');
tabulka(i,:)=[c i exp(i)];
end
tabulka=sortrows(tabulka);


for i=1:length(syndromy)
    for j=1:2^m
        if tabulka(j,1)==syndromy(i)
            S2(i)=(tabulka(j,3));
        end
    end
end

S=S2;

% inicializace Euklidova algoritmu
R = [-Inf*ones(1,2*t) 0];
T = S;

A11 = 0;
A12 = -Inf;
A21 = -Inf;
A22 = 0;

l = 1;
while(T(t+1) ~= -Inf)
    [Q, rmndr] = gfdeconv(R,T,field);
    A11_a = A12;
    A12_a = A22;
    A21 = gfadd(A11, gfconv(Q,A21,field),field);
    A22 = gfadd(A12, gfconv(Q,A22,field),field);
    A11 = A11_a;
    A12 = A12_a;
                
    R1 = T;
    T = gfadd(R ,gfconv(Q,T,field),field);
    R = R1;
    l = l + 1;
end

u_minus_1 = gfdiv(0,A22(1),field);
sigma = gfmul(u_minus_1*ones(1,length(A22)), A22,field);
omega = gfmul(u_minus_1*ones(1,length(T)), T,field);

for count=1:length(sigma)
    if sigma(count)==-Inf
      sigma(count)=-1;
    end
end
if sigma==0 & (length(sigma)==1)
 opraveno=prijate_chybne;
     hodnotachyb = 0;
    pozice = 0;

else
sum = -Inf;
l = 1;
for i = 0:2^m-2
    sum = -Inf;
    for j = 1:length(sigma)
        sum = gfadd(sum,mod(sigma(j)-i*j,2^m-1),field);
    end
  
    
    if sum == -Inf
    polynom_chyb(i+1)=1;
    end
    
end

pozice = (2^m-(find(polynom_chyb(:)~=0)'));

hodnotachyb=zeros(1,length(pozice));

for count=1:length(pozice)
    hodnotachyb(count)=1;
end



polypom=zeros(1,n);
for i = 1:n                            % invertace prijateho slova
    polypom(i) = polynom_chyb(n+1-i);
end
polynom_chyb=polypom;
opraveno = prijate_chybne + polynom_chyb;

end

opraveno=double(opraveno.x);
vystupdekoderu=opraveno(1:k);