// File: /workspace/github_c_makefile_star2_O3/dlbeer[P]ufat/main.c
// Function: cmd_write
static int cmd_write(struct ufat *uf, const struct options *opt)
{
	FILE *in;
	const char *basename;
	struct ufat_directory dir;
	struct ufat_dirent ent;
	struct ufat_file file;
	int err;

	if (!opt->argc) {
		fprintf(stderr, "You must specify a file path\n");
		return -1;
	}

	err = find_path(uf, &dir, opt->argv[0], &ent, &basename);
	if (err < 0)
		return -1;

	in = open_input(opt->in_file);
	if (!in)
		return -1;

	if (err) {
		time_t now = time(NULL);
		struct tm *local = localtime(&now);

		ent.attributes = 0;
		ent.create_date = UFAT_DATE(local->tm_year + 1900,
					    local->tm_mon + 1,
					    local->tm_mday);
		ent.create_time = UFAT_TIME(local->tm_hour,
					    local->tm_min,
					    local->tm_sec);
		ent.modify_date = ent.create_date;
		ent.modify_time = ent.create_time;
		ent.access_date = ent.create_date;

		err = ufat_dir_mkfile(&dir, &ent, basename);
		if (err < 0) {
			fprintf(stderr, "Failed to create file: %s: %s\n",
				basename, ufat_strerror(err));
			close_input(opt->in_file, in);
			return -1;
		}
	}

	err = ufat_open_file(uf, &file, &ent);
	if (err < 0) {
		fprintf(stderr, "ufat_open_file: %s\n", ufat_strerror(err));
		close_input(opt->in_file, in);
		return -1;
	}

	for (;;) {
		char buf[16384];
		int req_size = sizeof(buf);
		int len;

		if (opt->flags & OPTION_RANDOMIZE)
			req_size = random() % sizeof(buf) + 1;

		len = fread(buf, 1, req_size, in);
		if (ferror(stdin)) {
			perror("fread");
			close_input(opt->in_file, in);
			return -1;
		}

		if (!len)
			break;

		len = ufat_file_write(&file, buf, len);
		if (len < 0) {
			fprintf(stderr, "ufat_file_write: %s\n",
				ufat_strerror(len));
			close_input(opt->in_file, in);
			return -1;
		}
	}

	err = ufat_file_truncate(&file);
	if (err < 0) {
		fprintf(stderr, "ufat_file_truncate: %s\n",
			ufat_strerror(err));
		close_input(opt->in_file, in);
		return -1;
	}

	close_input(opt->in_file, in);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40a8, %rsp           # imm = 0x40A8
cmpl $0x0, 0x40(%rsi)
je 0x5b7d
movq %rsi, %r12
movq %rdi, %r13
movq 0x38(%rsi), %rax
movq (%rax), %rdx
leaq 0x38(%rsp), %rsi
leaq 0x58(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x6222
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x5c3c
movl %eax, %ebp
movq 0x18(%r12), %r15
testq %r15, %r15
je 0x5a18
leaq 0x216e(%rip), %rsi      # 0x7b69
movq %r15, %rdi
callq 0x11a0
movq %rax, %r14
testq %rax, %rax
jne 0x5a2b
movq %r15, %rdi
callq 0x1512
jmp 0x5c37
movq 0x55a9(%rip), %rax      # 0xafc8
movq (%rax), %r14
testq %r14, %r14
je 0x5c3c
leaq 0x18(%r12), %rbx
testl %ebp, %ebp
je 0x5ab0
xorl %edi, %edi
callq 0x1170
leaq 0xa0(%rsp), %rdi
movq %rax, (%rdi)
callq 0x1050
leaq 0x58(%rsp), %rsi
movl $0x0, 0x2c(%rsi)
movl 0x10(%rax), %edx
movl 0x14(%rax), %ecx
shll $0x9, %ecx
addl $0x6000, %ecx           # imm = 0x6000
shll $0x5, %edx
orl 0xc(%rax), %ecx
addl $0x20, %edx
orl %edx, %ecx
movw %cx, 0x30(%rsi)
movl 0x8(%rax), %edx
shll $0xb, %edx
movl (%rax), %edi
movl 0x4(%rax), %eax
shll $0x5, %eax
orl %edx, %eax
shrl %edi
orl %eax, %edi
movw %di, 0x32(%rsi)
movw %cx, 0x34(%rsi)
movw %di, 0x36(%rsi)
movw %cx, 0x38(%rsi)
movq 0x8(%rsp), %rdx
leaq 0x38(%rsp), %rdi
callq 0x2bd3
testl %eax, %eax
js 0x5bfc
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %r13, %rdi
callq 0x2e8c
testl %eax, %eax
js 0x5b87
movq %rbx, (%rsp)
leaq 0xa0(%rsp), %r13
movq 0x54eb(%rip), %rbx      # 0xafc8
leaq 0x10(%rsp), %rbp
movl $0x4000, %edx           # imm = 0x4000
testb $0x2, (%r12)
je 0x5aff
callq 0x1180
movq %rax, %rdx
andl $0x3fff, %edx           # imm = 0x3FFF
incq %rdx
movl $0x1, %esi
movq %r13, %rdi
movq %r14, %rcx
callq 0x10b0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x10a0
testl %eax, %eax
jne 0x5bae
testl %r15d, %r15d
je 0x5b52
movq %rbp, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x3164
testl %eax, %eax
jns 0x5ae2
movl %eax, %edi
movq (%rsp), %rsi
callq 0x154e
testb $0x1, %al
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
je 0x5b70
jmp 0x5c3c
leaq 0x10(%rsp), %rdi
callq 0x3429
testl %eax, %eax
js 0x5bcb
xorl %ebx, %ebx
movq (%rsp), %rax
cmpq $0x0, (%rax)
je 0x5c3c
movq %r14, %rdi
callq 0x10c0
jmp 0x5c3c
callq 0x158c
jmp 0x5c37
movq 0x546a(%rip), %rcx      # 0xaff8
movq (%rcx), %r15
movl %eax, %edi
callq 0x2102
leaq 0x1f56(%rip), %rsi      # 0x7af5
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x5c29
leaq 0x21f4(%rip), %rdi      # 0x7da9
callq 0x11b0
movq (%rsp), %rax
cmpq $0x0, (%rax)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jne 0x5b70
jmp 0x5c3c
movq 0x5426(%rip), %rcx      # 0xaff8
movq (%rcx), %r15
movl %eax, %edi
callq 0x2102
leaq 0x1f6e(%rip), %rsi      # 0x7b51
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq (%rsp), %rax
cmpq $0x0, (%rax)
jne 0x5c2f
jmp 0x5c37
movq 0x53f5(%rip), %rcx      # 0xaff8
movq (%rcx), %r15
movq 0x8(%rsp), %r12
movl %eax, %edi
callq 0x2102
leaq 0x1f04(%rip), %rsi      # 0x7b1d
movq %r15, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1140
cmpq $0x0, (%rbx)
je 0x5c37
movq %r14, %rdi
callq 0x10c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x40a8, %rsp           # imm = 0x40A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq