// File: /workspace/github_c_makefile_star2_O1/dxwc[P]sfeed/sfeed_gph.c
// Function: main
int
main(int argc, char *argv[])
{
	FILE *fpitems, *fpindex, *fp;
	char *name, path[PATH_MAX + 1];
	int i;
	struct feed *f;

	if (pledge("stdio rpath wpath cpath", NULL) == -1)
		err(1, "pledge");

	if (!(prefixpath = getenv("SFEED_GPH_PATH")))
		prefixpath = "/";

	if (!(feeds = calloc(argc, sizeof(struct feed *))))
		err(1, "calloc");

	if ((comparetime = time(NULL)) == -1)
		err(1, "time");
	/* 1 day is old news */
	comparetime -= 86400;

	if (argc == 1) {
		if (pledge("stdio", NULL) == -1)
			err(1, "pledge");
		if (!(feeds[0] = calloc(1, sizeof(struct feed))))
			err(1, "calloc");
		feeds[0]->name = "";
		printfeed(stdout, stdin, feeds[0]);
	} else {
		/* write main index page */
		if (!(fpindex = fopen("index.gph", "wb")))
			err(1, "fopen: index.gph");

		for (i = 1; i < argc; i++) {
			if (!(feeds[i - 1] = calloc(1, sizeof(struct feed))))
				err(1, "calloc");
			f = feeds[i - 1];
			name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
			f->name = name;

			if (!(fp = fopen(argv[i], "r")))
				err(1, "fopen: %s", argv[i]);

			snprintf(path, sizeof(path), "%s.gph", f->name);
			if (!(fpitems = fopen(path, "wb")))
				err(1, "fopen");
			printfeed(fpitems, fp, f);
			if (ferror(fp))
				err(1, "ferror: %s", argv[i]);
			fclose(fp);
			fclose(fpitems);

			/* append directory item to index */
			fprintf(fpindex, "[1|");
			gphlink(fpindex, f->name, strlen(f->name));
			fprintf(fpindex, " (%lu/%lu)|%s",
			        f->totalnew, f->total, prefixpath);
			snprintf(path, sizeof(path), "%s.gph", f->name);
			gphlink(fpindex, path, strlen(path));
			fputs("|server|port]\n", fpindex);
		}
		fclose(fpindex);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movq %rsi, (%rsp)
movl %edi, %ebp
leaq 0x1c70(%rip), %rdi      # 0x3010
callq 0x1030
testq %rax, %rax
leaq 0x1d23(%rip), %rcx      # 0x30d2
cmovneq %rax, %rcx
movq %rcx, 0x3d56(%rip)      # 0x5110
movslq %ebp, %rdi
movl $0x8, %esi
callq 0x1110
movq %rax, 0x3d4a(%rip)      # 0x5118
testq %rax, %rax
je 0x160d
xorl %edi, %edi
callq 0x1170
movq %rax, 0x3d3b(%rip)      # 0x5120
cmpq $-0x1, %rax
je 0x1657
addq $-0x15180, %rax         # imm = 0xFFFEAE80
movq %rax, 0x3d24(%rip)      # 0x5120
cmpl $0x1, %ebp
jne 0x144e
movl $0x1, %edi
movl $0x60, %esi
callq 0x1110
movq 0x3d01(%rip), %rcx      # 0x5118
movq %rax, (%rcx)
testq %rax, %rax
je 0x160d
leaq 0x1c5a(%rip), %rdx      # 0x3084
movq %rdx, (%rax)
movq 0x3ba4(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
movq 0x3ba2(%rip), %rax      # 0x4fe0
movq (%rax), %rsi
movq (%rcx), %rdx
callq 0x1669
jmp 0x15f9
leaq 0x1bdb(%rip), %rdi      # 0x3030
leaq 0x1bca(%rip), %rsi      # 0x3026
callq 0x11a0
testq %rax, %rax
je 0x1660
movq %rax, %r14
cmpl $0x2, %ebp
jl 0x15f1
movl %ebp, %eax
decq %rax
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %r13
xorl %r12d, %r12d
movl $0x1, %edi
movl $0x60, %esi
callq 0x1110
movq %rax, %rbp
movq 0x3c77(%rip), %rax      # 0x5118
movq %rbp, (%rax,%r12,8)
testq %rbp, %rbp
je 0x160d
movq (%rsp), %rax
movq 0x8(%rax,%r12,8), %rbx
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x10c0
leaq 0x1(%rax), %rcx
testq %rax, %rax
cmoveq %rbx, %rcx
movq %rcx, (%rbp)
movq %rbx, %rdi
leaq 0x1b5d(%rip), %rsi      # 0x303a
callq 0x11a0
testq %rax, %rax
je 0x1629
movq %rax, %r15
movq (%rbp), %rcx
movl $0x1001, %esi           # imm = 0x1001
movq %r13, %rdi
leaq 0x1b45(%rip), %rdx      # 0x3046
xorl %eax, %eax
callq 0x10b0
movq %r13, %rdi
leaq 0x1b14(%rip), %rsi      # 0x3026
callq 0x11a0
testq %rax, %rax
je 0x1616
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1669
movq %r15, %rdi
callq 0x1070
testl %eax, %eax
jne 0x163b
movq %r15, %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1080
movl $0x3, %esi
movl $0x1, %edx
leaq 0x1afc(%rip), %rdi      # 0x305e
movq %r14, %rcx
callq 0x11d0
movq (%rbp), %rbx
movq %rbx, %rdi
callq 0x1090
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x12ec
movq 0x8(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x3b7d(%rip), %r8       # 0x5110
movq %r14, %rdi
leaq 0x1ac5(%rip), %rsi      # 0x3062
xorl %eax, %eax
callq 0x1140
movq (%rbp), %rcx
movl $0x1001, %esi           # imm = 0x1001
movq %r13, %rdi
leaq 0x1a8f(%rip), %rdx      # 0x3046
xorl %eax, %eax
callq 0x10b0
movq %r13, %rdi
callq 0x1090
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x12ec
leaq 0x1a95(%rip), %rdi      # 0x3070
movq %r14, %rsi
callq 0x10d0
incq %r12
cmpq %r12, 0x8(%rsp)
jne 0x1488
movq %r14, %rdi
callq 0x1080
xorl %eax, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a0b(%rip), %rsi      # 0x301f
jmp 0x161d
leaq 0x1a30(%rip), %rsi      # 0x304d
movl $0x1, %edi
xorl %eax, %eax
callq 0x1190
movq (%rsp), %rax
movq 0x8(%rax,%r12,8), %rdx
leaq 0x1a03(%rip), %rsi      # 0x303c
jmp 0x164b
movq (%rsp), %rax
movq 0x8(%rax,%r12,8), %rdx
leaq 0x1a08(%rip), %rsi      # 0x3053
movl $0x1, %edi
xorl %eax, %eax
callq 0x1190
leaq 0x1a2c(%rip), %rsi      # 0x308a
jmp 0x161d
leaq 0x19c2(%rip), %rsi      # 0x3029
jmp 0x161d