// File: /workspace/github_c_makefile_star2_O2/kobolt[P]curses-collection/difftree/tree.c
// Function: diff_tree_compare_dir
void diff_tree_compare_dir(char *path1, char *path2, diff_node_t *current)
{
  DIR *dh;
  struct dirent *entry;
  struct stat st1, st2;
  char fullpath1[PATH_MAX], fullpath2[PATH_MAX];
  diff_node_t *subnode;

  dh = opendir(path1);
  if (dh == NULL) {
    fprintf(stderr, "Warning: Unable to open directory: %s\n", path1);
    return;
  }

  while ((entry = readdir(dh))) {
    if (entry->d_name[0] == '.')
      continue; /* Ignore files with leading dot. */

    snprintf(fullpath1, PATH_MAX, "%s/%s", path1, entry->d_name);
    snprintf(fullpath2, PATH_MAX, "%s/%s", path2, entry->d_name);

    if (stat(fullpath1, &st1) == -1) {
      fprintf(stderr, "Warning: Unable to stat() path: %s\n", fullpath1);
      continue;
    }

    if (S_ISDIR(st1.st_mode)) {
      if (stat(fullpath2, &st2) == -1) {
        subnode = diff_node_add(current, entry->d_name, DIFF_TYPE_DIR_ADDED);
        diff_node_parents_differ(current);
        diff_tree_added_dir(fullpath1, subnode);
      } else {
        if (S_ISDIR(st2.st_mode)) {
          subnode = diff_node_add(current, entry->d_name, DIFF_TYPE_DIR_EQUAL);
          diff_tree_compare_dir(fullpath1, fullpath2, subnode);
        } else {
          subnode = diff_node_add(current, entry->d_name, DIFF_TYPE_DIR_ADDED);
          diff_node_parents_differ(current);
          diff_tree_added_dir(fullpath1, subnode);
        }
      }
      
    } else if (S_ISREG(st1.st_mode)) {
      if (stat(fullpath2, &st2) == -1) {
        diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_ADDED);
        diff_node_parents_differ(current);
      } else {
        if (S_ISREG(st2.st_mode)) {
          if (st1.st_size == st2.st_size) {
            if (diff_tree_compare_file(fullpath1, fullpath2)) {
              diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_DIFFERS);
              diff_node_parents_differ(current);
            } else {
              diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_EQUAL);
            }
          } else {
            diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_DIFFERS);
            diff_node_parents_differ(current);
          }
        } else {
          diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_ADDED);
          diff_node_parents_differ(current);
        }
      }
    }
  }
  closedir(dh);

  dh = opendir(path2);
  if (dh == NULL) {
    fprintf(stderr, "Warning: Unable to open directory: %s\n", path2);
    return;
  }

  while ((entry = readdir(dh))) {
    if (entry->d_name[0] == '.')
      continue; /* Ignore files with leading dot. */

    snprintf(fullpath1, PATH_MAX, "%s/%s", path1, entry->d_name);
    snprintf(fullpath2, PATH_MAX, "%s/%s", path2, entry->d_name);

    if (stat(fullpath2, &st2) == -1) {
      fprintf(stderr, "Warning: Unable to stat() path: %s\n", fullpath2);
      continue;
    }

    if (S_ISDIR(st2.st_mode)) {
      if (stat(fullpath1, &st1) == -1) {
        subnode = diff_node_add(current, entry->d_name, DIFF_TYPE_DIR_MISSING);
        diff_node_parents_differ(current);
        diff_tree_missing_dir(fullpath2, subnode);
      } else {
        if (! S_ISDIR(st1.st_mode)) {
          subnode = diff_node_add(current, entry->d_name, DIFF_TYPE_DIR_MISSING);
          diff_node_parents_differ(current);
          diff_tree_missing_dir(fullpath2, subnode);
        }
      }
      
    } else if (S_ISREG(st2.st_mode)) {
      if (stat(fullpath1, &st1) == -1) {
        diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_MISSING);
        diff_node_parents_differ(current);
      } else {
        if (! S_ISREG(st1.st_mode)) {
          diff_node_add(current, entry->d_name, DIFF_TYPE_FILE_MISSING);
          diff_node_parents_differ(current);
        }
      }
    }
  }
  closedir(dh);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2138, %rsp           # imm = 0x2138
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, 0x8(%rsp)
callq 0x20f0
testq %rax, %rax
je 0x2b76
movq %rax, %r12
leaq 0x1904(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
movq %r15, (%rsp)
movq %r12, %rdi
callq 0x2200
testq %rax, %rax
je 0x29d2
movq %rax, %r13
cmpb $0x2e, 0x13(%rax)
je 0x270c
addq $0x13, %r13
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2140
movl $0x1000, %esi           # imm = 0x1000
leaq 0x1130(%rsp), %rdi
movq %r14, %rdx
movq %rbp, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2140
movq %rbx, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2924
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
movl %eax, %ecx
negw %cx
jo 0x27f1
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x270c
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x28d3
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x28d3
movq %r15, %rdi
movq %r13, %rsi
pushq $0x5
popq %rdx
callq 0x2466
movq %rbx, %rdi
leaq 0x1130(%rsp), %rsi
movq %rax, %rdx
callq 0x26cc
jmp 0x270c
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2909
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x2909
movq 0xd0(%rsp), %rax
cmpq 0x40(%rsp), %rax
jne 0x2944
movq %rbx, %rdi
leaq 0x183c(%rip), %rsi      # 0x407b
callq 0x2250
testq %rax, %rax
je 0x2987
movq %rax, %rbx
leaq 0x1130(%rsp), %rdi
leaq 0x181c(%rip), %rsi      # 0x407b
callq 0x2250
testq %rax, %rax
je 0x29ac
movq %rax, %r15
movq %rbp, %r14
movq %rbx, %rdi
callq 0x2170
cmpl $-0x1, %eax
je 0x294e
movl %eax, %ebp
movq %r15, %rdi
callq 0x2170
cmpl %eax, %ebp
je 0x2873
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x20e0
movq (%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
pushq $0x2
popq %rdx
callq 0x2466
movq %r15, %rdi
callq 0x2618
movq %r14, %rbp
leaq 0x173e(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
jmp 0x270c
movq %r15, %rdi
movq %r13, %rsi
pushq $0x7
popq %rdx
callq 0x2466
movq %rax, %rbx
movq %r15, %rdi
callq 0x2618
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
leaq 0x130(%rsp), %rbx
callq 0x2bc1
jmp 0x270c
movq %r15, %rdi
movq %r13, %rsi
pushq $0x3
popq %rdx
callq 0x2466
movq %r15, %rdi
callq 0x2618
jmp 0x270c
movq 0x36cd(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1722(%rip), %rsi      # 0x4057
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x270c
movq %r15, %rdi
movq %r13, %rsi
pushq $0x2
jmp 0x2911
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x20e0
movq %r14, %rbp
movq (%rsp), %r15
leaq 0x1698(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
movq %r15, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x2466
jmp 0x270c
movq 0x366a(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x16e6(%rip), %rsi      # 0x407e
leaq 0x130(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x2974
movq 0x3645(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x16c1(%rip), %rsi      # 0x407e
leaq 0x1130(%rsp), %rdx
xorl %eax, %eax
callq 0x21e0
movq (%rsp), %r15
jmp 0x296c
movq %r12, %rdi
callq 0x21a0
movq %rbp, %rdi
callq 0x20f0
testq %rax, %rax
je 0x2ba4
movq %rax, %r12
leaq 0x160f(%rip), %r13      # 0x4004
leaq 0x130(%rsp), %r14
leaq 0x1130(%rsp), %r15
movq %r12, %rdi
callq 0x2200
testq %rax, %rax
je 0x2b5c
movq %rax, %rbx
cmpb $0x2e, 0x13(%rax)
je 0x2a05
addq $0x13, %rbx
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2140
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2140
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2b3c
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
movl %eax, %ecx
negw %cx
jo 0x2ae7
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2a05
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2ab2
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
je 0x2a05
movq (%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
pushq $0x8
popq %rdx
callq 0x2466
movq %rax, %rbx
movq %r14, %rdi
leaq 0x130(%rsp), %r14
callq 0x2618
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2ceb
jmp 0x2a05
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2b15
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x2a05
movq (%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
pushq $0x4
popq %rdx
callq 0x2466
movq %r14, %rdi
leaq 0x130(%rsp), %r14
callq 0x2618
jmp 0x2a05
movq 0x34b5(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x150a(%rip), %rsi      # 0x4057
movq %r15, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x2a05
movq %r12, %rdi
callq 0x21a0
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x347b(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x14a9(%rip), %rsi      # 0x4030
movq 0x8(%rsp), %rdx
xorl %eax, %eax
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21e0
movq 0x344d(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x147b(%rip), %rsi      # 0x4030
movq %rbp, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x2b64