// File: SoftwearDevelopment[P]xattr_syscall_wrapper/listxattr.cc
// Function: main
int main(int argc, char** argv) {
  if (argc != 2 && argc != 3) {
    std::cerr << "USAGE: "
      << std::endl << "  getxattr [FILE] > data"
      << std::endl << "  getxattr -z [FILE] > data"
      << std::endl;
    return 1;
  }

  auto &file = argv[1];
  bool print_zeros = std::string(file) == "-z";
  if (print_zeros) file = argv[2];

  std::vector<char> payload;

  // load the data

  size_t alloc = 1024*4; // One page
  while (true) {
    payload.resize(alloc);
    ssize_t len = listxattr(file, payload.data(), payload.size());
    alloc *=2;

    if (len >= 0) { // success
      payload.resize(len);
      break;

    } else if (errno != ERANGE) { // unknown error
      fatal_errno(file, "listxattr");

    } else if (alloc > max_buf_size) { // to large
      std::cerr << "listxattr error: xattr list larger than "
                << max_buf_size << " bytes. "
                << "Refusing to allocate more.";
      return 1;
    }
  }

  if (print_zeros)
    std::cout.write(payload.data(), payload.size());
  else
    print_with_newline(payload.data(), payload.data() + payload.size(),
                       std::cout);;

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x247f
cmpl $0x3, -0x8(%rbp)
je 0x247f
movq 0x4bd4(%rip), %rdi      # 0x6ff0
leaq 0x1bf1(%rip), %rsi      # 0x4014
callq 0x2170
movq %rax, %rdi
movq 0x4b8e(%rip), %rsi      # 0x6fc0
callq 0x21a0
movq %rax, %rdi
leaq 0x1bdb(%rip), %rsi      # 0x401c
callq 0x2170
movq %rax, %rdi
movq 0x4b70(%rip), %rsi      # 0x6fc0
callq 0x21a0
movq %rax, %rdi
leaq 0x1bd6(%rip), %rsi      # 0x4035
callq 0x2170
movq %rax, %rdi
movq 0x4b52(%rip), %rsi      # 0x6fc0
callq 0x21a0
movl $0x1, -0x4(%rbp)
jmp 0x2858
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x2280
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x28a0
jmp 0x24c2
leaq 0x1b88(%rip), %rsi      # 0x4051
leaq -0x40(%rbp), %rdi
callq 0x2870
movb %al, -0xe1(%rbp)
jmp 0x24da
leaq -0x40(%rbp), %rdi
callq 0x20e0
leaq -0x41(%rbp), %rdi
callq 0x21c0
movb -0xe1(%rbp), %al
andb $0x1, %al
movb %al, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
je 0x253f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x253f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x2531
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x20e0
leaq -0x41(%rbp), %rdi
callq 0x21c0
jmp 0x2864
leaq -0x70(%rbp), %rdi
callq 0x2950
movq $0x1000, -0x78(%rbp)    # imm = 0x1000
jmp 0x2552
movq -0x78(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2970
jmp 0x2561
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2a00
movq %rax, -0xf0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2a20
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq %rax, %rdx
callq 0x2100
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
shlq %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x25d9
movq -0x80(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2970
jmp 0x25c3
jmp 0x2797
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x284d
callq 0x2030
cmpl $0x22, (%rax)
je 0x2707
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x108(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x2280
movq -0x108(%rbp), %rsi
movq -0x100(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0x28a0
jmp 0x2624
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x2280
movq -0x110(%rbp), %rdx
leaq 0x1a0f(%rip), %rsi      # 0x4054
leaq -0xc8(%rbp), %rdi
callq 0x28a0
jmp 0x2653
callq 0x2030
movslq (%rax), %rdx
leaq -0xa0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
movl $0x1, %ecx
callq 0x2a40
jmp 0x2675
leaq -0xc8(%rbp), %rdi
callq 0x20e0
leaq -0xc9(%rbp), %rdi
callq 0x21c0
leaq -0xa0(%rbp), %rdi
callq 0x20e0
leaq -0xa1(%rbp), %rdi
callq 0x21c0
jmp 0x2790
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x26f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x26de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x20e0
leaq -0xc9(%rbp), %rdi
callq 0x21c0
leaq -0xa0(%rbp), %rdi
callq 0x20e0
leaq -0xa1(%rbp), %rdi
callq 0x21c0
jmp 0x284d
cmpq $0x6400000, -0x78(%rbp) # imm = 0x6400000
jbe 0x278e
movq 0x48d8(%rip), %rdi      # 0x6ff0
leaq 0x193f(%rip), %rsi      # 0x405e
callq 0x2170
movq %rax, -0x118(%rbp)
jmp 0x272d
movq -0x118(%rbp), %rdi
movl $0x6400000, %esi        # imm = 0x6400000
callq 0x2050
movq %rax, -0x120(%rbp)
jmp 0x2747
movq -0x120(%rbp), %rdi
leaq 0x1932(%rip), %rsi      # 0x4087
callq 0x2170
movq %rax, -0x128(%rbp)
jmp 0x2763
movq -0x128(%rbp), %rdi
leaq 0x191f(%rip), %rsi      # 0x4090
callq 0x2170
jmp 0x2778
movl $0x1, -0x4(%rbp)
movl $0x1, -0xd0(%rbp)
jmp 0x2842
jmp 0x2790
jmp 0x2792
jmp 0x2550
testb $0x1, -0x19(%rbp)
je 0x27da
leaq -0x70(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x2a00
movq -0x138(%rbp), %rdi
movq %rax, -0x130(%rbp)
callq 0x2a20
movq -0x130(%rbp), %rsi
movq %rax, %rdx
movq 0x47ff(%rip), %rdi      # 0x6fd0
callq 0x2210
jmp 0x27d8
jmp 0x2831
leaq -0x70(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x2a00
movq -0x150(%rbp), %rdi
movq %rax, -0x140(%rbp)
callq 0x2a00
movq -0x150(%rbp), %rdi
movq %rax, -0x148(%rbp)
callq 0x2a20
movq -0x148(%rbp), %rsi
movq -0x140(%rbp), %rdi
addq %rax, %rsi
movq 0x47a8(%rip), %rdx      # 0x6fd0
callq 0x2ad0
jmp 0x282f
jmp 0x2831
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2b90
jmp 0x2858
leaq -0x70(%rbp), %rdi
callq 0x2b90
jmp 0x2864
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2270
nopl (%rax)