// File: /workspace/github_c_makefile_star2_O3/mist64[P]cbm6502asm/src/macrodef.c
// Function: macro_def
void macro_def(char *macprmbgn, char **maclinptr, char *macargbgn, int mactype) {
/* mactype: 1=.MACRO 2=.REPT 3=.IRP 4=.IRPC */
	unsigned int i, l, m, macrocnt = 1;

	spechar = 'D';
	print = 1;
	outline((char)0, (char)0, (char *)0, (char)0, (char)1);
	newmac = (struct macdef *)malloc(
	        l = CHUNK); /* initial size of structure */
	                    /*
	                      save macro name
	                    */
	for (m = i = 0; s1[i]; i++) newmac->name[m++] = s1[i]; /* copy name */
	newmac->name[31] = newmac->name[m++] = 0; /* terminate name string */
	m = m > 32 ? 32 : m;                /* fix up if name was > 31 chars */
	if (!strlen(newmac->name))          /* check for missing name */
		display_error(error = 'D'); /* missing macro name */
	                                    /*
	                                      get macro's parameter list
	                                    */
	parses3(1);                         /* separate fields in operand */
	while (*s3 || s3[1])                /* copy parms to macro definition */
		newmac->name[m++] = *s3++;
	newmac->name[m++] = 0;
	newmac->name[m++] = 0;
	/*
	  store the macro definition lines
	*/
	i = 1;
	while (i) {
		get_line(macprmbgn, maclinptr,
		         macargbgn); /* get a source or macro exp line */
		print = 1;

		if (strcmp(s2, ".MACRO") ==
		            0 || /* check for nested macro defs */
		    strcmp(s2, ".IRP") == 0 ||
		    strcmp(s2, ".IRPC") == 0 || strcmp(s2, ".REPT") == 0)
			macrocnt++;
		if (strcmp(s2, ".ENDM") == 0 || strcmp(s2, ".ENDR") == 0)
			macrocnt--;

		if (!strcmp(s2, ".END"))
			error_msg(
			        ".END encountered during definition of macro: ",
			        newmac->name, 4);

		spechar = 'D';
		/* check for end of macro definition */
		if (((!strcmp(s2, ".ENDM") && mactype == 1) ||
		     (!strcmp(s2, ".ENDR") && mactype > 1)) &&
		    !macrocnt) {
			newmac->name[m++] =
			        0; /* add a null indicating macro end */
			newmac->size = m + 8;
			if (error) {
				free((char *)newmac); /* don't save macro if
				                         error */
				return;
			}
			newmac->nextmac = macro_head; /* link macro definition
			                                 into list */
			/* truncated memory block */
			tmpmac =
			        (struct macdef *)realloc((char *)newmac, m + 8);
			if (!(long)(macro_head = tmpmac))
				error_msg("Available memory exceeded",
				          newmac->name, 4);
			return; /* finished */
		}
		outline((char)0, (char)0, (char *)0, (char)0, (char)1);
		/* check for macro def in macro */
		if (m + 8 + LINELENG > l) { /* alloc more memory if needed */
			tmpmac = (struct macdef *)realloc((char *)newmac,
			                                  l += CHUNK);
			if (!(long)tmpmac) /* check if allocation sucessful */
				error_msg("Available memory exceeded",
				          newmac->name, 4);
			newmac = tmpmac; /* allocation was successful */
		}
		if (strlen(line)) {
			strcpy(&newmac->name[m], line); /* copy the line */
			m += 1 + strlen(line); /* add length of line and null */
		} else {
			newmac->name[m++] = ' '; /* blank line */
			newmac->name[m++] = 0;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x83f2(%rip), %rax      # 0xdc16
movb $0x44, (%rax)
leaq 0x85be(%rip), %rax      # 0xddec
movl $0x1, (%rax)
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x7be8
movl $0x200, %edi            # imm = 0x200
callq 0x1180
leaq 0x8426(%rip), %r14      # 0xdc80
movq %rax, (%r14)
leaq 0x8224(%rip), %rcx      # 0xda88
movq (%rcx), %rdx
movb (%rdx), %dl
testb %dl, %dl
je 0x5887
xorl %ebx, %ebx
movq (%r14), %rax
movl %ebx, %esi
incl %ebx
movb %dl, 0xc(%rax,%rsi)
movq (%rcx), %rax
movb (%rax,%rbx), %dl
testb %dl, %dl
jne 0x586f
movq (%r14), %rax
movl %ebx, %ecx
incl %ebx
xorl %edx, %edx
movb %dl, 0xc(%rax,%rcx)
movq (%r14), %rax
movb %dl, 0x2b(%rax)
cmpl $0x20, %ebx
movl $0x20, %ebp
cmovbl %ebx, %ebp
cmpb %dl, 0xc(%rax)
jne 0x58bb
leaq 0x8542(%rip), %rax      # 0xddf0
movb $0x44, (%rax)
movl $0x44, %edi
callq 0x1ec8
movl $0x1, %edi
callq 0x5204
leaq 0x87d4(%rip), %rax      # 0xe0a0
movq (%rax), %rdx
cmpb $0x0, (%rdx)
movl %ebp, %ecx
jne 0x58dc
cmpb $0x0, 0x1(%rdx)
je 0x58f0
leaq 0x1(%rdx), %rsi
movq %rsi, (%rax)
movb (%rdx), %dl
movq (%r14), %rsi
incl %ebp
movb %dl, 0xc(%rsi,%rcx)
jmp 0x58cc
movq (%r14), %rax
leal 0x1(%rbp), %edx
xorl %esi, %esi
movb %sil, 0xc(%rax,%rcx)
movq (%r14), %rax
addl $0x2, %ebp
movb %sil, 0xc(%rax,%rdx)
movl $0x1, %ebx
movl $0x200, %r13d           # imm = 0x200
leaq 0x50bc(%rip), %r14      # 0xa9d6
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x5663
leaq 0x84b7(%rip), %rax      # 0xddec
movl $0x1, (%rax)
leaq 0x8756(%rip), %rax      # 0xe098
movq (%rax), %r15
movq %r15, %rdi
leaq 0x4999(%rip), %rsi      # 0xa2e8
callq 0x1130
testl %eax, %eax
je 0x5991
movq %r15, %rdi
leaq 0x4981(%rip), %rsi      # 0xa2e3
callq 0x1130
testl %eax, %eax
je 0x5991
movq %r15, %rdi
leaq 0x4968(%rip), %rsi      # 0xa2dd
callq 0x1130
testl %eax, %eax
je 0x5991
movq %r15, %rdi
leaq 0x493b(%rip), %rsi      # 0xa2c3
callq 0x1130
testl %eax, %eax
jne 0x5993
incl %ebx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1130
testl %eax, %eax
je 0x59b5
movq %r15, %rdi
leaq 0x5030(%rip), %rsi      # 0xa9dc
callq 0x1130
testl %eax, %eax
jne 0x59b7
decl %ebx
movq %r15, %rdi
leaq 0x4978(%rip), %rsi      # 0xa339
callq 0x1130
testl %eax, %eax
jne 0x59f3
leaq 0x82af(%rip), %rax      # 0xdc80
movq (%rax), %rsi
addq $0xc, %rsi
leaq 0x5003(%rip), %rdi      # 0xa9e2
movl $0x4, %edx
callq 0x1e8b
leaq 0x86a8(%rip), %rax      # 0xe098
movq (%rax), %r15
leaq 0x821c(%rip), %rax      # 0xdc16
movb $0x44, (%rax)
movq %r15, %rdi
movq %r14, %r12
movq %r14, %rsi
callq 0x1130
movl %ebp, %r14d
cmpl $0x1, 0xc(%rsp)
jne 0x5a19
testl %eax, %eax
je 0x5a33
movq %r15, %rdi
leaq 0x4fb9(%rip), %rsi      # 0xa9dc
callq 0x1130
cmpl $0x2, 0xc(%rsp)
jl 0x5a3b
testl %eax, %eax
jne 0x5a3b
testl %ebx, %ebx
je 0x5af6
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x7be8
leal 0x8e(%rbp), %eax
cmpl %r13d, %eax
leaq 0x8222(%rip), %r15      # 0xdc80
jbe 0x5aa6
movq (%r15), %rdi
addl $0x200, %r13d           # imm = 0x200
movq %r13, %rsi
callq 0x11b0
leaq 0x81ff(%rip), %rcx      # 0xdc78
movq %rax, (%rcx)
testq %rax, %rax
jne 0x5aa3
movq (%r15), %rsi
addq $0xc, %rsi
leaq 0x4f81(%rip), %rdi      # 0xaa10
movl $0x4, %edx
callq 0x1e8b
leaq 0x81d8(%rip), %rax      # 0xdc78
movq (%rax), %rax
movq %rax, (%r15)
movq (%r15), %rdi
addq $0xc, %rdi
leaq 0x85fc(%rip), %rax      # 0xe0b0
cmpb $0x0, (%rax)
je 0x5adb
addq %r14, %rdi
movq %rax, %r14
movq %rax, %rsi
callq 0x1080
movq %r14, %rdi
callq 0x10b0
addl %eax, %ebp
incl %ebp
movq %r12, %r14
jmp 0x591a
leal 0x1(%rbp), %eax
movb $0x20, (%rdi,%r14)
movq (%r15), %rcx
addl $0x2, %ebp
movb $0x0, 0xc(%rcx,%rax)
movq %r12, %r14
jmp 0x591a
leaq 0x8183(%rip), %r15      # 0xdc80
movq (%r15), %rax
movb $0x0, 0xc(%rax,%r14)
addl $0x9, %ebp
movq (%r15), %rdi
movl %ebp, 0x8(%rdi)
leaq 0x82da(%rip), %rax      # 0xddf0
cmpb $0x0, (%rax)
je 0x5b2e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1040
leaq 0x7f73(%rip), %rbx      # 0xdaa8
movq (%rbx), %rax
movq %rax, (%rdi)
movq (%r15), %rdi
movl %ebp, %esi
callq 0x11b0
leaq 0x812c(%rip), %rcx      # 0xdc78
movq %rax, (%rcx)
movq %rax, (%rbx)
testq %rax, %rax
je 0x5b66
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rsi
addq $0xc, %rsi
leaq 0x4e9c(%rip), %rdi      # 0xaa10
movl $0x4, %edx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1e8b