// File: /workspace/github_c_makefile_star2_O0/FH0[P]tun_direct/lwip-2.1.2/src/core/udp.c
// Function: udp_sendto_if_src_chksum
err_t
udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
                         u16_t dst_port, struct netif *netif, u8_t have_chksum,
                         u16_t chksum, const ip_addr_t *src_ip)
{
#endif /* TUN2SOCKS */
#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  struct udp_hdr *udphdr;
  err_t err;
  struct pbuf *q; /* q will be sent down the stack */
  u8_t ip_proto;
  u8_t ttl;

  LWIP_ASSERT_CORE_LOCKED();

  LWIP_ERROR("udp_sendto_if_src: invalid pcb", pcb != NULL, return ERR_ARG);
  LWIP_ERROR("udp_sendto_if_src: invalid pbuf", p != NULL, return ERR_ARG);
  LWIP_ERROR("udp_sendto_if_src: invalid dst_ip", dst_ip != NULL, return ERR_ARG);
  LWIP_ERROR("udp_sendto_if_src: invalid src_ip", src_ip != NULL, return ERR_ARG);
  LWIP_ERROR("udp_sendto_if_src: invalid netif", netif != NULL, return ERR_ARG);

  if (!IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
      !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
    return ERR_VAL;
  }

#if LWIP_IPV4 && IP_SOF_BROADCAST
  /* broadcast filter? */
  if (!ip_get_option(pcb, SOF_BROADCAST) &&
#if LWIP_IPV6
      IP_IS_V4(dst_ip) &&
#endif /* LWIP_IPV6 */
      ip_addr_isbroadcast(dst_ip, netif)) {
    LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
                ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
    return ERR_VAL;
  }
#endif /* LWIP_IPV4 && IP_SOF_BROADCAST */

  /* if the PCB is not yet bound to a port, bind it here */
  if (pcb->local_port == 0) {
    LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
    err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
    if (err != ERR_OK) {
      LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
      return err;
    }
  }

  /* packet too large to add a UDP header without causing an overflow? */
  if ((u16_t)(p->tot_len + UDP_HLEN) < p->tot_len) {
    return ERR_MEM;
  }
  /* not enough space to add an UDP header to first pbuf in given p chain? */
  if (pbuf_add_header(p, UDP_HLEN)) {
    /* allocate header in a separate new pbuf */
    q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
    /* new header pbuf could not be allocated? */
    if (q == NULL) {
      LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
      return ERR_MEM;
    }
    if (p->tot_len != 0) {
      /* chain header q in front of given pbuf p (only if p contains data) */
      pbuf_chain(q, p);
    }
    /* first pbuf q points to header pbuf */
    LWIP_DEBUGF(UDP_DEBUG,
                ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  } else {
    /* adding space for header within p succeeded */
    /* first pbuf q equals given pbuf */
    q = p;
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
  }
  LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
              (q->len >= sizeof(struct udp_hdr)));
  /* q now represents the packet to be sent */
  udphdr = (struct udp_hdr *)q->payload;
#if TUN2SOCKS
  udphdr->src = lwip_htons(src_port);
#else
  udphdr->src = lwip_htons(pcb->local_port);
#endif /* TUN2SOCKS */
  udphdr->dest = lwip_htons(dst_port);
  /* in UDP, 0 checksum means 'no checksum' */
  udphdr->chksum = 0x0000;

  /* Multicast Loop? */
#if LWIP_MULTICAST_TX_OPTIONS
  if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
    q->flags |= PBUF_FLAG_MCASTLOOP;
  }
#endif /* LWIP_MULTICAST_TX_OPTIONS */

  LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));

#if LWIP_UDPLITE
  /* UDP Lite protocol? */
  if (pcb->flags & UDP_FLAGS_UDPLITE) {
    u16_t chklen, chklen_hdr;
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
    /* set UDP message length in UDP header */
    chklen_hdr = chklen = pcb->chksum_len_tx;
    if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
      if (chklen != 0) {
        LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
      }
      /* For UDP-Lite, checksum length of 0 means checksum
         over the complete packet. (See RFC 3828 chap. 3.1)
         At least the UDP-Lite header must be covered by the
         checksum, therefore, if chksum_len has an illegal
         value, we generate the checksum over the complete
         packet to be safe. */
      chklen_hdr = 0;
      chklen = q->tot_len;
    }
    udphdr->len = lwip_htons(chklen_hdr);
    /* calculate checksum */
#if CHECKSUM_GEN_UDP
    IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
#if LWIP_CHECKSUM_ON_COPY
      if (have_chksum) {
        chklen = UDP_HLEN;
      }
#endif /* LWIP_CHECKSUM_ON_COPY */
      udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
                       q->tot_len, chklen, src_ip, dst_ip);
#if LWIP_CHECKSUM_ON_COPY
      if (have_chksum) {
        u32_t acc;
        acc = udphdr->chksum + (u16_t)~(chksum);
        udphdr->chksum = FOLD_U32T(acc);
      }
#endif /* LWIP_CHECKSUM_ON_COPY */

      /* chksum zero must become 0xffff, as zero means 'no checksum' */
      if (udphdr->chksum == 0x0000) {
        udphdr->chksum = 0xffff;
      }
    }
#endif /* CHECKSUM_GEN_UDP */

    ip_proto = IP_PROTO_UDPLITE;
  } else
#endif /* LWIP_UDPLITE */
  {      /* UDP */
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
    udphdr->len = lwip_htons(q->tot_len);
    /* calculate checksum */
#if CHECKSUM_GEN_UDP
    IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
      /* Checksum is mandatory over IPv6. */
      if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
        u16_t udpchksum;
#if LWIP_CHECKSUM_ON_COPY
        if (have_chksum) {
          u32_t acc;
          udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
                                               q->tot_len, UDP_HLEN, src_ip, dst_ip);
          acc = udpchksum + (u16_t)~(chksum);
          udpchksum = FOLD_U32T(acc);
        } else
#endif /* LWIP_CHECKSUM_ON_COPY */
        {
          udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
                                       src_ip, dst_ip);
        }

        /* chksum zero must become 0xffff, as zero means 'no checksum' */
        if (udpchksum == 0x0000) {
          udpchksum = 0xffff;
        }
        udphdr->chksum = udpchksum;
      }
    }
#endif /* CHECKSUM_GEN_UDP */
    ip_proto = IP_PROTO_UDP;
  }

  /* Determine TTL to use */
#if LWIP_MULTICAST_TX_OPTIONS
  ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl);
#else /* LWIP_MULTICAST_TX_OPTIONS */
  ttl = pcb->ttl;
#endif /* LWIP_MULTICAST_TX_OPTIONS */

  LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
  LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
  /* output to IP */
  NETIF_SET_HINTS(netif, &(pcb->netif_hints));
  err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
  NETIF_RESET_HINTS(netif);

  /* @todo: must this be increased even if error occurred? */
  MIB2_STATS_INC(mib2.udpoutdatagrams);

  /* did we chain a separate header pbuf earlier? */
  if (q != p) {
    /* free the header pbuf */
    pbuf_free(q);
    q = NULL;
    /* p is still referenced by the caller, and will live on */
  }

  UDP_STATS_INC(udp.xmit);
  return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movb %r9b, %al
movw 0x20(%rbp), %r9w
movq 0x18(%rbp), %r9
movw 0x10(%rbp), %r9w
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movw %cx, -0x22(%rbp)
movq %r8, -0x30(%rbp)
movb %al, -0x31(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x17b18
jmp 0x17ae9
leaq 0xf7dc(%rip), %rdi      # 0x272cc
leaq 0x119cd(%rip), %rsi     # 0x294c4
movl $0x33b, %edx            # imm = 0x33B
leaq 0x1184d(%rip), %rcx     # 0x29350
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x17b1a
jmp 0x17b1c
cmpq $0x0, -0x18(%rbp)
jne 0x17b54
jmp 0x17b25
leaq 0xf7a0(%rip), %rdi      # 0x272cc
leaq 0x119b0(%rip), %rsi     # 0x294e3
movl $0x33c, %edx            # imm = 0x33C
leaq 0x11811(%rip), %rcx     # 0x29350
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x17b56
jmp 0x17b58
cmpq $0x0, -0x20(%rbp)
jne 0x17b90
jmp 0x17b61
leaq 0xf764(%rip), %rdi      # 0x272cc
leaq 0x11994(%rip), %rsi     # 0x29503
movl $0x33d, %edx            # imm = 0x33D
leaq 0x117d5(%rip), %rcx     # 0x29350
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x17b92
jmp 0x17b94
cmpq $0x0, 0x18(%rbp)
jne 0x17bcc
jmp 0x17b9d
leaq 0xf728(%rip), %rdi      # 0x272cc
leaq 0x1197a(%rip), %rsi     # 0x29525
movl $0x33e, %edx            # imm = 0x33E
leaq 0x11799(%rip), %rcx     # 0x29350
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x17bce
jmp 0x17bd0
cmpq $0x0, -0x30(%rbp)
jne 0x17c08
jmp 0x17bd9
leaq 0xf6ec(%rip), %rdi      # 0x272cc
leaq 0x11960(%rip), %rsi     # 0x29547
movl $0x33f, %edx            # imm = 0x33F
leaq 0x1175d(%rip), %rcx     # 0x29350
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x17c0a
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x2e, %eax
je 0x17c2b
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
movq 0x18(%rbp), %rcx
movzbl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jne 0x17c4c
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x2e, %eax
je 0x17c55
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0x14(%rcx), %ecx
cmpl %ecx, %eax
je 0x17c55
movb $-0x6, -0x1(%rbp)
jmp 0x17f4c
movq -0x10(%rbp), %rax
movzwl 0x42(%rax), %eax
cmpl $0x0, %eax
jne 0x17c98
jmp 0x17c64
jmp 0x17c66
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movzwl 0x42(%rax), %edx
callq 0x17fd0
movb %al, -0x41(%rbp)
movsbl -0x41(%rbp), %eax
cmpl $0x0, %eax
je 0x17c96
jmp 0x17c89
jmp 0x17c8b
movb -0x41(%rbp), %al
movb %al, -0x1(%rbp)
jmp 0x17f4c
jmp 0x17c98
movq -0x18(%rbp), %rax
movzwl 0x10(%rax), %eax
addl $0x8, %eax
movzwl %ax, %eax
movq -0x18(%rbp), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jge 0x17cbb
movb $-0x1, -0x1(%rbp)
jmp 0x17f4c
movq -0x18(%rbp), %rdi
movl $0x8, %esi
callq 0x7780
cmpb $0x0, %al
je 0x17d19
movl $0x36, %edi
movl $0x8, %esi
movl $0x280, %edx            # imm = 0x280
callq 0x6e20
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x17cf9
jmp 0x17cee
jmp 0x17cf0
movb $-0x1, -0x1(%rbp)
jmp 0x17f4c
movq -0x18(%rbp), %rax
movzwl 0x10(%rax), %eax
cmpl $0x0, %eax
je 0x17d13
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7e00
jmp 0x17d15
jmp 0x17d17
jmp 0x17d25
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x17d23
jmp 0x17d25
jmp 0x17d27
movq -0x50(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpq $0x8, %rax
jae 0x17d66
jmp 0x17d37
leaq 0xf58e(%rip), %rdi      # 0x272cc
leaq 0x11823(%rip), %rsi     # 0x29568
movl $0x378, %edx            # imm = 0x378
leaq 0x115ff(%rip), %rcx     # 0x29350
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x17d68
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movzwl 0x20(%rbp), %edi
callq 0x251c0
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, (%rax)
movzwl -0x22(%rbp), %edi
callq 0x251c0
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x40(%rbp), %rax
movw $0x0, 0x6(%rax)
jmp 0x17da7
jmp 0x17da9
jmp 0x17dab
movq -0x50(%rbp), %rax
movzwl 0x10(%rax), %edi
callq 0x251c0
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x4(%rax)
cmpq $0x0, -0x20(%rbp)
je 0x17dd7
movq -0x20(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x6, %eax
je 0x17deb
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x17e89
cmpb $0x0, -0x31(%rbp)
je 0x17e49
movq -0x50(%rbp), %rdi
movq -0x50(%rbp), %rax
movw 0x10(%rax), %ax
movq 0x18(%rbp), %r8
movq -0x20(%rbp), %r9
movl $0x11, %esi
movl $0x8, %ecx
movzwl %ax, %edx
callq 0x25e80
movw %ax, -0x54(%rbp)
movzwl -0x54(%rbp), %eax
movzwl 0x10(%rbp), %ecx
xorl $-0x1, %ecx
movzwl %cx, %ecx
addl %ecx, %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
shrl $0x10, %eax
movl %eax, %eax
movl -0x58(%rbp), %ecx
andq $0xffff, %rcx           # imm = 0xFFFF
addq %rcx, %rax
movw %ax, -0x54(%rbp)
jmp 0x17e6e
movq -0x50(%rbp), %rdi
movq -0x50(%rbp), %rax
movw 0x10(%rax), %ax
movq 0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
movl $0x11, %esi
movzwl %ax, %edx
callq 0x25a10
movw %ax, -0x54(%rbp)
movzwl -0x54(%rbp), %eax
cmpl $0x0, %eax
jne 0x17e7d
movw $0xffff, -0x54(%rbp)    # imm = 0xFFFF
movw -0x54(%rbp), %cx
movq -0x40(%rbp), %rax
movw %cx, 0x6(%rax)
movb $0x11, -0x51(%rbp)
movq -0x10(%rbp), %rax
movb 0x33(%rax), %al
movb %al, -0x52(%rbp)
jmp 0x17e99
jmp 0x17e9b
jmp 0x17e9d
cmpq $0x0, -0x20(%rbp)
je 0x17eec
movq -0x20(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x6, %eax
jne 0x17eec
movq -0x50(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movb -0x52(%rbp), %cl
movq -0x10(%rbp), %rax
movb 0x32(%rax), %r8b
movb -0x51(%rbp), %r9b
movq -0x30(%rbp), %rax
movzbl %cl, %ecx
movzbl %r8b, %r8d
movzbl %r9b, %r9d
movq %rax, (%rsp)
callq 0x1d690
movsbl %al, %eax
movl %eax, -0x5c(%rbp)
jmp 0x17f25
movq -0x50(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movb -0x52(%rbp), %cl
movq -0x10(%rbp), %rax
movb 0x32(%rax), %r8b
movb -0x51(%rbp), %r9b
movq -0x30(%rbp), %rax
movzbl %cl, %ecx
movzbl %r8b, %r8d
movzbl %r9b, %r9d
movq %rax, (%rsp)
callq 0x19cc0
movsbl %al, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movb %al, -0x41(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
je 0x17f46
movq -0x50(%rbp), %rdi
callq 0x7260
movq $0x0, -0x50(%rbp)
movb -0x41(%rbp), %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)