program reversefile2;


uses Windows;
const MAX_READ = 4096;

// avoids using SysUtils and its initializations
function FileRead(Handle: THandle; var Buffer; Count: LongWord): Integer;
begin
  if not ReadFile(Handle, Buffer, Count, LongWord(Result), nil) then
    Result := -1;
end;

function FileWrite(Handle: THandle; const Buffer; Count: LongWord): Integer;
begin
  if not WriteFile(Handle, Buffer, Count, LongWord(Result), nil) then
    Result := -1;
end;

var buf: array of char;
    numRead: integer;
    inHandle,outHandle: THandle;
    filePos,i,e: cardinal;
begin
  inHandle:=GetStdHandle(STD_INPUT_HANDLE);
  outHandle:=GetStdHandle(STD_OUTPUT_HANDLE);
  setLength(buf, MAX_READ);
  numRead:=fileRead(inHandle,buf[0],MAX_READ);
  filePos:=0;
  while numRead>0 do begin
    inc(filePos,numRead);
    if integer(filePos)+MAX_READ-1>high(buf) then
      setLength(buf, (high(buf)+1)*2);

    numRead:=fileRead(inHandle,buf[filePos],MAX_READ);
  end;

  e:=filePos-1;
  for i:=filePos-2 downto 0 do
    if buf[i]=#10 then begin
      fileWrite(outHandle,buf[i+1],e-i);
      e:=i;
    end;
  fileWrite(outHandle,buf[0],e+1);
end.