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

  auto &file = argv[1], &attr = argv[2];
  std::vector<char> payload;

  // load the data

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

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

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

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

  // return the data

  std::cout.write(payload.data(), payload.size());
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x242d
movq 0x3bea(%rip), %rdi      # 0x5ff0
leaq 0x1bf7(%rip), %rsi      # 0x4004
callq 0x2160
movq %rax, %rdi
movq 0x3ba4(%rip), %rsi      # 0x5fc0
callq 0x2190
movl $0x1, -0x4(%rbp)
jmp 0x26fc
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2720
movq $0x1000, -0x40(%rbp)    # imm = 0x1000
jmp 0x2458
movq -0x40(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2740
jmp 0x2467
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x27d0
movq %rax, -0xb0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x27f0
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq %rax, %rcx
callq 0x2050
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
shlq %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x58(%rbp)
jl 0x24f4
movq -0x58(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2740
jmp 0x24de
jmp 0x269a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26f1
callq 0x2030
cmpl $0x22, (%rax)
je 0x260d
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x2270
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x28a0
jmp 0x2539
leaq -0xa1(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x2270
movq -0xd8(%rbp), %rdx
leaq 0x1ad0(%rip), %rsi      # 0x402a
leaq -0xa0(%rbp), %rdi
callq 0x28a0
jmp 0x2568
callq 0x2030
movslq (%rax), %rdx
leaq -0x78(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x1, %ecx
callq 0x2810
jmp 0x2587
leaq -0xa0(%rbp), %rdi
callq 0x20e0
leaq -0xa1(%rbp), %rdi
callq 0x21b0
leaq -0x78(%rbp), %rdi
callq 0x20e0
leaq -0x79(%rbp), %rdi
callq 0x21b0
jmp 0x2693
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x25ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x25ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x20e0
leaq -0xa1(%rbp), %rdi
callq 0x21b0
leaq -0x78(%rbp), %rdi
callq 0x20e0
leaq -0x79(%rbp), %rdi
callq 0x21b0
jmp 0x26f1
cmpq $0x6400000, -0x40(%rbp) # imm = 0x6400000
jbe 0x2691
movq 0x39d2(%rip), %rdi      # 0x5ff0
leaq 0x1a0e(%rip), %rsi      # 0x4033
callq 0x2160
movq %rax, -0xe0(%rbp)
jmp 0x2633
movq -0xe0(%rbp), %rdi
movl $0x6400000, %esi        # imm = 0x6400000
callq 0x2060
movq %rax, -0xe8(%rbp)
jmp 0x264d
movq -0xe8(%rbp), %rdi
leaq 0x19fb(%rip), %rsi      # 0x4056
callq 0x2160
movq %rax, -0xf0(%rbp)
jmp 0x2669
movq -0xf0(%rbp), %rdi
leaq 0x19e8(%rip), %rsi      # 0x405f
callq 0x2160
jmp 0x267e
movl $0x1, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0x26e6
jmp 0x2693
jmp 0x2695
jmp 0x2456
leaq -0x38(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x27d0
movq -0x100(%rbp), %rdi
movq %rax, -0xf8(%rbp)
callq 0x27f0
movq -0xf8(%rbp), %rsi
movq %rax, %rdx
movq 0x3902(%rip), %rdi      # 0x5fd0
callq 0x2200
jmp 0x26d5
movl $0x0, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2950
jmp 0x26fc
leaq -0x38(%rbp), %rdi
callq 0x2950
jmp 0x2708
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2260
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)