// File: /workspace/github_c_makefile_star2_O2/findstr[P]version-database/patch.c
// Function: patch
void
patch(struct patch_args *args)
{
	dr_t patch;
	dr_t outdir, outtemp = NULL;
	int size, dfn = 0;
	const uint8_t *p, *e;
	outdir = args->output;
	checkout(args->hash, outdir);
	patch = dir_readfile(str(args->patch), NULL);
	if (patch == NULL) {
		fprintf(stderr, "patch file '%s' %s\n",
			args->patch->buf, strerror(errno));
		exit(EINVAL);
	}
	//prepare temp file
	printf("DFF/DFX\n");
	size = outdir->size;
	outtemp = dr_new(size + 1 + sizeof(TEMP) - 1, NULL);
	memcpy(outtemp->buf, outdir->buf, size);
	outtemp->buf[size] = '/';
	memcpy(&outtemp->buf[size+1], TEMP, sizeof(TEMP) - 1);
	p = patch->buf;
	e = p + patch->size;
	while (p < e) {
		struct CTRL ctrl;
		dr_t namea, patchd;
		dr_t newfile = NULL, oldfile = NULL;
		//printf("offset:%ld\n", p - patch->buf);
		p = ctrl_read(p, &ctrl);
		switch (ctrl.act) {
		case CTRL_DFF:
			namea = ctrl.u.dff.name;
			patchd = ctrl.u.dff.patch;
			goto diff;
		case CTRL_DFX:
			namea = ctrl.u.dfx.namea;
			patchd = ctrl.u.dfx.patch;
			diff:
			++dfn;
			oldfile = dir_readfile(str(namea), outdir);
			newfile = patch_content(oldfile, patchd);
			//printf("ref:%d\n", newfile->ref);
			dir_writefile(str(ctrl.u.dff.name), newfile, outtemp);
			break;
		case CTRL_NEW:
			dir_writefile(str(ctrl.u.new.name),
				ctrl.u.new.data, outtemp);
			break;
		case CTRL_CPY:
			namea = ctrl.u.cpy.namea;
			oldfile = dir_readfile(str(namea), outdir);
			dir_writefile(str(ctrl.u.cpy.name), oldfile, outtemp);
			break;
		case CTRL_DEL:
			break;
		}
		dr_unref(oldfile);
		dr_unref(newfile);
		ctrl_destroy(&ctrl);
	}
	printf("NEW/MOV/DEL\n");
	p = patch->buf;
	e = p + patch->size;
	while (p < e) {
		struct CTRL ctrl;
		dr_t newfile = NULL, oldfile = NULL;
		p = ctrl_read(p, &ctrl);
		switch (ctrl.act) {
		case CTRL_DEL:
			dir_remove(str(ctrl.u.del.name), outdir);
			break;
		}
		dr_unref(oldfile);
		dr_unref(newfile);
		ctrl_destroy(&ctrl);
	}
	printf("apply DFF/DFX result\n");
	if (dfn > 0)
		dir_movdir(outtemp, outdir);
	dr_unref(patch);
	dr_unref(outtemp);
	return ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x10(%rbx), %rsi
movq %rsi, -0x38(%rbp)
callq 0x41ec
movq 0x8(%rbx), %rdi
addq $0x8, %rdi
xorl %esi, %esi
callq 0x4605
testq %rax, %rax
je 0x4061
movq %rax, %r14
leaq 0x476e(%rip), %rdi      # 0x8483
callq 0x20a0
movq -0x38(%rbp), %r15
movslq 0x4(%r15), %rbx
leal 0x6(%rbx), %edi
callq 0x409e
movq %rax, %r12
leaq 0x8(%rax), %rdi
leaq 0x8(%r15), %rsi
movq %rbx, %rdx
callq 0x21c0
movb $0x2f, 0x8(%r12,%rbx)
movl $0x6d65745f, 0x9(%r12,%rbx) # imm = 0x6D65745F
movq %r12, -0x48(%rbp)
movb $0x70, 0xd(%r12,%rbx)
movslq 0x4(%r14), %rax
leaq (%r14,%rax), %rbx
addq $0x8, %rbx
movq %r14, -0x78(%rbp)
addq $0x8, %r14
leaq -0x68(%rbp), %r12
movq %r14, -0x80(%rbp)
movq %r14, %rdi
xorl %r13d, %r13d
movq %rbx, -0x88(%rbp)
cmpq %rbx, %rdi
jae 0x3f76
leaq -0x70(%rbp), %rsi
callq 0x59e9
movq %rax, -0x90(%rbp)
movzbl -0x70(%rbp), %eax
addl $-0x10, %eax
roll $0x1c, %eax
cmpl $0x3, %eax
ja 0x3dd3
leaq 0x462e(%rip), %rcx      # 0x83dc
movslq (%rcx,%rax,4), %rdx
addq %rcx, %rdx
movq %r12, %rax
leaq -0x60(%rbp), %rcx
jmpq *%rdx
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x8, %rdi
movq -0x48(%rbp), %rdx
callq 0x475e
xorl %r15d, %r15d
xorl %edi, %edi
jmp 0x3f54
leaq -0x60(%rbp), %rax
leaq -0x58(%rbp), %rcx
movq (%rax), %rdi
movq (%rcx), %rbx
incl %r13d
movl %r13d, -0x2c(%rbp)
addq $0x8, %rdi
movq -0x38(%rbp), %rsi
callq 0x4605
movq %rax, %r14
leaq 0x8(%rbx), %rdi
movslq 0x4(%rbx), %rax
addq %rbx, %rax
addq $0x8, %rax
movq %rax, -0xa0(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0x5ca3
movq %rax, %r15
movl -0x3c(%rbp), %edi
callq 0x409e
movq %rax, %r13
movq %rax, -0x50(%rbp)
addq $0x8, %r13
movl 0x4(%rbx), %esi
movl -0x3c(%rbp), %edx
leaq 0x45bc(%rip), %rdi      # 0x8400
xorl %eax, %eax
callq 0x2110
movq %r14, %rax
addq $0x8, %rax
movq %rax, -0x98(%rbp)
movq %r13, %r12
cmpq -0xa0(%rbp), %r15
jae 0x3efa
movq %r15, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x5d33
movq %rax, %r15
movzbl -0xb8(%rbp), %eax
cmpl $0x2, %eax
je 0x3ebf
cmpl $0x1, %eax
jne 0x4012
movl -0xac(%rbp), %ebx
movq -0x50(%rbp), %rax
movslq 0x4(%rax), %rax
movq %r13, %rcx
subq %r12, %rcx
addq %rax, %rcx
cmpq %rbx, %rcx
jl 0x402b
movl -0xb0(%rbp), %esi
addq -0x98(%rbp), %rsi
jmp 0x3ee7
movslq -0xa8(%rbp), %rbx
movq -0x50(%rbp), %rax
movslq 0x4(%rax), %rax
movq %r13, %rcx
subq %r12, %rcx
addq %rax, %rcx
cmpq %rbx, %rcx
jl 0x4044
movq -0xb0(%rbp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x21c0
addq %rbx, %r12
jmp 0x3e5c
movq -0x68(%rbp), %rdi
addq $0x8, %rdi
movq -0x50(%rbp), %r15
movq %r15, %rsi
movq -0x48(%rbp), %rdx
callq 0x475e
movl -0x2c(%rbp), %r13d
movq -0x88(%rbp), %rbx
leaq -0x68(%rbp), %r12
movq %r14, %rdi
jmp 0x3f54
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq -0x38(%rbp), %rsi
callq 0x4605
movq %rax, %r15
movq -0x68(%rbp), %rdi
addq $0x8, %rdi
movq %rax, %rsi
movq -0x48(%rbp), %rdx
callq 0x475e
movq %r15, %rdi
xorl %r15d, %r15d
callq 0x40c5
movq %r15, %rdi
callq 0x40c5
leaq -0x70(%rbp), %rdi
callq 0x5b65
movq -0x90(%rbp), %rdi
jmp 0x3d7f
movl %r13d, -0x2c(%rbp)
leaq 0x450a(%rip), %rdi      # 0x848b
callq 0x20a0
movq -0x78(%rbp), %rax
movslq 0x4(%rax), %r14
movq -0x80(%rbp), %r12
addq %r12, %r14
leaq -0x70(%rbp), %rbx
cmpq %r14, %r12
jae 0x3fcd
movq %r12, %rdi
movq %rbx, %rsi
callq 0x59e9
movq %rax, %r12
cmpb $0x50, -0x70(%rbp)
jne 0x3fc3
movq -0x68(%rbp), %rdi
addq $0x8, %rdi
movq -0x38(%rbp), %rsi
callq 0x48a8
movq %rbx, %rdi
callq 0x5b65
jmp 0x3f99
leaq 0x44c3(%rip), %rdi      # 0x8497
callq 0x20a0
cmpl $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %rbx
jle 0x3fef
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x48d6
movq -0x78(%rbp), %rdi
callq 0x40c5
movq %rbx, %rdi
callq 0x40c5
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x497f(%rip), %rdi      # 0x8998
leaq 0x441b(%rip), %rsi      # 0x843b
leaq 0x441c(%rip), %rcx      # 0x8443
pushq $0x2b
jmp 0x405b
leaq 0x43e6(%rip), %rdi      # 0x8418
leaq 0x4402(%rip), %rsi      # 0x843b
leaq 0x4403(%rip), %rcx      # 0x8443
pushq $0x22
jmp 0x405b
leaq 0x4417(%rip), %rdi      # 0x8462
leaq 0x43e9(%rip), %rsi      # 0x843b
leaq 0x43ea(%rip), %rcx      # 0x8443
pushq $0x27
popq %rdx
callq 0x2130
movq 0x6f90(%rip), %rax      # 0xaff8
movq (%rax), %r14
movq 0x8(%rbx), %rbx
addq $0x8, %rbx
callq 0x2050
movl (%rax), %edi
callq 0x22a0
leaq 0x4366(%rip), %rsi      # 0x83ec
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2190
pushq $0x16
popq %rdi
callq 0x2270