#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct msghdr {
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  void *iov_len;
};
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ iovec;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct msghdr *, char, int);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *strlen(char *);
int main(void) {
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    fprintf(stderr, "socket() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8000);
  inet_aton("127.0.0.1", &addr.sin_addr);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    fprintf(stderr, "connect() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  char *iovec_buf0 = "short string\n";
  char *iovec_buf1 = "This is a longer string\n";
  char *iovec_buf2 = "This is the longest string in this example\n";
  struct iovec iovec[3];
  iovec[0].iov_base = iovec_buf0;
  iovec[0].iov_len = strlen(iovec_buf0);
  iovec[1].iov_base = iovec_buf1;
  iovec[1].iov_len = strlen(iovec_buf1);
  iovec[2].iov_base = iovec_buf2;
  iovec[2].iov_len = strlen(iovec_buf2);
  struct msghdr msg;
  memset(&msg, '\0', sizeof(msg));
  msg.msg_iov = iovec;
  msg.msg_iovlen = sizeof(iovec) / sizeof(struct iovec);
  if (sendmsg(sock, &msg, 0) < 0) {
    fprintf(stderr, "sendmsg() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    120b <main+0x7b>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xe0b(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1dc>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xde7(%rip),%rdi
call   1080 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    128c <main+0xfc>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xdaa(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1dc>
lea    0xda4(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xda7(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xdb5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0x3,-0x70(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jge    1360 <main+0x1d0>
mov    0x2ca6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    %rax,%rdx
lea    0xd40(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136c <main+0x1dc>
mov    0x2c51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    0x2e4a(%rip),%r14
mov    (%r14),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1283 <main+0xf3>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe38(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
call   1080 <inet_aton@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     12a3 <main+0x113>
lea    0xe2f(%rip),%rdi
mov    %rdi,0x20(%rsp)
call   1040 <strlen@plt>
mov    %rax,0x28(%rsp)
lea    0xe27(%rip),%rdi
mov    %rdi,0x30(%rsp)
call   1040 <strlen@plt>
mov    %rax,0x38(%rsp)
lea    0xe2a(%rip),%rdi
mov    %rdi,0x40(%rsp)
call   1040 <strlen@plt>
mov    %rax,0x48(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x3,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
js     12c3 <main+0x133>
mov    0x2d37(%rip),%rax
jmp    12f2 <main+0x162>
mov    0x2d46(%rip),%rax
mov    (%rax),%ebx
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd5f(%rip),%rsi
jmp    12e1 <main+0x151>
mov    0x2d26(%rip),%rax
mov    (%rax),%ebx
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd5f(%rip),%rsi
jmp    12e1 <main+0x151>
mov    0x2d06(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.iovec = type { i8*, i8* }
%struct.msghdr = type { i32, %struct.iovec* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"socket() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"connect() failed: %s\0A.\00"
@.str.3 = [14 x i8] c"short string\0A\00"
@.str.4 = [25 x i8] c"This is a longer string\0A\00"
@.str.5 = [44 x i8] c"This is the longest string in this example\0A\00"
@.str.6 = [23 x i8] c"sendmsg() failed: %s\0A.\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [3 x %struct.iovec]
%8 = alloca %struct.msghdr
store i32 0, i32* %1
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %1
br label %74
20: ; preds = %0
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 8000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%26 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %25)
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %1
br label %74
37: ; preds = %20
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8** %6
%38 = load i8*, i8** %4
%39 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %39, i32 0, i32 0
store i8* %38, i8** %40
%41 = load i8*, i8** %4
%42 = call i8* @strlen(i8* %41)
%43 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %43, i32 0, i32 1
store i8* %42, i8** %44
%45 = load i8*, i8** %5
%46 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 1
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %46, i32 0, i32 0
store i8* %45, i8** %47
%48 = load i8*, i8** %5
%49 = call i8* @strlen(i8* %48)
%50 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 1
%51 = getelementptr inbounds %struct.iovec, %struct.iovec* %50, i32 0, i32 1
store i8* %49, i8** %51
%52 = load i8*, i8** %6
%53 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 2
%54 = getelementptr inbounds %struct.iovec, %struct.iovec* %53, i32 0, i32 0
store i8* %52, i8** %54
%55 = load i8*, i8** %6
%56 = call i8* @strlen(i8* %55)
%57 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 2
%58 = getelementptr inbounds %struct.iovec, %struct.iovec* %57, i32 0, i32 1
store i8* %56, i8** %58
%59 = call i32 @memset(%struct.msghdr* %8, i8 signext 0, i32 16)
%60 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%61 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store %struct.iovec* %60, %struct.iovec** %61
%62 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 3, i32* %62
%63 = load i32, i32* %2
%64 = call i64 @sendmsg(i32 %63, %struct.msghdr* %8, i32 0)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %37
%67 = load i32, i32* @stderr
%68 = load i32, i32* @errno
%69 = call i8* @strerror(i32 %68)
%70 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %69)
%71 = load i32, i32* @EXIT_FAILURE
store i32 %71, i32* %1
br label %74
72: ; preds = %37
%73 = load i32, i32* @EXIT_SUCCESS
store i32 %73, i32* %1
br label %74
74: ; preds = %72, %66, %31, %14
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.iovec = type { i8*, i8* }
%struct.msghdr = type { i32, %struct.iovec* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"socket() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"connect() failed: %s\0A.\00"
@.str.3 = [14 x i8] c"short string\0A\00"
@.str.4 = [25 x i8] c"This is a longer string\0A\00"
@.str.5 = [44 x i8] c"This is the longest string in this example\0A\00"
@.str.6 = [23 x i8] c"sendmsg() failed: %s\0A.\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [3 x %struct.iovec]
%3 = alloca %struct.msghdr
%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 = icmp slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
br label %60
15: ; preds = %0
%16 = bitcast %struct.sockaddr_in* %1 to i8*
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 8000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%22 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i64 @connect(i32 %7, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
br label %58
32: ; preds = %15
%33 = bitcast [3 x %struct.iovec]* %2 to i8*
%34 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0
%35 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %35
%36 = call i8* @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%37 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0, i32 1
store i8* %36, i8** %37
%38 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 1, i32 0
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8** %38
%39 = call i8* @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%40 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 1, i32 1
store i8* %39, i8** %40
%41 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 2, i32 0
store i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8** %41
%42 = call i8* @strlen(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%43 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 2, i32 1
store i8* %42, i8** %43
%44 = bitcast %struct.msghdr* %3 to i8*
%45 = call i32 @memset(%struct.msghdr* %3, i8 signext 0, i32 16)
%46 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store %struct.iovec* %34, %struct.iovec** %46
%47 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i32 3, i32* %47
%48 = call i64 @sendmsg(i32 %7, %struct.msghdr* %3, i32 0)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %32
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %32, %50
%56 = phi i32* [ @EXIT_FAILURE, %50 ], [ @EXIT_SUCCESS, %32 ]
%57 = load i32, i32* %56
br label %58
58: ; preds = %55, %26
%59 = phi i32 [ %31, %26 ], [ %57, %55 ]
br label %60
60: ; preds = %58, %9
%61 = phi i32 [ %14, %9 ], [ %59, %58 ]
ret i32 %61
}