#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;
  int /*<<< orphan*/ sin_family;
};
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 {
  char *iov_base;
  int /*<<< orphan*/ iov_len;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef int /*<<< orphan*/ recv_addr;
typedef int /*<<< orphan*/ control_info;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ IP_TOS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_IP;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  int sock, sock_rc;
  int tos = 16;
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  struct sockaddr_in recv_addr;
  struct msghdr out_message;
  struct iovec iovectors[1];
  unsigned char control_info[CMSG_SPACE(sizeof tos)];
  struct cmsghdr *cmsg;
  recv_addr.sin_family = AF_INET;
  recv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  recv_addr.sin_port = htons(5000);
  out_message.msg_name = &recv_addr;
  out_message.msg_namelen = sizeof(recv_addr);
  out_message.msg_iov = iovectors;
  out_message.msg_iovlen = 1;
  out_message.msg_control = control_info;
  out_message.msg_controllen = sizeof(control_info);
  out_message.msg_flags = 0;
  iovectors[0].iov_base = "hello";
  iovectors[0].iov_len = strlen("hello");
  cmsg = CMSG_FIRSTHDR(&out_message);
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  cmsg->cmsg_level = SOL_IP;
  cmsg->cmsg_type = IP_TOS;
  *(int *)CMSG_DATA(cmsg) = tos;
  sock_rc = sendmsg(sock, &out_message, 0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x10,-0xc(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    $0x4,%edi
call   1090 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x88(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x70(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1388,%edi
call   1060 <htons@plt>
mov    -0x90(%rbp),%rcx
mov    %eax,%edx
mov    -0x88(%rbp),%rax
mov    %edx,-0x18(%rbp)
lea    -0x18(%rbp),%rdx
mov    %rdx,-0x20(%rbp)
movl   $0xc,-0x48(%rbp)
lea    -0x60(%rbp),%rdx
mov    %rdx,-0x28(%rbp)
movl   $0x1,-0x44(%rbp)
mov    %rcx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xd94(%rip),%rax
mov    %rax,-0x60(%rbp)
lea    0xd89(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x58(%rbp)
lea    -0x48(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x78(%rbp)
mov    $0x4,%edi
call   1080 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2d52(%rip),%rax
mov    (%rax),%ecx
mov    -0x78(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2d22(%rip),%rax
mov    (%rax),%ecx
mov    -0x78(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x78(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x7c(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x8(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    0x2e38(%rip),%r15
mov    (%r15),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,%r14d
mov    $0x4,%edi
call   1090 <CMSG_SPACE@plt>
mov    %eax,%r13d
mov    %rsp,%r12
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r15),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    $0x1388,%edi
call   1060 <htons@plt>
mov    %eax,-0x38(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
movabs $0x10000000c,%rax
mov    %rax,-0x80(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    %rbx,-0x78(%rbp)
mov    %r13d,-0x70(%rbp)
movq   $0x0,-0x68(%rbp)
lea    0xdbf(%rip),%rdi
mov    %rdi,-0x50(%rbp)
call   1050 <strlen@plt>
mov    %eax,-0x48(%rbp)
lea    -0x80(%rbp),%r15
mov    %r15,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1080 <CMSG_LEN@plt>
mov    %eax,0x8(%rbx)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
movl   $0x10,(%rax)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.msghdr = type { i32, i32, i8*, i32, i64, %struct.iovec*, %struct.sockaddr_in* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_LOOPBACK = global i32 0
@.str = [6 x i8] c"hello\00"
@SOL_IP = global i32 0
@IP_TOS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.msghdr
%6 = alloca [1 x %struct.iovec]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.cmsghdr*
store i32 16, i32* %3
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %1
%14 = call i32 @CMSG_SPACE(i32 4)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %7
%17 = alloca i8, i64 %15
store i64 %15, i64* %8
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_LOOPBACK
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 5000)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 6
store %struct.sockaddr_in* %4, %struct.sockaddr_in** %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i32 12, i32* %27
%28 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 5
store %struct.iovec* %28, %struct.iovec** %29
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i32 1, i32* %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i8* %17, i8** %31
%32 = trunc i64 %15 to i32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 4
store i64 0, i64* %34
%35 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %35, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %36
%37 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%38 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %38, i32 0, i32 1
store i32 %37, i32* %39
%40 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
store %struct.cmsghdr* %40, %struct.cmsghdr** %9
%41 = call i32 @CMSG_LEN(i32 4)
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 2
store i32 %41, i32* %43
%44 = load i32, i32* @SOL_IP
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 1
store i32 %44, i32* %46
%47 = load i32, i32* @IP_TOS
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %48, i32 0, i32 0
store i32 %47, i32* %49
%50 = load i32, i32* %3
%51 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%52 = call i64 @CMSG_DATA(%struct.cmsghdr* %51)
%53 = inttoptr i64 %52 to i32*
store i32 %50, i32* %53
%54 = load i32, i32* %1
%55 = call i32 @sendmsg(i32 %54, %struct.msghdr* %5, i32 0)
store i32 %55, i32* %2
%56 = load i8*, i8** %7
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.msghdr = type { i32, i32, i8*, i32, i64, %struct.iovec*, %struct.sockaddr_in* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_LOOPBACK = global i32 0
@.str = [6 x i8] c"hello\00"
@SOL_IP = global i32 0
@IP_TOS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.msghdr
%3 = alloca [1 x %struct.iovec]
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_UDP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = bitcast %struct.sockaddr_in* %1 to i8*
%9 = bitcast %struct.msghdr* %2 to i8*
%10 = bitcast [1 x %struct.iovec]* %3 to i8*
%11 = tail call i32 @CMSG_SPACE(i32 4)
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_LOOPBACK
%18 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 5000)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 6
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 0
store i32 12, i32* %23
%24 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %3, i64 0, i64 0
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 5
store %struct.iovec* %24, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 1
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 2
store i8* %14, i8** %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 3
store i32 %11, i32* %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 4
store i64 0, i64* %29
%30 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %3, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %30
%31 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%32 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %3, i64 0, i64 0, i32 1
store i32 %31, i32* %32
%33 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%34 = call i32 @CMSG_LEN(i32 4)
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i64 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @SOL_IP
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i64 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @IP_TOS
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i64 0, i32 0
store i32 %38, i32* %39
%40 = call i64 @CMSG_DATA(%struct.cmsghdr* %33)
%41 = inttoptr i64 %40 to i32*
store i32 16, i32* %41
%42 = call i32 @sendmsg(i32 %7, %struct.msghdr* %2, i32 0)
ret i32 0
}