// File: /workspace/github_c_makefile_star2_O1/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 0x2c01
movq %rax, %r12
movq %rax, %rdi
callq 0x2200
testq %rax, %rax
movq %r15, (%rsp)
je 0x2a4c
movq %rax, %r13
leaq 0x188c(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
cmpb $0x2e, 0x13(%r13)
je 0x2986
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 0x299f
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
movl %eax, %ecx
negw %cx
jo 0x2858
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2986
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2939
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2939
movq %r15, %rdi
movq %r13, %rsi
movl $0x5, %edx
callq 0x2487
movq %rbx, %rdi
leaq 0x1130(%rsp), %rsi
movq %rax, %rdx
callq 0x272c
jmp 0x2986
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x296e
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x296e
movq 0xd0(%rsp), %rax
cmpq 0x40(%rsp), %rax
jne 0x29bc
movq %rbx, %rdi
leaq 0x17d5(%rip), %rsi      # 0x407b
callq 0x2250
testq %rax, %rax
je 0x2a01
movq %rax, %rbx
leaq 0x1130(%rsp), %rdi
leaq 0x17b5(%rip), %rsi      # 0x407b
callq 0x2250
testq %rax, %rax
je 0x2a26
movq %rax, %r15
movq %rbp, %r14
movq %rbx, %rdi
callq 0x2170
cmpl $-0x1, %eax
je 0x29c9
movl %eax, %ebp
movq %r15, %rdi
callq 0x2170
cmpl %eax, %ebp
je 0x28da
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x20e0
movq (%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0x2487
movq %r15, %rdi
callq 0x266b
movq %r14, %rbp
leaq 0x16d5(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
jmp 0x2986
movq %r15, %rdi
movq %r13, %rsi
movl $0x7, %edx
callq 0x2487
movq %rax, %rbx
movq %r15, %rdi
callq 0x266b
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
leaq 0x130(%rsp), %rbx
callq 0x2c4c
jmp 0x2986
movq %r15, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq 0x2487
movq %r15, %rdi
callq 0x266b
movq %r12, %rdi
callq 0x2200
movq %rax, %r13
testq %rax, %rax
jne 0x2780
jmp 0x2a4c
movq 0x3652(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x16a7(%rip), %rsi      # 0x4057
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x2986
movq %r15, %rdi
movq %r13, %rsi
movl $0x2, %edx
jmp 0x2979
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x20e0
movq %r14, %rbp
movq (%rsp), %r15
leaq 0x161d(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
movq %r15, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x2487
jmp 0x2986
movq 0x35f0(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x166c(%rip), %rsi      # 0x407e
leaq 0x130(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x29ef
movq 0x35cb(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1647(%rip), %rsi      # 0x407e
leaq 0x1130(%rsp), %rdx
xorl %eax, %eax
callq 0x21e0
movq (%rsp), %r15
jmp 0x29e7
movq %r12, %rdi
callq 0x21a0
movq %rbp, %rdi
callq 0x20f0
testq %rax, %rax
je 0x2c2f
movq %rax, %r12
movq %rax, %rdi
callq 0x2200
testq %rax, %rax
je 0x2be7
movq %rax, %rbx
leaq 0x1581(%rip), %r13      # 0x4004
leaq 0x130(%rsp), %r14
leaq 0x1130(%rsp), %r15
cmpb $0x2e, 0x13(%rbx)
je 0x2bd3
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 0x2bb8
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
movl %eax, %ecx
negw %cx
jo 0x2b68
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2bd3
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2b34
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
je 0x2bd3
movq (%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x2487
movq %rax, %rbx
movq %r14, %rdi
leaq 0x130(%rsp), %r14
callq 0x266b
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2d8a
jmp 0x2bd3
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2b92
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x2bd3
movq (%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x4, %edx
callq 0x2487
movq %r14, %rdi
leaq 0x130(%rsp), %r14
callq 0x266b
jmp 0x2bd3
movq 0x3439(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x148e(%rip), %rsi      # 0x4057
movq %r15, %rdx
xorl %eax, %eax
callq 0x21e0
movq %r12, %rdi
callq 0x2200
movq %rax, %rbx
testq %rax, %rax
jne 0x2a93
movq %r12, %rdi
callq 0x21a0
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x33f0(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x141e(%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 0x33c2(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x13f0(%rip), %rsi      # 0x4030
movq %rbp, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x2bef