#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 iovec {
  char *iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ iovec_buf2;
typedef int /*<<< orphan*/ iovec_buf1;
typedef int /*<<< orphan*/ iovec_buf0;
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 readv(int, struct iovec *, int);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int 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 *req = "GET / HTTP/1.0\r\n\r\n";
  send(sock, req, sizeof(char) * strlen(req), 0);
  char iovec_buf0[20];
  char iovec_buf1[30];
  char iovec_buf2[40];
  struct iovec iovec[3];
  iovec[0].iov_base = iovec_buf0;
  iovec[0].iov_len = sizeof(iovec_buf0);
  iovec[1].iov_base = iovec_buf1;
  iovec[1].iov_len = sizeof(iovec_buf1);
  iovec[2].iov_base = iovec_buf2;
  iovec[2].iov_len = sizeof(iovec_buf2);
  if (readv(sock, iovec, -1) != -1)
    return (EXIT_FAILURE);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%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    1211 <main+0x81>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc4(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xc4(%rbp),%edi
mov    %rax,%rdx
lea    0xe05(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1358 <main+0x1c8>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xde1(%rip),%rdi
call   1070 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1298 <main+0x108>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc8(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xc8(%rbp),%edi
mov    %rax,%rdx
lea    0xd9e(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1358 <main+0x1c8>
lea    0xd98(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xd4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rsi
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0xc0(%rbp)
movl   $0x14,-0xb8(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0xb0(%rbp)
movl   $0x1e,-0xa8(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
movl   $0x28,-0x98(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0xffffffff,%edx
call   1080 <readv@plt>
cmp    $0xffffffff,%eax
je     134c <main+0x1bc>
mov    0x2c76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1358 <main+0x1c8>
mov    0x2c5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xa8,%rsp
mov    0x2e47(%rip),%r14
mov    (%r14),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1279 <main+0xe9>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe35(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
call   1070 <inet_aton@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1299 <main+0x109>
lea    0xe2c(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    0x40(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x14,0x18(%rsp)
lea    0x60(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x1e,0x28(%rsp)
lea    0x80(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x28,0x38(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xffffffff,%edx
call   1080 <readv@plt>
cmp    $0xffffffff,%eax
jne    12c1 <main+0x131>
mov    0x2d39(%rip),%rax
jmp    12c8 <main+0x138>
mov    0x2d50(%rip),%rax
mov    (%rax),%ebx
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd69(%rip),%rsi
jmp    12b7 <main+0x127>
mov    0x2d30(%rip),%rax
mov    (%rax),%ebx
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd69(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.iovec = type { i8*, i32 }
%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 = [19 x i8] c"GET / HTTP/1.0\0D\0A\0D\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 [20 x i8]
%6 = alloca [30 x i8]
%7 = alloca [40 x i8]
%8 = alloca [3 x %struct.iovec]
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 %69
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 %69
37: ; preds = %20
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %4
%38 = load i32, i32* %2
%39 = load i8*, i8** %4
%40 = load i8*, i8** %4
%41 = call i32 @strlen(i8* %40)
%42 = sext i32 %41 to i64
%43 = mul i64 1, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @send(i32 %38, i8* %39, i32 %44, i32 0)
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%47 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 0
%48 = getelementptr inbounds %struct.iovec, %struct.iovec* %47, i32 0, i32 0
store i8* %46, i8** %48
%49 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 0
%50 = getelementptr inbounds %struct.iovec, %struct.iovec* %49, i32 0, i32 1
store i32 20, i32* %50
%51 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%52 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 1
%53 = getelementptr inbounds %struct.iovec, %struct.iovec* %52, i32 0, i32 0
store i8* %51, i8** %53
%54 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 1
%55 = getelementptr inbounds %struct.iovec, %struct.iovec* %54, i32 0, i32 1
store i32 30, i32* %55
%56 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%57 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 2
%58 = getelementptr inbounds %struct.iovec, %struct.iovec* %57, i32 0, i32 0
store i8* %56, i8** %58
%59 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 2
%60 = getelementptr inbounds %struct.iovec, %struct.iovec* %59, i32 0, i32 1
store i32 40, i32* %60
%61 = load i32, i32* %2
%62 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %8, i64 0, i64 0
%63 = call i32 @readv(i32 %61, %struct.iovec* %62, i32 -1)
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %37
%66 = load i32, i32* @EXIT_FAILURE
store i32 %66, i32* %1
br label %69
67: ; preds = %37
%68 = load i32, i32* @EXIT_SUCCESS
store i32 %68, i32* %1
br label %69
69: ; preds = %67, %65, %31, %14
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.iovec = type { i8*, i32 }
%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 = [19 x i8] c"GET / HTTP/1.0\0D\0A\0D\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [20 x i8]
%3 = alloca [30 x i8]
%4 = alloca [40 x i8]
%5 = alloca [3 x %struct.iovec]
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
br label %55
17: ; preds = %0
%18 = bitcast %struct.sockaddr_in* %1 to i8*
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 8000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%24 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %23)
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i64 @connect(i32 %9, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %17
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
br label %53
34: ; preds = %17
%35 = call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @send(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 0)
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%38 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%39 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%40 = bitcast [3 x %struct.iovec]* %5 to i8*
%41 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 0
%42 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 0, i32 0
store i8* %37, i8** %42
%43 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 0, i32 1
store i32 20, i32* %43
%44 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 1, i32 0
store i8* %38, i8** %44
%45 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 1, i32 1
store i32 30, i32* %45
%46 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 2, i32 0
store i8* %39, i8** %46
%47 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 2, i32 1
store i32 40, i32* %47
%48 = call i32 @readv(i32 %9, %struct.iovec* %41, 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 = %34, %28
%54 = phi i32 [ %33, %28 ], [ %52, %34 ]
br label %55
55: ; preds = %53, %11
%56 = phi i32 [ %16, %11 ], [ %54, %53 ]
ret i32 %56
}