// File: /workspace/github_c_makefile_star2_O1/tomka[P]cliaspora/config.c
// Function: write_config
int
write_config(const char *label)
{
	int   i, fd, varlen;
	bool  var_written[NVARS];
	char  *p, *path, *ln, tmpl[] = "/tmp/XXXXXX";
	FILE  *fp, *tmpfp;
	var_t *var;

	if ((path = cfgpath()) == NULL)
		return (-1);
	if ((fp = fopen(path, "r+")) == NULL && errno == ENOENT) {
		if ((fp = fopen(path, "w+")) == NULL) {
			warn("fopen(%s)", path); free(path);
			return (-1);
		}
		(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
	} else if (fp == NULL) {
		warn("fopen(%s)", path); free(path); return (-1);
	}
	free(path);

	if ((fd = mkstemp(tmpl)) == -1) {
		warn("mkstemp()"); (void)fclose(fp); return (-1);
	}
	if ((tmpfp = fdopen(fd, "r+")) == NULL) {
		warn("fdopen()"); (void)fclose(fp); (void)close(fd);
		return (-1);
	}
	for (i = 0; i < NVARS; i++)
		var_written[i] = false;
	(void)fseek(fp, 0, SEEK_SET);
	/* Just copy all global variables, etc. */
	while ((ln = readln(fp)) != NULL && !cmp_label(ln, label))
		(void)fputs(ln, tmpfp);
	if (ln == NULL)
		/* Label doesn't exist yet. */
		(void)fprintf(tmpfp, "\n%s:\n", label);
	else {
		/* Write label. */
		(void)fputs(ln, tmpfp);
		/* Write variables in section as they appear in config file. */
		while ((ln = readln(fp)) != NULL && !is_label(ln) != 0) {
			for (p = ln; isspace(*p); p++)
				;
			if (*p == '\0' || *p == '#')
				(void)fputs(ln, tmpfp);
			else {
				varlen = strcspn(p, " =\t");
				if ((var = find_var(p, varlen)) == NULL)
					(void)fputs(ln, tmpfp);
				else {
					for (i = 0; i < NVARS; i++) {
						if (var == &vars[i])
							var_written[i] = true;
					}
					(void)write_var(var, tmpfp);
				}
			}
		}
	}
	/* Write remaining variables. */
	for (i = 0; i < NVARS; i++) {
		if (!var_written[i])
			(void)write_var(&vars[i], tmpfp);
	}
	/* Copy the remaining lines. */ 
	while (ln != NULL) {
		(void)fputs(ln, tmpfp); ln = readln(fp);
	}
	(void)fflush(tmpfp);
	(void)fseek(fp, 0, SEEK_SET); (void)fseek(tmpfp, 0, SEEK_SET);

	while ((ln = readln(tmpfp)) != NULL)
		(void)fputs(ln, fp);
	(void)fclose(tmpfp); (void)remove(tmpl);

	(void)fflush(fp);
        (void)ftruncate(fileno(fp), ftell(fp));
        (void)fclose(fp); free(ln);

	return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r12
movabsq $0x5858582f706d742f, %rax # imm = 0x5858582F706D742F
movq %rax, (%rsp)
movl $0x585858, 0x8(%rsp)    # imm = 0x585858
callq 0x6950
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x749e
movq %rax, %r14
leaq 0x43d1(%rip), %rsi      # 0xb512
movq %rax, %rdi
callq 0x23a0
movq %rax, %rbx
testq %rax, %rax
je 0x721d
movq %r14, %rdi
callq 0x23c0
movq %rsp, %rdi
callq 0x2100
cmpl $-0x1, %eax
je 0x7257
movl %eax, %ebp
leaq 0x439b(%rip), %rsi      # 0xb512
movl %eax, %edi
callq 0x22c0
testq %rax, %rax
je 0x7288
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2310
leaq 0x8283(%rip), %rdi      # 0xf430
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x2520
testq %rax, %rax
je 0x7201
movq %rax, %r15
leaq 0x8267(%rip), %r13      # 0xf430
movq %r15, %rdi
movq %r12, %rsi
callq 0x6ff6
movl %eax, %ebp
movq %r15, %rdi
movq %r14, %rsi
callq 0x2350
movq %r13, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x2520
movq %rax, %r15
testl %ebp, %ebp
jne 0x72aa
testq %r15, %r15
jne 0x71c9
leaq 0x4323(%rip), %rsi      # 0xb52b
xorl %r15d, %r15d
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2490
jmp 0x73a6
callq 0x2060
cmpl $0x2, (%rax)
jne 0x726a
leaq 0x42e7(%rip), %rsi      # 0xb515
movq %r14, %rdi
callq 0x23a0
testq %rax, %rax
je 0x726a
movq %rax, %rbx
movq %rax, %rdi
callq 0x22b0
movl %eax, %edi
movl $0x180, %esi            # imm = 0x180
callq 0x2160
jmp 0x7155
leaq 0x42ba(%rip), %rdi      # 0xb518
xorl %eax, %eax
callq 0x2110
jmp 0x7496
leaq 0x424a(%rip), %rdi      # 0xb4bb
movq %r14, %rsi
xorl %eax, %eax
callq 0x2110
movq %r14, %rdi
callq 0x23c0
jmp 0x749e
leaq 0x4293(%rip), %rdi      # 0xb522
xorl %eax, %eax
callq 0x2110
movq %rbx, %rdi
callq 0x2280
movl %ebp, %edi
callq 0x25c0
jmp 0x749e
testq %r15, %r15
je 0x73a3
leaq 0x8176(%rip), %r12      # 0xf430
movq %r15, %rdi
callq 0x6c43
testl %eax, %eax
jne 0x73a6
callq 0x22e0
movq (%rax), %rax
leaq -0x1(%r15), %r13
movsbq 0x1(%r13), %rcx
incq %r13
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x72d6
movzbl %cl, %eax
cmpl $0x23, %eax
je 0x737c
testl %eax, %eax
je 0x737c
movq %r13, %rdi
leaq 0x422e(%rip), %rsi      # 0xb531
callq 0x20a0
movslq %eax, %r12
xorl %ebp, %ebp
leaq 0x7fec(%rip), %rax      # 0xf300
movq (%rbp,%rax), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x2170
testl %eax, %eax
je 0x7336
addq $0x18, %rbp
cmpq $0x78, %rbp
jne 0x730d
xorl %ebp, %ebp
jmp 0x7340
leaq 0x7fc3(%rip), %rax      # 0xf300
addq %rax, %rbp
testq %rbp, %rbp
leaq 0x80e6(%rip), %r12      # 0xf430
je 0x737c
leaq 0x7fad(%rip), %rax      # 0xf300
xorl %ecx, %ecx
cmpq %rbp, %rax
jne 0x7362
movl $0x1, 0x10(%rsp,%rcx,4)
incq %rcx
addq $0x18, %rax
cmpq $0x5, %rcx
jne 0x7355
movq %rbp, %rdi
movq %r14, %rsi
callq 0x74b0
jmp 0x7387
movq %r15, %rdi
movq %r14, %rsi
callq 0x2350
movq %r12, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x2520
movq %rax, %r15
testq %rax, %rax
jne 0x72ba
xorl %r15d, %r15d
leaq 0x7f53(%rip), %r12      # 0xf300
xorl %r13d, %r13d
cmpl $0x0, 0x10(%rsp,%r13,4)
jne 0x73c3
movq %r12, %rdi
movq %r14, %rsi
callq 0x74b0
incq %r13
addq $0x18, %r12
cmpq $0x5, %r13
jne 0x73b0
testq %r15, %r15
je 0x73ff
leaq 0x8054(%rip), %r12      # 0xf430
movq %r15, %rdi
movq %r14, %rsi
callq 0x2350
movq %r12, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x2520
movq %rax, %r15
testq %rax, %rax
jne 0x73dc
movq %r14, %rdi
callq 0x2390
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2310
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2310
leaq 0x8007(%rip), %rdi      # 0xf430
movl $0x800, %esi            # imm = 0x800
movq %r14, %rdx
callq 0x2520
testq %rax, %rax
je 0x7462
leaq 0x7fee(%rip), %r12      # 0xf430
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2350
movq %r12, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r14, %rdx
callq 0x2520
testq %rax, %rax
jne 0x7442
movq %r14, %rdi
callq 0x2280
movq %rsp, %rdi
callq 0x2560
movq %rbx, %rdi
callq 0x2390
movq %rbx, %rdi
callq 0x22b0
movl %eax, %ebp
movq %rbx, %rdi
callq 0x2030
movl %ebp, %edi
movq %rax, %rsi
callq 0x2430
movq %rbx, %rdi
callq 0x2280
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq