// File: /workspace/github_c_makefile_star2_O1/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 0x5ca4
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 0x6324
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x5cae
movl %eax, %ebp
movq 0x18(%r12), %r15
testq %r15, %r15
je 0x5b11
leaq 0x2075(%rip), %rsi      # 0x7b69
movq %r15, %rdi
callq 0x11a0
movq %rax, %r14
testq %rax, %rax
jne 0x5b1b
movq %r15, %rdi
callq 0x1538
xorl %r14d, %r14d
jmp 0x5b1b
movq 0x54b0(%rip), %rax      # 0xafc8
movq (%rax), %r14
testq %r14, %r14
je 0x5cae
leaq 0x18(%r12), %rax
movq %rax, (%rsp)
testl %ebp, %ebp
je 0x5bad
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 0x2d1c
testl %eax, %eax
js 0x5cdc
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %r13, %rdi
callq 0x3074
testl %eax, %eax
js 0x5cc2
leaq 0xa0(%rsp), %r13
movq 0x53f2(%rip), %rbx      # 0xafc8
leaq 0x10(%rsp), %rbp
movl $0x4000, %edx           # imm = 0x4000
testb $0x2, (%r12)
je 0x5bf8
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 0x5c51
testl %r15d, %r15d
je 0x5c46
movq %rbp, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x335a
xorl %ecx, %ecx
testl %eax, %eax
jns 0x5c4b
movl %eax, %edi
movq (%rsp), %rsi
callq 0x1574
movl $0x1, %ecx
testb $0x1, %al
jne 0x5c4b
jmp 0x5c67
movl $0x2, %ecx
testl %ecx, %ecx
je 0x5bdb
jmp 0x5c76
leaq 0x2151(%rip), %rdi      # 0x7da9
callq 0x11b0
movq (%rsp), %rax
cmpq $0x0, (%rax)
je 0x5c6f
movq %r14, %rdi
callq 0x10c0
movl $0x1, %ecx
jmp 0x5c4b
cmpl $0x2, %ecx
jne 0x5ca9
leaq 0x10(%rsp), %rdi
callq 0x35dd
testl %eax, %eax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
js 0x5d0b
xorl %ebx, %ebx
movq (%rsp), %rax
cmpq $0x0, (%rax)
je 0x5cae
movq %r14, %rdi
callq 0x10c0
jmp 0x5cae
callq 0x15b2
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
movq 0x532f(%rip), %rcx      # 0xaff8
movq (%rcx), %r15
movl %eax, %edi
callq 0x2188
leaq 0x1e1b(%rip), %rsi      # 0x7af5
jmp 0x5d23
movq 0x5315(%rip), %rcx      # 0xaff8
movq (%rcx), %r15
movq 0x8(%rsp), %r12
movl %eax, %edi
callq 0x2188
leaq 0x1e24(%rip), %rsi      # 0x7b1d
movq %r15, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1140
jmp 0x5c90
movq 0x52e6(%rip), %rcx      # 0xaff8
movq (%rcx), %r15
movl %eax, %edi
callq 0x2188
leaq 0x1e2e(%rip), %rsi      # 0x7b51
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x5c90