// File: /workspace/github_c_makefile_star2_O2/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 0x52ff
movq %rsi, %r14
movq %rdi, %r12
movq 0x38(%rsi), %rax
movq (%rax), %rdx
leaq 0x38(%rsp), %rsi
leaq 0x58(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x5b06
pushq $-0x1
popq %r15
testl %eax, %eax
js 0x53c4
movl %eax, %ebp
movq 0x18(%r14), %r13
testq %r13, %r13
je 0x51a6
leaq 0x1a06(%rip), %rsi      # 0x6b69
movq %r13, %rdi
callq 0x11a0
movq %rax, (%rsp)
testq %rax, %rax
jne 0x51bd
movq 0x4e7d(%rip), %rax      # 0x9ff8
movq (%rax), %rbx
callq 0x1060
movl (%rax), %edi
callq 0x11f0
leaq 0x19db(%rip), %rsi      # 0x6b6c
movq %rbx, %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1140
jmp 0x53c4
movq 0x4e1b(%rip), %rax      # 0x9fc8
movq (%rax), %rax
movq %rax, (%rsp)
testq %rax, %rax
je 0x53c4
testl %ebp, %ebp
je 0x523a
xorl %edi, %edi
callq 0x1170
leaq 0xa0(%rsp), %rdi
movq %rax, (%rdi)
callq 0x1050
leaq 0x58(%rsp), %rsi
andl $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 0x24a8
testl %eax, %eax
js 0x538a
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %r12, %rdi
callq 0x274c
testl %eax, %eax
js 0x531d
leaq 0xa0(%rsp), %r12
pushq $0x1
popq %r13
movq 0x4d61(%rip), %r15      # 0x9fc8
leaq 0x10(%rsp), %rbp
movl $0x4000, %edx           # imm = 0x4000
testb $0x2, (%r14)
je 0x5288
callq 0x1180
movq %rax, %rdx
andl $0x3fff, %edx           # imm = 0x3FFF
incq %rdx
movq %r12, %rdi
movq %r13, %rsi
movq (%rsp), %rcx
callq 0x10b0
movq %rax, %rbx
movq (%r15), %rdi
callq 0x10a0
testl %eax, %eax
jne 0x5344
testl %ebx, %ebx
je 0x52dc
movq %rbp, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x2a12
testl %eax, %eax
jns 0x526c
movq 0x4d32(%rip), %rcx      # 0x9ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x1a87
leaq 0x1865(%rip), %rsi      # 0x6b3c
jmp 0x536a
leaq 0x10(%rsp), %rdi
callq 0x2c87
testl %eax, %eax
js 0x5352
movq 0x18(%r14), %rdi
movq (%rsp), %rsi
callq 0x5c03
xorl %r15d, %r15d
jmp 0x53c4
movq 0x4cf2(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x168e(%rip), %rdi      # 0x699e
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x11e0
jmp 0x5384
movq 0x4cd4(%rip), %rcx      # 0x9ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x1a87
leaq 0x17c0(%rip), %rsi      # 0x6af5
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x53b7
leaq 0x1a5e(%rip), %rdi      # 0x6da9
callq 0x11b0
jmp 0x5377
movq 0x4c9f(%rip), %rcx      # 0x9ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x1a87
leaq 0x17e7(%rip), %rsi      # 0x6b51
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x18(%r14), %rdi
movq (%rsp), %rsi
callq 0x5c03
pushq $-0x1
popq %r15
jmp 0x53c4
movq 0x4c67(%rip), %rcx      # 0x9ff8
movq (%rcx), %rbx
movq 0x8(%rsp), %r12
movl %eax, %edi
callq 0x1a87
leaq 0x1776(%rip), %rsi      # 0x6b1d
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1140
movq 0x18(%r14), %rdi
movq (%rsp), %rsi
callq 0x5c03
movl %r15d, %eax
addq $0x40a8, %rsp           # imm = 0x40A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq