// File: /workspace/github_c_makefile_star2_O0/agl[P]obstcp/obstcp-cli.c
// Function: client
static int
client(uint32_t destip, const char *advert) {
  const int urfd = open("/dev/urandom", O_RDONLY);
  if (urfd < 0) {
    perror("opening urandom");
    return 1;
  }

  struct obstcp_keys keys;
  obstcp_keys_init(&keys);
  uint8_t private_key[32];
  if (read(urfd, private_key, sizeof(private_key)) != sizeof(private_key)) {
    perror("reading from /dev/urandom");
    return 1;
  }

  if (!obstcp_keys_key_add(&keys, private_key)) {
    perror("obstcp_keys_key_add");
    return 1;
  }

  uint8_t randbytes[16];
  if (read(urfd, randbytes, sizeof(randbytes)) != sizeof(randbytes)) {
    perror("reading from /dev/urandom");
    return 1;
  }

  struct obstcp_client_ctx ctx;
  if (!obstcp_client_ctx_init(&ctx, &keys, advert, strlen(advert), randbytes)) {
    perror("obstcp_client_ctx_init");
    return 1;
  }

  int port;
  if (!obstcp_advert_base32_parse(advert, strlen(advert),
                                  OBSTCP_ADVERT_OBSPORT, &port,
                                  OBSTCP_ADVERT_END)) {
    perror("obstcp_advert_parse");
    return 1;
  }

  const int fd = socket(PF_INET, SOCK_STREAM, 0);
  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  sin.sin_addr.s_addr = destip;

  if (connect(fd, (struct sockaddr *) &sin, sizeof(sin))) {
    perror("connect");
    return 1;
  }

  struct iovec iov[3];
  obstcp_client_banner(&ctx, &iov[0]);
  write(fd, iov[0].iov_base, iov[0].iov_len);

  const int efd = epoll_create(2);
  if (efd < 0) {
    perror("epoll_create");
    return 1;
  }

  struct epoll_event eev;
  memset(&eev, 0, sizeof(eev));
  eev.events = EPOLLIN;

  eev.data.fd = 0;
  if (epoll_ctl(efd, EPOLL_CTL_ADD, 0, &eev)) {
    perror("epoll_ctl");
    return 1;
  }

  eev.data.fd = fd;
  if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &eev)) {
    perror("epoll_ctl");
    return 1;
  }

  struct obstcp_rbuf rbuf;
  obstcp_rbuf_client_init(&rbuf, &ctx);

  for (;;) {
    if (epoll_wait(efd, &eev, 1, -1) == -1) {
      perror("epoll_wait");
      return 1;
    }

    if (eev.data.fd == 0) {
      uint8_t buffer[8192];
      ssize_t n;

      do {
        n = read(0, buffer, sizeof(buffer));
      } while (n == -1 && errno == EINTR);

      if (n == 0) {
        fprintf(stderr, " ** Stdin closed\n");
        if (quit_on_stdin_eof)
          return 0;
        epoll_ctl(efd, EPOLL_CTL_DEL, 0, NULL);
      } else if (n < 0) {
        perror("reading from stdin");
        return 1;
      } else {
        struct iovec iov[2];

        obstcp_client_encrypt(&ctx, buffer, buffer, n);
        const int a = obstcp_client_prefix(&ctx, &iov[0]);
        if (a == -1) {
          perror("obstcp_server_ends");
          return 1;
        } else if (a == 0) {
          write(fd, buffer, n);
        } else {
          iov[1].iov_base = buffer;
          iov[1].iov_len = n;
          writev(fd, iov, 2);
        }
      }
    } else {
      uint8_t buffer[8192];

      const ssize_t n =
        obstcp_rbuf_read_fd(&rbuf, fd, buffer, sizeof(buffer));
      if (n == 0) {
        fprintf(stderr, "  ** Remote closed\n");
        return 0;
      } else if (n < 0) {
        perror("obstcp_rbuf_read_fd");
        return 1;
      } else {
        write(1, buffer, n);
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x43c0, %rsp           # imm = 0x43C0
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xacc4(%rip), %rdi      # 0xc04d
xorl %esi, %esi
movb $0x0, %al
callq 0x1160
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x13b3
leaq 0xac8e(%rip), %rdi      # 0xc030
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x20(%rbp), %rdi
callq 0x26a0
movl -0x14(%rbp), %edi
leaq -0x40(%rbp), %rsi
movl $0x20, %edx
callq 0x10f0
cmpq $0x20, %rax
je 0x13eb
leaq 0xac66(%rip), %rdi      # 0xc040
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x40(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x26d0
cmpl $0x0, %eax
jne 0x1415
leaq 0xac56(%rip), %rdi      # 0xc05a
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
movl -0x14(%rbp), %edi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0x10f0
cmpq $0x10, %rax
je 0x1444
leaq 0xac0d(%rip), %rdi      # 0xc040
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
movq -0x10(%rbp), %rax
movq %rax, -0x43b0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1090
movq -0x43b0(%rbp), %rdx
movl %eax, %ecx
leaq -0x50(%rbp), %r8
leaq -0x2d8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x34a0
cmpl $0x0, %eax
jne 0x1492
leaq 0xabed(%rip), %rdi      # 0xc06e
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
movq -0x10(%rbp), %rax
movq %rax, -0x43b8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1090
movq -0x43b8(%rbp), %rdi
movl %eax, %esi
movl $0x1, %edx
leaq -0x2dc(%rbp), %rcx
xorl %r8d, %r8d
movb $0x0, %al
callq 0x2140
cmpl $0x0, %eax
jne 0x14e2
leaq 0xabb4(%rip), %rdi      # 0xc085
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11b0
movl %eax, -0x2e0(%rbp)
leaq -0x2f0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10d0
movw $0x2, -0x2f0(%rbp)
movl -0x2dc(%rbp), %eax
movzwl %ax, %edi
callq 0x10a0
movw %ax, -0x2ee(%rbp)
movl -0x8(%rbp), %eax
movl %eax, -0x2ec(%rbp)
movl -0x2e0(%rbp), %edi
leaq -0x2f0(%rbp), %rsi
movl $0x10, %edx
callq 0x1190
cmpl $0x0, %eax
je 0x1567
leaq 0xab43(%rip), %rdi      # 0xc099
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x320(%rbp), %rsi
leaq -0x2d8(%rbp), %rdi
callq 0x3a60
movl -0x2e0(%rbp), %edi
movq -0x320(%rbp), %rsi
movq -0x318(%rbp), %rdx
callq 0x1080
movl $0x2, %edi
callq 0x1180
movl %eax, -0x324(%rbp)
cmpl $0x0, -0x324(%rbp)
jge 0x15c4
leaq 0xaaee(%rip), %rdi      # 0xc0a1
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x330(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x10d0
movl $0x1, -0x330(%rbp)
movl $0x0, -0x32c(%rbp)
movl -0x324(%rbp), %edi
movl $0x1, %esi
xorl %edx, %edx
leaq -0x330(%rbp), %rcx
callq 0x10e0
cmpl $0x0, %eax
je 0x1621
leaq 0xaa9e(%rip), %rdi      # 0xc0ae
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
movl -0x2e0(%rbp), %eax
movl %eax, -0x32c(%rbp)
movl -0x324(%rbp), %edi
movl -0x2e0(%rbp), %edx
movl $0x1, %esi
leaq -0x330(%rbp), %rcx
callq 0x10e0
cmpl $0x0, %eax
je 0x1667
leaq 0xaa58(%rip), %rdi      # 0xc0ae
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x360(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x4820
movl -0x324(%rbp), %edi
leaq -0x330(%rbp), %rsi
movl $0x1, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1150
cmpl $-0x1, %eax
jne 0x16b3
leaq 0xaa16(%rip), %rdi      # 0xc0b8
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
cmpl $0x0, -0x32c(%rbp)
jne 0x1825
jmp 0x16c2
leaq -0x2360(%rbp), %rsi
xorl %edi, %edi
movl $0x2000, %edx           # imm = 0x2000
callq 0x10f0
movq %rax, -0x2368(%rbp)
xorl %eax, %eax
cmpq $-0x1, -0x2368(%rbp)
movb %al, -0x43b9(%rbp)
jne 0x16ff
callq 0x1050
cmpl $0x4, (%rax)
sete %al
movb %al, -0x43b9(%rbp)
movb -0x43b9(%rbp), %al
testb $0x1, %al
jne 0x16c2
cmpq $0x0, -0x2368(%rbp)
jne 0x1746
movq 0xd8de(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xa99f(%rip), %rsi      # 0xc0c3
movb $0x0, %al
callq 0x1100
movl -0x324(%rbp), %edi
movl $0x2, %esi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x10e0
jmp 0x1820
cmpq $0x0, -0x2368(%rbp)
jge 0x1768
leaq 0xa97e(%rip), %rdi      # 0xc0d5
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x2360(%rbp), %rsi
leaq -0x2360(%rbp), %rdx
movq -0x2368(%rbp), %rcx
leaq -0x2d8(%rbp), %rdi
callq 0x3d20
leaq -0x2390(%rbp), %rsi
leaq -0x2d8(%rbp), %rdi
callq 0x3d60
movl %eax, -0x2394(%rbp)
cmpl $-0x1, -0x2394(%rbp)
jne 0x17c3
leaq 0xa936(%rip), %rdi      # 0xc0e8
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
cmpl $0x0, -0x2394(%rbp)
jne 0x17e7
movl -0x2e0(%rbp), %edi
leaq -0x2360(%rbp), %rsi
movq -0x2368(%rbp), %rdx
callq 0x1080
jmp 0x181a
leaq -0x2360(%rbp), %rax
movq %rax, -0x2380(%rbp)
movq -0x2368(%rbp), %rax
movq %rax, -0x2378(%rbp)
movl -0x2e0(%rbp), %edi
leaq -0x2390(%rbp), %rsi
movl $0x2, %edx
callq 0x1070
jmp 0x181c
jmp 0x181e
jmp 0x1820
jmp 0x18b0
movl -0x2e0(%rbp), %esi
leaq -0x43a0(%rbp), %rdx
leaq -0x360(%rbp), %rdi
movl $0x2000, %ecx           # imm = 0x2000
callq 0x50d0
movq %rax, -0x43a8(%rbp)
cmpq $0x0, -0x43a8(%rbp)
jne 0x1875
movq 0xd79d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xa896(%rip), %rsi      # 0xc0fb
movb $0x0, %al
callq 0x1100
movl $0x0, -0x4(%rbp)
jmp 0x18b5
cmpq $0x0, -0x43a8(%rbp)
jge 0x1894
leaq 0xa889(%rip), %rdi      # 0xc10f
callq 0x1170
movl $0x1, -0x4(%rbp)
jmp 0x18b5
leaq -0x43a0(%rbp), %rsi
movq -0x43a8(%rbp), %rdx
movl $0x1, %edi
callq 0x1080
jmp 0x18ae
jmp 0x18b0
jmp 0x167a
movl -0x4(%rbp), %eax
addq $0x43c0, %rsp           # imm = 0x43C0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)