#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;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ FIONREAD;
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*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, 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);
  int bytes;
  if (ioctl(sock, FIONREAD, &bytes) < 0) {
    fprintf(stderr, "ioctl() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1208 <main+0x78>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe0e(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1329 <main+0x199>
mov    0x2dd1(%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    0xdea(%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    1289 <main+0xf9>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdad(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1329 <main+0x199>
lea    0xda7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0x8(%rbp),%edi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    131d <main+0x18d>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd43(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1329 <main+0x199>
mov    0x2c84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e42(%rip),%r14
mov    (%r14),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     123b <main+0xab>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe3c(%rip),%rdi
lea    0x8(%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     125b <main+0xcb>
lea    0xe35(%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>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     127b <main+0xeb>
mov    0x2d6f(%rip),%rax
jmp    12aa <main+0x11a>
mov    0x2d86(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda7(%rip),%rsi
jmp    1299 <main+0x109>
mov    0x2d66(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda7(%rip),%rsi
jmp    1299 <main+0x109>
mov    0x2d46(%rip),%rax
mov    (%rax),%ebx
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, 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"
@FIONREAD = global i32 0
@.str.4 = [21 x i8] c"ioctl() 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 i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %2
%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 = call i8* @strerror(i32 %13)
%15 = 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
store i32 %16, i32* %1
br label %55
17: ; preds = %0
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 8000)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %22)
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @connect(i32 %24, %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
store i32 %33, i32* %1
br label %55
34: ; preds = %17
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %4
%35 = load i32, i32* %2
%36 = load i8*, i8** %4
%37 = load i8*, i8** %4
%38 = call i32 @strlen(i8* %37)
%39 = sext i32 %38 to i64
%40 = mul i64 1, %39
%41 = trunc i64 %40 to i32
%42 = call i32 @send(i32 %35, i8* %36, i32 %41, i32 0)
%43 = load i32, i32* %2
%44 = load i32, i32* @FIONREAD
%45 = call i64 @ioctl(i32 %43, i32 %44, i32* %5)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %34
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @EXIT_FAILURE
store i32 %52, i32* %1
br label %55
53: ; preds = %34
%54 = load i32, i32* @EXIT_SUCCESS
store i32 %54, i32* %1
br label %55
55: ; preds = %53, %47, %28, %11
%56 = load i32, i32* %1
ret i32 %56
}

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