#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  unsigned char *msg_control;
  int msg_controllen;
  scalar_t__ msg_flags;
  struct iovec *msg_iov;
  struct sockaddr_in *msg_name;
};
struct iovec {
  unsigned char *iov_base;
  int iov_len;
};
struct in_pktinfo {
  int dummy;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ ctrl;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_PKTINFO;
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int ntohl(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, ...);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
void forward_response(int sock_fd, unsigned char *buf, int len,
                      struct sockaddr_in *dest, struct in_pktinfo *src) {
  struct iovec iovec[1];
  iovec[0].iov_base = buf;
  iovec[0].iov_len = len;
  unsigned char ctrl[256];
  memset(ctrl, 0, sizeof(ctrl));
  struct msghdr msg;
  msg.msg_name = dest;
  msg.msg_namelen = sizeof(*dest);
  msg.msg_iov = iovec;
  msg.msg_iovlen = 1;
  msg.msg_control = ctrl;
  msg.msg_controllen = sizeof(ctrl);
  msg.msg_flags = 0;
  if (src) {
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = IPPROTO_IP;
    cmsg->cmsg_type = IP_PKTINFO;
    cmsg->cmsg_len = CMSG_LEN(sizeof(*src));
    *(struct in_pktinfo *)CMSG_DATA(cmsg) = *src;
    msg.msg_controllen = CMSG_SPACE(sizeof(*src));
  } else {
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
  }
  int nbytes = sendmsg(sock_fd, &msg, 0);
  if (nbytes < 0) {
    perror("Send error");
  } else if (nbytes < len) {
    printf("Only %d bytes forwarded to %08x\n", nbytes,
           ntohl(dest->sin_addr.s_addr));
  } else {
    printf("Forwarded %d bytes to %08x port %u\n", nbytes,
           ntohl(dest->sin_addr.s_addr), ntohs(dest->sin_port));
  }
}


x86 assembly (clang, -O0):
<forward_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x148(%rbp)
movl   $0x8,-0x170(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x150(%rbp)
movl   $0x1,-0x16c(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x168(%rbp)
movl   $0x100,-0x160(%rbp)
movq   $0x0,-0x158(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12ad <forward_response+0x10d>
lea    -0x170(%rbp),%rdi
call   10b0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x178(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%ecx
mov    -0x178(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2d7d(%rip),%rax
mov    (%rax),%ecx
mov    -0x178(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x178(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x178(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    $0x4,%edi
call   1080 <CMSG_SPACE@plt>
mov    %eax,-0x160(%rbp)
jmp    12c2 <forward_response+0x122>
movq   $0x0,-0x168(%rbp)
movl   $0x0,-0x160(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x170(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x17c(%rbp)
cmpl   $0x0,-0x17c(%rbp)
jge    12f7 <forward_response+0x157>
lea    0xd13(%rip),%rdi
call   10a0 <perror@plt>
jmp    1383 <forward_response+0x1e3>
mov    -0x17c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1339 <forward_response+0x199>
mov    -0x17c(%rbp),%eax
mov    %eax,-0x180(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   10c0 <ntohl@plt>
mov    -0x180(%rbp),%esi
mov    %eax,%edx
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    137e <forward_response+0x1de>
mov    -0x17c(%rbp),%eax
mov    %eax,-0x188(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   10c0 <ntohl@plt>
mov    %eax,-0x184(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1090 <ntohs@plt>
mov    -0x188(%rbp),%esi
mov    -0x184(%rbp),%edx
mov    %eax,%ecx
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1383 <forward_response+0x1e3>
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forward_response>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
mov    %r8,%r13
mov    %rcx,%r12
mov    %edx,%r15d
mov    %edi,%r14d
mov    %rsi,0x30(%rsp)
mov    %edx,0x38(%rsp)
lea    0x40(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    %r12,0x28(%rsp)
movabs $0x100000008,%rax
mov    %rax,(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    %rbp,0x8(%rsp)
movl   $0x100,0x10(%rsp)
movq   $0x0,0x18(%rsp)
test   %r13,%r13
je     125f <forward_response+0xbf>
mov    %rsp,%rdi
call   10b0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    0x0(%r13),%ecx
mov    %ecx,(%rax)
mov    $0x4,%edi
call   1080 <CMSG_SPACE@plt>
mov    %eax,%ebx
jmp    1268 <forward_response+0xc8>
movq   $0x0,0x8(%rsp)
mov    %ebx,0x10(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %eax,%eax
js     12d3 <forward_response+0x133>
mov    %eax,%ebp
mov    0x4(%r12),%edi
call   10c0 <ntohl@plt>
mov    %eax,%ebx
cmp    %r15d,%ebp
jge    12a4 <forward_response+0x104>
lea    0xd74(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12c1 <forward_response+0x121>
mov    (%r12),%edi
call   1090 <ntohs@plt>
lea    0xd78(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x148,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd26(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c1 <forward_response+0x121>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.in_pktinfo = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i8*, i32, i64, %struct.iovec*, %struct.sockaddr_in* }
%struct.cmsghdr = type { i32, i32, i32 }
@IPPROTO_IP = global i32 0
@IP_PKTINFO = global i32 0
@.str = [11 x i8] c"Send error\00"
@.str.1 = [33 x i8] c"Only %d bytes forwarded to %08x\0A\00"
@.str.2 = [36 x i8] c"Forwarded %d bytes to %08x port %u\0A\00"
define void @forward_response(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, %struct.in_pktinfo* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.in_pktinfo*
%11 = alloca [1 x %struct.iovec]
%12 = alloca [256 x i8]
%13 = alloca %struct.msghdr
%14 = alloca %struct.cmsghdr*
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
store %struct.in_pktinfo* %4, %struct.in_pktinfo** %10
%16 = load i8*, i8** %7
%17 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %11, i64 0, i64 0
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i32 0, i32 0
store i8* %16, i8** %18
%19 = load i32, i32* %8
%20 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %11, i64 0, i64 0
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 1
store i32 %19, i32* %21
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 256)
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 6
store %struct.sockaddr_in* %24, %struct.sockaddr_in** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 0
store i32 8, i32* %26
%27 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %11, i64 0, i64 0
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 5
store %struct.iovec* %27, %struct.iovec** %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 1
store i32 1, i32* %29
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
store i8* %30, i8** %31
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 3
store i32 256, i32* %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 4
store i64 0, i64* %33
%34 = load %struct.in_pktinfo*, %struct.in_pktinfo** %10
%35 = icmp ne %struct.in_pktinfo* %34, null
br i1 %35, label %36, label %55
36: ; preds = %5
%37 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %13)
store %struct.cmsghdr* %37, %struct.cmsghdr** %14
%38 = load i32, i32* @IPPROTO_IP
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i32 0, i32 2
store i32 %38, i32* %40
%41 = load i32, i32* @IP_PKTINFO
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 1
store i32 %41, i32* %43
%44 = call i32 @CMSG_LEN(i32 4)
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 0
store i32 %44, i32* %46
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%48 = call i64 @CMSG_DATA(%struct.cmsghdr* %47)
%49 = inttoptr i64 %48 to %struct.in_pktinfo*
%50 = load %struct.in_pktinfo*, %struct.in_pktinfo** %10
%51 = bitcast %struct.in_pktinfo* %49 to i8*
%52 = bitcast %struct.in_pktinfo* %50 to i8*
%53 = call i32 @CMSG_SPACE(i32 4)
%54 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 3
store i32 %53, i32* %54
br label %58
55: ; preds = %5
%56 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
store i8* null, i8** %56
%57 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 3
store i32 0, i32* %57
br label %58
58: ; preds = %55, %36
%59 = load i32, i32* %6
%60 = call i32 @sendmsg(i32 %59, %struct.msghdr* %13, i32 0)
store i32 %60, i32* %15
%61 = load i32, i32* %15
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %90
65: ; preds = %58
%66 = load i32, i32* %15
%67 = load i32, i32* %8
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %77
69: ; preds = %65
%70 = load i32, i32* %15
%71 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%72 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %71, i32 0, i32 1
%73 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i32 @ntohl(i32 %74)
%76 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %70, i32 %75)
br label %89
77: ; preds = %65
%78 = load i32, i32* %15
%79 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%80 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %79, i32 0, i32 1
%81 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %80, i32 0, i32 0
%82 = load i32, i32* %81
%83 = call i32 @ntohl(i32 %82)
%84 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @ntohs(i32 %86)
%88 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %78, i32 %83, i32 %87)
br label %89
89: ; preds = %77, %69
br label %90
90: ; preds = %89, %63
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.in_pktinfo = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i8*, i32, i64, %struct.iovec*, %struct.sockaddr_in* }
%struct.cmsghdr = type { i32, i32, i32 }
@IPPROTO_IP = global i32 0
@IP_PKTINFO = global i32 0
@.str = [11 x i8] c"Send error\00"
@.str.1 = [33 x i8] c"Only %d bytes forwarded to %08x\0A\00"
@.str.2 = [36 x i8] c"Forwarded %d bytes to %08x port %u\0A\00"
define void @forward_response(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, %struct.in_pktinfo* %4) {
%6 = alloca [1 x %struct.iovec]
%7 = alloca [256 x i8]
%8 = alloca %struct.msghdr
%9 = bitcast [1 x %struct.iovec]* %6 to i8*
%10 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0
%11 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0, i32 0
store i8* %1, i8** %11
%12 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0, i32 1
store i32 %2, i32* %12
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 256)
%15 = bitcast %struct.msghdr* %8 to i8*
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 6
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 0
store i32 8, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 5
store %struct.iovec* %10, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 1
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 2
store i8* %13, i8** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 3
store i32 256, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i64 0, i32 4
store i64 0, i64* %22
%23 = icmp eq %struct.in_pktinfo* %4, null
br i1 %23, label %37, label %24
24: ; preds = %5
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
%26 = load i32, i32* @IPPROTO_IP
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @IP_PKTINFO
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 1
store i32 %28, i32* %29
%30 = call i32 @CMSG_LEN(i32 4)
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 0
store i32 %30, i32* %31
%32 = call i64 @CMSG_DATA(%struct.cmsghdr* %25)
%33 = getelementptr %struct.in_pktinfo, %struct.in_pktinfo* %4, i64 0, i32 0
%34 = inttoptr i64 %32 to i32*
%35 = load i32, i32* %33
store i32 %35, i32* %34
%36 = call i32 @CMSG_SPACE(i32 4)
br label %38
37: ; preds = %5
store i8* null, i8** %20
br label %38
38: ; preds = %37, %24
%39 = phi i32 [ 0, %37 ], [ %36, %24 ]
store i32 %39, i32* %21
%40 = call i32 @sendmsg(i32 %0, %struct.msghdr* %8, i32 0)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %56
44: ; preds = %38
%45 = icmp slt i32 %40, %2
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%47 = load i32, i32* %46
%48 = call i32 @ntohl(i32 %47)
br i1 %45, label %49, label %51
49: ; preds = %44
%50 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %48)
br label %56
51: ; preds = %44
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @ntohs(i32 %53)
%55 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %40, i32 %48, i32 %54)
br label %56
56: ; preds = %49, %51, %42
ret void
}