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

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

  // load the data

  size_t have = 0, alloc = 1024*4; // One page
  while (true) {
    payload.resize(alloc);
    std::cin.read(payload.data()+have, payload.size() - have);

    alloc *=2;
    have += std::cin.gcount();

    if (std::cin.eof()) { // success
      payload.resize(have);
      break;

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

  // return the data
  int r = setxattr(file, attr, payload.data(), payload.size(), 0);
  if (r == -1)
    fatal_errno(file, "setxattr");

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x244d
movq 0x3bca(%rip), %rdi      # 0x5ff0
leaq 0x1bd7(%rip), %rsi      # 0x4004
callq 0x2150
movq %rax, %rdi
movq 0x3b84(%rip), %rsi      # 0x5fc0
callq 0x2180
movl $0x1, -0x4(%rbp)
jmp 0x2778
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 0x2790
movq $0x0, -0x40(%rbp)
movq $0x1000, -0x48(%rbp)    # imm = 0x1000
jmp 0x2480
movq -0x48(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x27b0
jmp 0x248f
leaq -0x38(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x2840
movq -0xc0(%rbp), %rdi
movq -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
callq 0x2860
movq -0xb8(%rbp), %rsi
movq %rax, %rdx
movq -0x40(%rbp), %rax
subq %rax, %rdx
movq 0x3b07(%rip), %rdi      # 0x5fd8
callq 0x21f0
jmp 0x24d8
movq -0x48(%rbp), %rax
addq %rax, %rax
movq %rax, -0x48(%rbp)
movq 0x3aee(%rip), %rdi      # 0x5fd8
callq 0x22b0
movq %rax, -0xc8(%rbp)
jmp 0x24f8
movq -0xc8(%rbp), %rcx
movq -0x40(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq 0x3ac7(%rip), %rdi      # 0x5fd8
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2290
movb %al, -0xc9(%rbp)
jmp 0x2528
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x2534
jmp 0x2559
movq -0x40(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x27b0
jmp 0x2543
jmp 0x25e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x276d
cmpq $0x6400000, -0x48(%rbp) # imm = 0x6400000
jbe 0x25dd
movq 0x3a86(%rip), %rdi      # 0x5ff0
leaq 0x1ab9(%rip), %rsi      # 0x402a
callq 0x2150
movq %rax, -0xd8(%rbp)
jmp 0x257f
movq -0xd8(%rbp), %rdi
movl $0x6400000, %esi        # imm = 0x6400000
callq 0x2050
movq %rax, -0xe0(%rbp)
jmp 0x2599
movq -0xe0(%rbp), %rdi
leaq 0x1aa1(%rip), %rsi      # 0x4048
callq 0x2150
movq %rax, -0xe8(%rbp)
jmp 0x25b5
movq -0xe8(%rbp), %rdi
leaq 0x1a8e(%rip), %rsi      # 0x4051
callq 0x2150
jmp 0x25ca
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x2762
jmp 0x25df
jmp 0x247e
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2840
movq %rax, -0xf0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2860
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x2200
movl %eax, -0x5c(%rbp)
cmpl $-0x1, -0x5c(%rbp)
jne 0x2754
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x110(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x2270
movq -0x110(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0x80(%rbp), %rdi
callq 0x2910
jmp 0x2680
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x2270
movq -0x118(%rbp), %rdx
leaq 0x19cb(%rip), %rsi      # 0x406c
leaq -0xa8(%rbp), %rdi
callq 0x2910
jmp 0x26af
callq 0x2030
movslq (%rax), %rdx
leaq -0x80(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movl $0x1, %ecx
callq 0x2880
jmp 0x26ce
leaq -0xa8(%rbp), %rdi
callq 0x20d0
leaq -0xa9(%rbp), %rdi
callq 0x21a0
leaq -0x80(%rbp), %rdi
callq 0x20d0
leaq -0x81(%rbp), %rdi
callq 0x21a0
jmp 0x2754
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x2746
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x2731
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x20d0
leaq -0xa9(%rbp), %rdi
callq 0x21a0
leaq -0x80(%rbp), %rdi
callq 0x20d0
leaq -0x81(%rbp), %rdi
callq 0x21a0
jmp 0x276d
movl $0x0, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x29c0
jmp 0x2778
leaq -0x38(%rbp), %rdi
callq 0x29c0
jmp 0x2784
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2260
nopl (%rax)