// File: /workspace/github_c_makefile_star2_O3/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 0x2c09
movq %rax, %r12
movq %rax, %rdi
callq 0x2200
testq %rax, %rax
movq %r15, (%rsp)
je 0x2a54
movq %rax, %r13
leaq 0x1884(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
cmpb $0x2e, 0x13(%r13)
je 0x298e
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 0x29a7
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
movl %eax, %ecx
negw %cx
jo 0x2860
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x298e
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2941
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2941
movq %r15, %rdi
movq %r13, %rsi
movl $0x5, %edx
callq 0x248e
movq %rbx, %rdi
leaq 0x1130(%rsp), %rsi
movq %rax, %rdx
callq 0x2734
jmp 0x298e
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2976
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x2976
movq 0xd0(%rsp), %rax
cmpq 0x40(%rsp), %rax
jne 0x29c4
movq %rbx, %rdi
leaq 0x17cd(%rip), %rsi      # 0x407b
callq 0x2250
testq %rax, %rax
je 0x2a09
movq %rax, %rbx
leaq 0x1130(%rsp), %rdi
leaq 0x17ad(%rip), %rsi      # 0x407b
callq 0x2250
testq %rax, %rax
je 0x2a2e
movq %rax, %r15
movq %rbp, %r14
movq %rbx, %rdi
callq 0x2170
cmpl $-0x1, %eax
je 0x29d1
movl %eax, %ebp
movq %r15, %rdi
callq 0x2170
cmpl %eax, %ebp
je 0x28e2
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x20e0
movq (%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0x248e
movq %r15, %rdi
callq 0x2672
movq %r14, %rbp
leaq 0x16cd(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
jmp 0x298e
movq %r15, %rdi
movq %r13, %rsi
movl $0x7, %edx
callq 0x248e
movq %rax, %rbx
movq %r15, %rdi
callq 0x2672
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
leaq 0x130(%rsp), %rbx
callq 0x2c54
jmp 0x298e
movq %r15, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq 0x248e
movq %r15, %rdi
callq 0x2672
movq %r12, %rdi
callq 0x2200
movq %rax, %r13
testq %rax, %rax
jne 0x2788
jmp 0x2a54
movq 0x364a(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x169f(%rip), %rsi      # 0x4057
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x298e
movq %r15, %rdi
movq %r13, %rsi
movl $0x2, %edx
jmp 0x2981
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x20e0
movq %r14, %rbp
movq (%rsp), %r15
leaq 0x1615(%rip), %r14      # 0x4004
leaq 0x130(%rsp), %rbx
movq %r15, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x248e
jmp 0x298e
movq 0x35e8(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1664(%rip), %rsi      # 0x407e
leaq 0x130(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x29f7
movq 0x35c3(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x163f(%rip), %rsi      # 0x407e
leaq 0x1130(%rsp), %rdx
xorl %eax, %eax
callq 0x21e0
movq (%rsp), %r15
jmp 0x29ef
movq %r12, %rdi
callq 0x21a0
movq %rbp, %rdi
callq 0x20f0
testq %rax, %rax
je 0x2c37
movq %rax, %r12
movq %rax, %rdi
callq 0x2200
testq %rax, %rax
je 0x2bef
movq %rax, %rbx
leaq 0x1579(%rip), %r13      # 0x4004
leaq 0x130(%rsp), %r14
leaq 0x1130(%rsp), %r15
cmpb $0x2e, 0x13(%rbx)
je 0x2bdb
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 0x2bc0
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
movl %eax, %ecx
negw %cx
jo 0x2b70
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2bdb
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2b3c
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
je 0x2bdb
movq (%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x248e
movq %rax, %rbx
movq %r14, %rdi
leaq 0x130(%rsp), %r14
callq 0x2672
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2d92
jmp 0x2bdb
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
cmpl $-0x1, %eax
je 0x2b9a
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x2bdb
movq (%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x4, %edx
callq 0x248e
movq %r14, %rdi
leaq 0x130(%rsp), %r14
callq 0x2672
jmp 0x2bdb
movq 0x3431(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1486(%rip), %rsi      # 0x4057
movq %r15, %rdx
xorl %eax, %eax
callq 0x21e0
movq %r12, %rdi
callq 0x2200
movq %rax, %rbx
testq %rax, %rax
jne 0x2a9b
movq %r12, %rdi
callq 0x21a0
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x33e8(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1416(%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 0x33ba(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x13e8(%rip), %rsi      # 0x4030
movq %rbp, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x2bf7