#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *host = "127.0.0.1";
  int port = 2000;
  int timeout = 2;
  int sockfd, n;
  char buffer[256];
  struct sockaddr_in serv_addr;
  struct hostent *server;
  struct timeval tv;
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  server = gethostbyname(host);
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(port);
  serv_addr.sin_family = AF_INET;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
  setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
  connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  n = read(sockfd, buffer, 255);
  if (n < 0) {
    perror("error reading from socket");
    return 1;
  }
  printf("%s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe50(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x7d0,-0x1c(%rbp)
movl   $0x2,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x158(%rbp)
movq   $0x0,-0x150(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x148(%rbp)
mov    -0x148(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x140(%rbp),%rsi
add    $0x8,%rsi
mov    -0x148(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x13c(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x140(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
lea    -0x158(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    -0x158(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    -0x24(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12de <main+0x14e>
lea    0xd3d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f8 <main+0x168>
lea    -0x130(%rbp),%rsi
lea    0xd38(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x128,%rsp
movl   $0x2,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xe4b(%rip),%rdi
call   1070 <gethostbyname@plt>
mov    0x8(%rax),%rsi
lea    0x8(%rsp),%rdi
mov    (%rax),%edx
call   1080 <memmove@plt>
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    %edi,(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    0x2de0(%rip),%rbx
mov    (%rbx),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r15,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1281 <main+0xf1>
lea    0xdbe(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd82(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
jmp    1270 <main+0xe0>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
@.str.1 = [26 x i8] c"error reading from socket\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.hostent*
%14 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 2000, i32* %7
store i32 2, i32* %8
%15 = load i32, i32* %8
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %17
%18 = load i8*, i8** %6
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %13
%20 = load %struct.hostent*, %struct.hostent** %13
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = bitcast i32* %25 to i8*
%27 = load %struct.hostent*, %struct.hostent** %13
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @bcopy(i8* %23, i8* %26, i32 %29)
%31 = load i32, i32* %7
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_RCVTIMEO
%42 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, %struct.timeval* %14, i32 16)
%43 = load i32, i32* %9
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_SNDTIMEO
%46 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, %struct.timeval* %14, i32 16)
%47 = load i32, i32* %9
%48 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = load i32, i32* %9
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 255)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %2
%56 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %60
57: ; preds = %2
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%59 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %55
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
@.str.1 = [26 x i8] c"error reading from socket\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.timeval
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 2, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 1
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to i8*
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
%16 = bitcast i32* %15 to i8*
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @htons(i32 2000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SOCK_STREAM
%24 = tail call i32 @socket(i32 %21, i32 %23, i32 0)
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_RCVTIMEO
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, %struct.timeval* %5, i32 16)
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_SNDTIMEO
%30 = call i32 @setsockopt(i32 %24, i32 %28, i32 %29, %struct.timeval* %5, i32 16)
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @connect(i32 %24, %struct.sockaddr* %31, i32 12)
%33 = call i32 @read(i32 %24, i8* %6, i32 255)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %2
%36 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %39
37: ; preds = %2
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %39
39: ; preds = %37, %35
%40 = phi i32 [ 1, %35 ], [ 0, %37 ]
ret i32 %40
}