// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %r12
movabsq $0x5858582f706d742f, %rax # imm = 0x5858582F706D742F
movq %rax, 0x10(%rsp)
movl $0x585858, 0x18(%rsp)   # imm = 0x585858
callq 0x6898
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x73a4
movq %rax, %r14
leaq 0x44b4(%rip), %rsi      # 0xb4f2
movq %rax, %rdi
callq 0x23a0
movq %rax, %rbx
testq %rax, %rax
je 0x7121
movq %r14, %rdi
callq 0x23c0
leaq 0x10(%rsp), %rdi
callq 0x2100
cmpl $-0x1, %eax
je 0x715b
movl %eax, %ebp
leaq 0x447c(%rip), %rsi      # 0xb4f2
movl %eax, %edi
callq 0x22c0
testq %rax, %rax
je 0x718c
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0x0, 0x30(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2310
leaq 0x8384(%rip), %rdi      # 0xf430
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x2520
testq %rax, %rax
movq %rbx, 0x8(%rsp)
je 0x7105
movq %rax, %r15
leaq 0x8363(%rip), %r13      # 0xf430
movq %r15, %rdi
movq %r12, %rsi
callq 0x6f06
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 0x71ae
testq %r15, %r15
jne 0x70cd
leaq 0x43ff(%rip), %rsi      # 0xb50b
xorl %r15d, %r15d
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2490
jmp 0x72a7
callq 0x2060
cmpl $0x2, (%rax)
jne 0x716e
leaq 0x43c3(%rip), %rsi      # 0xb4f5
movq %r14, %rdi
callq 0x23a0
testq %rax, %rax
je 0x716e
movq %rax, %rbx
movq %rax, %rdi
callq 0x22b0
movl %eax, %edi
movl $0x180, %esi            # imm = 0x180
callq 0x2160
jmp 0x7052
leaq 0x4396(%rip), %rdi      # 0xb4f8
xorl %eax, %eax
callq 0x2110
jmp 0x739c
leaq 0x4326(%rip), %rdi      # 0xb49b
movq %r14, %rsi
xorl %eax, %eax
callq 0x2110
movq %r14, %rdi
callq 0x23c0
jmp 0x73a4
leaq 0x436f(%rip), %rdi      # 0xb502
xorl %eax, %eax
callq 0x2110
movq %rbx, %rdi
callq 0x2280
movl %ebp, %edi
callq 0x25c0
jmp 0x73a4
testq %r15, %r15
je 0x72a4
leaq 0x8272(%rip), %r12      # 0xf430
movq %r15, %rdi
callq 0x6b74
testl %eax, %eax
jne 0x72a7
callq 0x22e0
movq (%rax), %rax
leaq -0x1(%r15), %rbp
movsbq 0x1(%rbp), %rcx
incq %rbp
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x71da
movzbl %cl, %eax
cmpl $0x23, %eax
je 0x71f5
testl %eax, %eax
jne 0x7205
movq %r15, %rdi
movq %r14, %rsi
callq 0x2350
jmp 0x7288
movq %rbp, %rdi
leaq 0x4302(%rip), %rsi      # 0xb511
callq 0x20a0
movslq %eax, %r12
leaq 0x80e2(%rip), %r13      # 0xf300
xorl %ebx, %ebx
movq (%r13), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x2170
testl %eax, %eax
je 0x7259
incq %rbx
addq $0x18, %r13
cmpq $0x5, %rbx
jne 0x7220
movq %r15, %rdi
movq %r14, %rsi
callq 0x2350
movq 0x8(%rsp), %rbx
leaq 0x81d9(%rip), %r12      # 0xf430
jmp 0x7288
xorl %eax, %eax
leaq 0x81ce(%rip), %r12      # 0xf430
cmpq %rax, %rbx
jne 0x726f
movl $0x1, 0x20(%rsp,%rbx,4)
incq %rax
cmpq $0x5, %rax
jne 0x7262
movq %r13, %rdi
movq %r14, %rsi
callq 0x73b6
movq 0x8(%rsp), %rbx
movq %r12, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x2520
movq %rax, %r15
testq %rax, %rax
jne 0x71be
xorl %r15d, %r15d
leaq 0x8052(%rip), %r12      # 0xf300
xorl %ebx, %ebx
cmpl $0x0, 0x20(%rsp,%rbx,4)
jne 0x72c2
movq %r12, %rdi
movq %r14, %rsi
callq 0x73b6
incq %rbx
addq $0x18, %r12
cmpq $0x5, %rbx
jne 0x72b0
testq %r15, %r15
movq 0x8(%rsp), %rbx
je 0x7303
leaq 0x8150(%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 0x72e0
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 0x8103(%rip), %rdi      # 0xf430
movl $0x800, %esi            # imm = 0x800
movq %r14, %rdx
callq 0x2520
testq %rax, %rax
je 0x7366
leaq 0x80ea(%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 0x7346
movq %r14, %rdi
callq 0x2280
leaq 0x10(%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 $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq