// File: /workspace/github_c_makefile_star2_O2/ajpaulson[P]srogue11/save.c
// Function: restore
bool restore(char const *file,char **envp)
{
  char inf;
  int pid;
	int ret_status;
#ifndef _AIX
	extern char **environ;
#endif
#ifdef __DJGPP__                      /* st_ino w/ DJGPP under WinXP broken */
        _djstat_flags |= _STAT_INODE; /* so turn off computing it for now   */
#endif
	char buf[LINLEN];
	STAT sbuf2;
	int slines, scols;

	if ((inf = open(file, O_RDONLY)) < 0) {
		printf("Cannot read save game %s\n",file);
		return FALSE;
	}

	encread(buf, strlen(version) + 1, inf);

	if (strcmp(buf, version) != 0) {
		printf("Sorry, saved game version is out of date.\n");
		return FALSE;
	}

	fstat(inf, &sbuf2);

	encread(&sbuf.st_ino,sizeof(sbuf.st_ino), inf);
	encread(&sbuf.st_dev,sizeof(sbuf.st_dev), inf);
	encread(&sbuf.st_ctime,sizeof(sbuf.st_ctime), inf);
	encread(&sbuf.st_mtime,sizeof(sbuf.st_mtime), inf);
	encread(&slines,sizeof(slines),inf);
	encread(&scols,sizeof(scols),inf);

	/*
	 * we do not close the file so that we will have a hold of the
	 * inode for as long as possible
	 */

	if (!wizard)
	{
		if(sbuf2.st_ino!=sbuf.st_ino || sbuf2.st_dev!=sbuf.st_dev) {
			printf("Sorry, saved game is not in the same file.\n");
			return FALSE;
		}
	}

#ifdef __INTERIX
	setenv("TERM","interix");
#endif

	initscr();

	if (slines > LINES)
	{
		endwin();
		printf("Sorry, original game was played on a screen with %d lines.\n",slines);
		printf("Current screen only has %d lines. Unable to restore game\n",LINES);
		return(FALSE);
	}

	if (scols > COLS)
	{
		endwin();
		printf("Sorry, original game was played on a screen with %d columns.\n", scols);
		printf("Current screen only has %d columns. Unable to restore game\n",COLS);
		return(FALSE);
	}

	cw = newwin(LINES, COLS, 0, 0);
	mw = newwin(LINES, COLS, 0, 0);
	hw = newwin(LINES, COLS, 0, 0);

	mpos = 0;
	mvwprintw(cw, 0, 0, "%s: %s", file, ctime(&sbuf2.st_mtime));

	/* defeat multiple restarting from the same place */

	if (!wizard)
	{
		if (sbuf2.st_nlink != 1)
		{
                        endwin();
			printf("Cannot restore from a linked file\n");
			return FALSE;
		}
	}

	if (rs_restore_file(inf) == FALSE)
	{
		endwin();
		printf("Cannot restore file\n");
		return(FALSE);
	}

#if defined(__CYGWIN__) || defined(__DJGPP__)
	close(inf);
#endif
	if (!wizard)
	{
#ifndef __DJGPP__
			endwin();
			while((pid = fork()) < 0)
				sleep(1);

			/* set id to unlink file */
			if(pid == 0)
			{
				setuid(playuid);
				setgid(playgid);
				unlink(file);
				exit(0);
			}
			/* wait for unlink to finish */
			else
			{
				while(wait(&ret_status) != pid)
					continue;
				if (ret_status < 0)
				{
					printf("Cannot unlink file\n");
					return FALSE;
				}
			}
#else
		if (unlink(file) < 0)
		{
			printf("Cannot unlink file\n");
			return FALSE;
		}
#endif

	}

	environ = envp;

	strcpy(file_name, file);
	setup();
	restscr(cw);
	srand48(getpid());
	playit();
  return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorl %esi, %esi
xorl %eax, %eax
callq 0x6430
movsbl %al, %ebp
testl %ebp, %ebp
js 0x135b0
leaq 0x1a02f(%rip), %r15     # 0x2d5a0
movq %r15, %rdi
callq 0x6170
leal 0x1(%rax), %esi
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movl %ebp, %edx
callq 0x146da
movq %r12, %rdi
movq %r15, %rsi
callq 0x6240
testl %eax, %eax
je 0x135c8
leaq 0xd216(%rip), %rdi      # 0x207ba
callq 0x60f0
xorl %ebp, %ebp
jmp 0x136ea
leaq 0xd07e(%rip), %rdi      # 0x20635
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x61c0
jmp 0x136ea
leaq 0x10(%rsp), %rsi
movl %ebp, %edi
callq 0x6550
leaq 0x1fe0d(%rip), %rdi     # 0x333e8
pushq $0x8
popq %r15
movl %r15d, %esi
movl %ebp, %edx
callq 0x146da
leaq 0x1fdf0(%rip), %rdi     # 0x333e0
movl %r15d, %esi
movl %ebp, %edx
callq 0x146da
leaq 0x1fe47(%rip), %rdi     # 0x33448
movl %r15d, %esi
movl %ebp, %edx
callq 0x146da
leaq 0x1fe26(%rip), %rdi     # 0x33438
movl %r15d, %esi
movl %ebp, %edx
callq 0x146da
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %r15
movl %r15d, %esi
movl %ebp, %edx
callq 0x146da
leaq 0x4(%rsp), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x146da
leaq 0x1a248(%rip), %r13     # 0x2d88d
cmpb $0x0, (%r13)
je 0x13686
callq 0x61f0
movq 0x16968(%rip), %r15     # 0x29fc0
movl (%r15), %edi
cmpl %edi, 0x8(%rsp)
jle 0x136ae
callq 0x6510
movl 0x8(%rsp), %esi
leaq 0xcfde(%rip), %rdi      # 0x2064f
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x61c0
movl (%r15), %esi
leaq 0xd007(%rip), %rdi      # 0x2068b
jmp 0x136e3
movq 0x18(%rsp), %rax
cmpq 0x1fd56(%rip), %rax     # 0x333e8
jne 0x136a2
movq 0x10(%rsp), %rax
cmpq 0x1fd40(%rip), %rax     # 0x333e0
je 0x1364c
leaq 0xd09d(%rip), %rdi      # 0x20746
jmp 0x135a4
movq 0x168eb(%rip), %r12     # 0x29fa0
movl (%r12), %esi
cmpl %esi, 0x4(%rsp)
jle 0x136fe
callq 0x6510
movl 0x4(%rsp), %esi
leaq 0xcff6(%rip), %rdi      # 0x206c5
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x61c0
movl (%r12), %esi
leaq 0xd020(%rip), %rdi      # 0x20703
xorl %eax, %eax
callq 0x61c0
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x60d0
leaq 0x1d2c2(%rip), %rcx     # 0x309d0
movq %rax, (%rcx)
movl (%r15), %edi
movl (%r12), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x60d0
leaq 0x1d2b8(%rip), %rcx     # 0x309e0
movq %rax, (%rcx)
movl (%r15), %edi
movl (%r12), %esi
leaq 0x1d297(%rip), %r12     # 0x309d0
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x60d0
leaq 0x1a117(%rip), %rcx     # 0x2d860
andl $0x0, (%rcx)
leaq 0x1d285(%rip), %rcx     # 0x309d8
movq %rax, (%rcx)
movq (%r12), %r15
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
callq 0x6160
leaq 0xcfd2(%rip), %rcx      # 0x2073f
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x6210
cmpb $0x0, (%r13)
jne 0x137a1
cmpq $0x1, 0x20(%rsp)
je 0x137a1
callq 0x6510
leaq 0xcffc(%rip), %rdi      # 0x20798
jmp 0x135a4
movl %ebp, %edi
callq 0x17830
testl %eax, %eax
je 0x137cf
cmpb $0x0, (%r13)
jne 0x137fc
callq 0x6510
pushq $0x1
popq %r15
callq 0x6570
testl %eax, %eax
jns 0x137e0
movl %r15d, %edi
callq 0x6540
jmp 0x137bc
callq 0x6510
leaq 0xcfa9(%rip), %rdi      # 0x20784
jmp 0x135a4
je 0x1384d
movl %eax, %ebp
leaq 0xc(%rsp), %r15
movq %r15, %rdi
callq 0x6560
cmpl %ebp, %eax
jne 0x137e9
cmpl $0x0, 0xc(%rsp)
js 0x13841
movq 0x1677d(%rip), %rax     # 0x29f80
movq %r14, (%rax)
leaq 0x1a7b3(%rip), %rdi     # 0x2dfc0
movq %rbx, %rsi
callq 0x60e0
xorl %eax, %eax
callq 0xc04d
movq (%r12), %rdi
callq 0xb7b6
callq 0x6130
movslq %eax, %rdi
callq 0x63b0
xorl %eax, %eax
callq 0xc0ef
movb $0x1, %bpl
jmp 0x136ea
leaq 0xcf29(%rip), %rdi      # 0x20771
jmp 0x135a4
leaq 0x1a5fc(%rip), %rax     # 0x2de50
movl (%rax), %edi
callq 0x6500
leaq 0x1a5f2(%rip), %rax     # 0x2de54
movl (%rax), %edi
callq 0x63f0
movq %rbx, %rdi
callq 0x60a0
xorl %edi, %edi
callq 0x64e0