#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_TCP;
int /*<<< orphan*/ SOCK_STREAM;
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);
int sendmsg(int, struct msghdr *, int);
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_STREAM, IPPROTO_TCP)) < 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, -1) != -1)
    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    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%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    0x2ddf(%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    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1342 <main+0x1b2>
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    0x2d5e(%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    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1342 <main+0x1b2>
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
mov    $0xffffffff,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
je     1336 <main+0x1a6>
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1342 <main+0x1b2>
mov    0x2c73(%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    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1286 <main+0xf6>
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     12a6 <main+0x116>
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
mov    $0xffffffff,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
jne    12ce <main+0x13e>
mov    0x2d2c(%rip),%rax
jmp    12d5 <main+0x145>
mov    0x2d43(%rip),%rax
mov    (%rax),%ebx
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd5c(%rip),%rsi
jmp    12c4 <main+0x134>
mov    0x2d23(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd5c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2ce3(%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_STREAM = global i32 0
@IPPROTO_TCP = 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"
@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_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%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 %70
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 %70
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 i32 @sendmsg(i32 %63, %struct.msghdr* %8, i32 -1)
%65 = icmp ne i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %37
%67 = load i32, i32* @EXIT_FAILURE
store i32 %67, i32* %1
br label %70
68: ; preds = %37
%69 = load i32, i32* @EXIT_SUCCESS
store i32 %69, i32* %1
br label %70
70: ; preds = %68, %66, %31, %14
%71 = load i32, i32* %1
ret i32 %71
}

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_STREAM = global i32 0
@IPPROTO_TCP = 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"
@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_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%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 %55
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 %53
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 i32 @sendmsg(i32 %7, %struct.msghdr* %3, i32 -1)
%49 = icmp eq i32 %48, -1
%50 = load i32, i32* @EXIT_SUCCESS
%51 = load i32, i32* @EXIT_FAILURE
%52 = select i1 %49, i32 %50, i32 %51
br label %53
53: ; preds = %32, %26
%54 = phi i32 [ %31, %26 ], [ %52, %32 ]
br label %55
55: ; preds = %53, %9
%56 = phi i32 [ %14, %9 ], [ %54, %53 ]
ret i32 %56
}