// File: /workspace/github_c_makefile_star2_O3/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 0x6450
movsbl %al, %ebp
testl %ebp, %ebp
js 0x143d7
leaq 0x19218(%rip), %r15     # 0x2d5b0
movq %r15, %rdi
callq 0x6170
leal 0x1(%rax), %esi
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movl %ebp, %edx
callq 0x15594
movq %r12, %rdi
movq %r15, %rsi
callq 0x6250
testl %eax, %eax
je 0x143ef
leaq 0xe3ef(%rip), %rdi      # 0x227ba
callq 0x60f0
xorl %ebp, %ebp
jmp 0x14515
leaq 0xe257(%rip), %rdi      # 0x22635
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x61c0
jmp 0x14515
leaq 0x10(%rsp), %rsi
movl %ebp, %edi
callq 0x6570
leaq 0x1eff6(%rip), %rdi     # 0x333f8
movl $0x8, %esi
movl %ebp, %edx
callq 0x15594
leaq 0x1efdb(%rip), %rdi     # 0x333f0
movl $0x8, %esi
movl %ebp, %edx
callq 0x15594
leaq 0x1f030(%rip), %rdi     # 0x33458
movl $0x8, %esi
movl %ebp, %edx
callq 0x15594
leaq 0x1f00d(%rip), %rdi     # 0x33448
movl $0x8, %esi
movl %ebp, %edx
callq 0x15594
leaq 0x8(%rsp), %rdi
movl $0x4, %esi
movl %ebp, %edx
callq 0x15594
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl %ebp, %edx
callq 0x15594
leaq 0x1942d(%rip), %r13     # 0x2d89d
cmpb $0x0, (%r13)
je 0x144b1
callq 0x61f0
movq 0x15b2d(%rip), %r15     # 0x29fb0
movl (%r15), %edi
cmpl %edi, 0x8(%rsp)
jle 0x144d9
callq 0x6530
movl 0x8(%rsp), %esi
leaq 0xe1b3(%rip), %rdi      # 0x2264f
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x61c0
movl (%r15), %esi
leaq 0xe1dc(%rip), %rdi      # 0x2268b
jmp 0x1450e
movq 0x18(%rsp), %rax
cmpq 0x1ef3b(%rip), %rax     # 0x333f8
jne 0x144cd
movq 0x10(%rsp), %rax
cmpq 0x1ef25(%rip), %rax     # 0x333f0
je 0x14477
leaq 0xe272(%rip), %rdi      # 0x22746
jmp 0x143cb
movq 0x15ab8(%rip), %r12     # 0x29f98
movl (%r12), %esi
cmpl %esi, 0x4(%rsp)
jle 0x14529
callq 0x6530
movl 0x4(%rsp), %esi
leaq 0xe1cb(%rip), %rdi      # 0x226c5
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x61c0
movl (%r12), %esi
leaq 0xe1f5(%rip), %rdi      # 0x22703
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 0x1c4a7(%rip), %rcx     # 0x309e0
movq %rax, (%rcx)
movl (%r15), %edi
movl (%r12), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x60d0
leaq 0x1c49d(%rip), %rcx     # 0x309f0
movq %rax, (%rcx)
movl (%r15), %edi
movl (%r12), %esi
leaq 0x1c47c(%rip), %r12     # 0x309e0
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x60d0
leaq 0x1c474(%rip), %rcx     # 0x309e8
movq %rax, (%rcx)
leaq 0x192f2(%rip), %rax     # 0x2d870
movl $0x0, (%rax)
movq (%r12), %r15
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
callq 0x6160
leaq 0xe1a4(%rip), %rcx      # 0x2273f
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x6210
cmpb $0x0, (%r13)
jne 0x145cf
cmpq $0x1, 0x20(%rsp)
je 0x145cf
callq 0x6530
leaq 0xe1ce(%rip), %rdi      # 0x22798
jmp 0x143cb
movl %ebp, %edi
callq 0x18f23
testl %eax, %eax
je 0x145fb
cmpb $0x0, (%r13)
jne 0x1462a
callq 0x6530
callq 0x6590
testl %eax, %eax
jns 0x1460c
movl $0x1, %edi
callq 0x6560
jmp 0x145e6
callq 0x6530
leaq 0xe17d(%rip), %rdi      # 0x22784
jmp 0x143cb
movl %eax, %ebp
testl %eax, %eax
je 0x1467b
leaq 0xc(%rsp), %r15
movq %r15, %rdi
callq 0x6580
cmpl %ebp, %eax
jne 0x14617
cmpl $0x0, 0xc(%rsp)
js 0x1466f
movq 0x15947(%rip), %rax     # 0x29f78
movq %r14, (%rax)
leaq 0x19995(%rip), %rdi     # 0x2dfd0
movq %rbx, %rsi
callq 0x60e0
xorl %eax, %eax
callq 0xc6e6
movq (%r12), %rdi
callq 0xbd09
callq 0x6130
movslq %eax, %rdi
callq 0x63c0
xorl %eax, %eax
callq 0xc7a0
movb $0x1, %bpl
jmp 0x14515
leaq 0xe0fb(%rip), %rdi      # 0x22771
jmp 0x143cb
leaq 0x197de(%rip), %rax     # 0x2de60
movl (%rax), %edi
callq 0x6520
leaq 0x197d4(%rip), %rax     # 0x2de64
movl (%rax), %edi
callq 0x6410
movq %rbx, %rdi
callq 0x60a0
xorl %edi, %edi
callq 0x6500
nop