// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x4108, %rsp           # imm = 0x4108
movq %rdi, 0x40f8(%rsp)
movq %rsi, 0x40f0(%rsp)
movq 0x40f0(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x9ffd
movq 0x601c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x2bb5(%rip), %rsi      # 0xcb9b
movb $0x0, %al
callq 0x2120
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
movq 0x40f8(%rsp), %rdi
movq 0x40f0(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax), %rdx
leaq 0x40c0(%rsp), %rsi
leaq 0x4078(%rsp), %rcx
leaq 0x40e0(%rsp), %r8
callq 0xaf10
movl %eax, 0x404c(%rsp)
cmpl $0x0, 0x404c(%rsp)
jge 0xa052
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xb0f0
movq %rax, 0x40e8(%rsp)
cmpq $0x0, 0x40e8(%rsp)
jne 0xa086
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
cmpl $0x0, 0x404c(%rsp)
je 0xa203
xorl %eax, %eax
movl %eax, %edi
callq 0x2150
movq %rax, 0x4040(%rsp)
leaq 0x4040(%rsp), %rdi
callq 0x2040
movq %rax, 0x4038(%rsp)
movl $0x0, 0x40a4(%rsp)
movq 0x4038(%rsp), %rax
movl 0x14(%rax), %eax
addl $0x76c, %eax            # imm = 0x76C
subl $0x7bc, %eax            # imm = 0x7BC
shll $0x9, %eax
movq 0x4038(%rsp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
shll $0x5, %ecx
orl %ecx, %eax
movq 0x4038(%rsp), %rcx
orl 0xc(%rcx), %eax
movw %ax, 0x40a8(%rsp)
movq 0x4038(%rsp), %rax
movl 0x8(%rax), %eax
shll $0xb, %eax
movq 0x4038(%rsp), %rcx
movl 0x4(%rcx), %ecx
shll $0x5, %ecx
orl %ecx, %eax
movq 0x4038(%rsp), %rcx
movl (%rcx), %ecx
sarl %ecx
orl %ecx, %eax
movw %ax, 0x40aa(%rsp)
movw 0x40a8(%rsp), %ax
movw %ax, 0x40ac(%rsp)
movw 0x40aa(%rsp), %ax
movw %ax, 0x40ae(%rsp)
movw 0x40a8(%rsp), %ax
movw %ax, 0x40b0(%rsp)
movq 0x40e0(%rsp), %rdx
leaq 0x40c0(%rsp), %rdi
leaq 0x4078(%rsp), %rsi
callq 0x4920
movl %eax, 0x404c(%rsp)
cmpl $0x0, 0x404c(%rsp)
jge 0xa201
movq 0x5e5c(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x40e0(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x404c(%rsp), %edi
callq 0x2c40
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rax, %rcx
leaq 0x2b49(%rip), %rsi      # 0xcd1a
movb $0x0, %al
callq 0x2120
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40e8(%rsp), %rsi
callq 0xb190
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
jmp 0xa203
movq 0x40f8(%rsp), %rdi
leaq 0x4050(%rsp), %rsi
leaq 0x4078(%rsp), %rdx
callq 0x4f00
movl %eax, 0x404c(%rsp)
cmpl $0x0, 0x404c(%rsp)
jge 0xa28b
movq 0x5dc0(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x404c(%rsp), %edi
callq 0x2c40
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x2a97(%rip), %rsi      # 0xccf2
movb $0x0, %al
callq 0x2120
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40e8(%rsp), %rsi
callq 0xb190
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
jmp 0xa28d
movl $0x4000, 0x2c(%rsp)     # imm = 0x4000
movq 0x40f0(%rsp), %rax
movl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xa2ba
callq 0x2160
andq $0x3fff, %rax           # imm = 0x3FFF
addq $0x1, %rax
movl %eax, 0x2c(%rsp)
leaq 0x30(%rsp), %rdi
movslq 0x2c(%rsp), %rdx
movq 0x40e8(%rsp), %rcx
movl $0x1, %esi
callq 0x20a0
movl %eax, 0x28(%rsp)
movq 0x5ce7(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x2090
cmpl $0x0, %eax
je 0xa323
leaq 0x2cb1(%rip), %rdi      # 0xcfa6
callq 0x2190
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40e8(%rsp), %rsi
callq 0xb190
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
cmpl $0x0, 0x28(%rsp)
jne 0xa32c
jmp 0xa3a9
leaq 0x30(%rsp), %rsi
movl 0x28(%rsp), %edx
leaq 0x4050(%rsp), %rdi
callq 0x55b0
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x28(%rsp)
jge 0xa3a4
movq 0x5ca4(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl 0x28(%rsp), %edi
callq 0x2c40
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x29c5(%rip), %rsi      # 0xcd39
movb $0x0, %al
callq 0x2120
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40e8(%rsp), %rsi
callq 0xb190
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
jmp 0xa28d
leaq 0x4050(%rsp), %rdi
callq 0x5ad0
movl %eax, 0x404c(%rsp)
cmpl $0x0, 0x404c(%rsp)
jge 0xa41c
movq 0x5c2a(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, (%rsp)
movl 0x404c(%rsp), %edi
callq 0x2c40
movq (%rsp), %rdi
movq %rax, %rdx
leaq 0x295f(%rip), %rsi      # 0xcd4e
movb $0x0, %al
callq 0x2120
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40e8(%rsp), %rsi
callq 0xb190
movl $0xffffffff, 0x4104(%rsp) # imm = 0xFFFFFFFF
jmp 0xa440
movq 0x40f0(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40e8(%rsp), %rsi
callq 0xb190
movl $0x0, 0x4104(%rsp)
movl 0x4104(%rsp), %eax
addq $0x4108, %rsp           # imm = 0x4108
retq
nop