// File: /workspace/github_c_makefile_star2_O1/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), %r13
movq %r13, %rsi
callq 0x4de8
movq 0x8(%rbx), %rdi
addq $0x8, %rdi
xorl %esi, %esi
callq 0x51ea
testq %rax, %rax
je 0x4cf0
movq %rax, %r15
leaq 0x5bb7(%rip), %rdi      # 0xa47f
callq 0x20a0
movslq 0x4(%r13), %rbx
leal 0x6(%rbx), %eax
movslq %eax, %r14
leaq 0xc(%r14), %rdi
callq 0x21f0
movq %rax, %r12
movl $0x1, (%rax)
movl %r14d, 0x4(%rax)
leaq 0x8(%rax), %rdi
movb $0x0, 0x8(%rax,%r14)
leaq 0x8(%r13), %rsi
movq %rbx, %rdx
callq 0x21c0
movb $0x2f, 0x8(%r12,%rbx)
movl $0x6d65745f, 0x9(%r12,%rbx) # imm = 0x6D65745F
movb $0x70, 0xd(%r12,%rbx)
leaq 0x8(%r15), %rcx
movslq 0x4(%r15), %rax
testq %rax, %rax
movq %r15, -0x40(%rbp)
movq %rcx, -0x78(%rbp)
jle 0x4ba6
leaq (%r15,%rax), %rbx
addq $0x8, %rbx
leaq -0x58(%rbp), %r14
leaq -0x68(%rbp), %r15
movl $0x0, -0x34(%rbp)
movq %rcx, %rdi
movq %r13, -0x90(%rbp)
movq %r12, -0x88(%rbp)
movq %rbx, -0x80(%rbp)
movq %r15, %rsi
callq 0x6b10
movq %rax, -0x98(%rbp)
movzbl -0x68(%rbp), %eax
addl $-0x10, %eax
roll $0x1c, %eax
cmpl $0x3, %eax
ja 0x49a6
leaq 0x5a56(%rip), %rcx      # 0xa3d8
movslq (%rcx,%rax,4), %rdx
addq %rcx, %rdx
leaq -0x60(%rbp), %rax
movq %r14, %rcx
jmpq *%rdx
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
addq $0x8, %rdi
movq %r12, %rdx
callq 0x530e
xorl %r15d, %r15d
xorl %edi, %edi
jmp 0x4b3c
movq %r14, %rax
leaq -0x50(%rbp), %rcx
movq (%rax), %rdi
movq (%rcx), %r12
addq $0x8, %rdi
movq %r13, %rsi
callq 0x51ea
movq %rax, -0x70(%rbp)
leaq 0x8(%r12), %rdi
movslq 0x4(%r12), %rax
leaq (%r12,%rax), %r13
addq $0x8, %r13
leaq -0x44(%rbp), %rsi
callq 0x70dd
movq %rax, %r14
movslq -0x44(%rbp), %rbx
leaq 0xc(%rbx), %rdi
callq 0x21f0
movl $0x1, (%rax)
movl %ebx, 0x4(%rax)
movq %rax, -0x30(%rbp)
movb $0x0, 0x8(%rax,%rbx)
movl 0x4(%r12), %esi
leaq 0x59e6(%rip), %rdi      # 0xa3fc
movl %ebx, %edx
xorl %eax, %eax
callq 0x2110
cmpq %r13, %r14
jae 0x4ada
movq %r13, %r15
movq -0x30(%rbp), %r13
addq $0x8, %r13
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xa0(%rbp)
movq %r13, %r12
movq %r14, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7333
movq %rax, %r14
movzbl -0xb8(%rbp), %eax
cmpl $0x2, %eax
je 0x4a9b
cmpl $0x1, %eax
jne 0x4c93
movl -0xac(%rbp), %ebx
movq -0x30(%rbp), %rax
movslq 0x4(%rax), %rax
movq %r13, %rcx
subq %r12, %rcx
addq %rax, %rcx
cmpq %rbx, %rcx
jl 0x4cb2
movl -0xb0(%rbp), %esi
addq -0xa0(%rbp), %rsi
jmp 0x4ac3
movslq -0xa8(%rbp), %rbx
movq -0x30(%rbp), %rax
movslq 0x4(%rax), %rax
movq %r13, %rcx
subq %r12, %rcx
addq %rax, %rcx
cmpq %rbx, %rcx
jl 0x4c74
movq -0xb0(%rbp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x21c0
addq %rbx, %r12
cmpq %r15, %r14
jb 0x4a45
incl -0x34(%rbp)
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq -0x30(%rbp), %r15
movq %r15, %rsi
movq -0x88(%rbp), %r12
movq %r12, %rdx
callq 0x530e
movq -0x90(%rbp), %r13
movq -0x80(%rbp), %rbx
leaq -0x58(%rbp), %r14
movq -0x70(%rbp), %rdi
jmp 0x4b3c
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x51ea
movq %rax, %r15
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x530e
movq %r15, %rdi
xorl %r15d, %r15d
testq %rdi, %rdi
je 0x4b5a
movl (%rdi), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%rdi)
cmpl $0x1, %ecx
jg 0x4b5a
testl %eax, %eax
jne 0x4cd1
callq 0x2030
testq %r15, %r15
je 0x4b7d
movl (%r15), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%r15)
cmpl $0x1, %ecx
jg 0x4b7d
testl %eax, %eax
jne 0x4cd1
movq %r15, %rdi
callq 0x2030
leaq -0x68(%rbp), %r15
movq %r15, %rdi
callq 0x6edb
movq -0x98(%rbp), %rdi
cmpq %rbx, %rdi
jb 0x495d
cmpl $0x0, -0x34(%rbp)
setg %al
movq %rax, -0x30(%rbp)
jmp 0x4bae
movq $0x0, -0x30(%rbp)
leaq 0x58d2(%rip), %rdi      # 0xa487
callq 0x20a0
movq -0x40(%rbp), %rax
movslq 0x4(%rax), %rax
testq %rax, %rax
movq -0x78(%rbp), %r14
jle 0x4c0c
movq -0x40(%rbp), %rcx
leaq (%rcx,%rax), %r15
addq $0x8, %r15
leaq -0x68(%rbp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6b10
movq %rax, %r14
cmpb $0x50, -0x68(%rbp)
jne 0x4bff
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x5428
movq %rbx, %rdi
callq 0x6edb
cmpq %r15, %r14
jb 0x4bdb
leaq 0x5880(%rip), %rdi      # 0xa493
callq 0x20a0
cmpb $0x0, -0x30(%rbp)
je 0x4c29
movq %r12, %rdi
movq %r13, %rsi
callq 0x5456
movq -0x40(%rbp), %rdi
movl (%rdi), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%rdi)
cmpl $0x1, %ecx
jg 0x4c46
testl %eax, %eax
jne 0x4cd1
callq 0x2030
movl (%r12), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%r12)
cmpl $0x1, %ecx
jg 0x4c62
testl %eax, %eax
jne 0x4cd1
movq %r12, %rdi
callq 0x2030
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x57e3(%rip), %rdi      # 0xa45e
leaq 0x57b5(%rip), %rsi      # 0xa437
leaq 0x57b6(%rip), %rcx      # 0xa43f
movl $0x27, %edx
callq 0x2130
leaq 0x5cfa(%rip), %rdi      # 0xa994
leaq 0x5796(%rip), %rsi      # 0xa437
leaq 0x5797(%rip), %rcx      # 0xa43f
movl $0x2b, %edx
callq 0x2130
leaq 0x575b(%rip), %rdi      # 0xa414
leaq 0x5777(%rip), %rsi      # 0xa437
leaq 0x5778(%rip), %rcx      # 0xa43f
movl $0x22, %edx
callq 0x2130
leaq 0x53a9(%rip), %rdi      # 0xa081
leaq 0x53ae(%rip), %rsi      # 0xa08d
leaq 0x53ae(%rip), %rcx      # 0xa094
movl $0x2d, %edx
callq 0x2130
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x23fd