#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_usec;
  scalar_t__ tv_sec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ recv_all(int);
scalar_t__ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *const argv[]) {
  int socket_fd;
  struct sockaddr_in client;
  char message[4096];
  char received_msg[4096];
  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_fd == -1) {
    perror("ERROR coldn't create socket");
  }
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 200000;
  setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv,
             sizeof(struct timeval));
  client.sin_addr.s_addr = inet_addr("127.0.0.1");
  client.sin_family = AF_INET;
  client.sin_port = htons(25005);
  if (connect(socket_fd, (struct sockaddr *)&client, sizeof(client)) < 0) {
    perror("ERROR Connect filed");
    return 1;
  }
  printf("Connected.\n");
  while (1) {
    printf("$ ");
    if (fgets(message, 4096, stdin) != NULL) {
      if (send(socket_fd, message, strlen(message), 0) < 0) {
        perror("ERROR Send filed");
        return 1;
      }
    }
    recv_all(socket_fd);
  }
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fb <main+0x4b>
lea    0xe0a(%rip),%rdi
call   10b0 <perror@plt>
movq   $0x0,-0x2028(%rbp)
movl   $0x30d40,-0x2030(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
lea    -0x2030(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
lea    0xdde(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x61ad,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    1292 <main+0xe2>
lea    0xda5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133b <main+0x18b>
lea    0xda1(%rip),%rdi
call   1070 <printf@plt>
lea    0xda1(%rip),%rdi
call   1070 <printf@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     132e <main+0x17e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x203c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x2038(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x203c(%rbp),%edi
mov    -0x2038(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0x0,%rax
jge    1329 <main+0x179>
lea    0xd31(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133b <main+0x18b>
jmp    132e <main+0x17e>
mov    -0x14(%rbp),%edi
call   1080 <recv_all@plt>
jmp    129e <main+0xee>
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1020,%rsp
mov    0x2e0a(%rip),%rbx
mov    (%rbx),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11fb <main+0x3b>
lea    0xe0a(%rip),%rdi
call   10c0 <perror@plt>
movq   $0x0,0x18(%rsp)
movl   $0x30d40,0x10(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
lea    0xde5(%rip),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    $0x61ad,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jns    1270 <main+0xb0>
lea    0xdb8(%rip),%r14
jmp    12e0 <main+0x120>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb7(%rip),%r15
mov    0x2d4e(%rip),%r12
lea    0x20(%rsp),%rbx
lea    0xda7(%rip),%r14
jmp    12a7 <main+0xe7>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1090 <recv_all@plt>
mov    %r15,%rdi
call   1080 <printf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     12a0 <main+0xe0>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
test   %rax,%rax
jns    12a0 <main+0xe0>
mov    %r14,%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"ERROR coldn't create socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [20 x i8] c"ERROR Connect filed\00"
@.str.3 = [12 x i8] c"Connected.\0A\00"
@.str.4 = [3 x i8] c"$ \00"
@stdin = global i32 0
@.str.5 = [17 x i8] c"ERROR Send filed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [4096 x i8]
%9 = alloca [4096 x i8]
%10 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 200000, i32* %20
%21 = load i32, i32* %6
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_RCVTIMEO
%24 = bitcast %struct.timeval* %10 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 16)
%26 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %29, i32* %30
%31 = call i32 @htons(i32 25005)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %18
%38 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %60
39: ; preds = %18
%40 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %57
%42 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* @stdin
%45 = call i32* @fgets(i8* %43, i32 4096, i32 %44)
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %57
47: ; preds = %41
%48 = load i32, i32* %6
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i64 @send(i32 %48, i8* %49, i32 %51, i32 0)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %60
56: ; preds = %47
br label %57
57: ; preds = %56, %41
%58 = load i32, i32* %6
%59 = call i32 @recv_all(i32 %58)
br label %41
60: ; preds = %54, %37
%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.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"ERROR coldn't create socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [20 x i8] c"ERROR Connect filed\00"
@.str.4 = [3 x i8] c"$ \00"
@stdin = global i32 0
@.str.5 = [17 x i8] c"ERROR Send filed\00"
@str = [11 x i8] c"Connected.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = alloca %struct.timeval
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = bitcast %struct.timeval* %5 to i8*
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 200000, i32* %17
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_RCVTIMEO
%20 = call i32 @setsockopt(i32 %10, i32 %18, i32 %19, i8* %15, i32 16)
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = call i32 @htons(i32 25005)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i64 @connect(i32 %10, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %43, label %30
30: ; preds = %14
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %41, %30
%33 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @stdin
%35 = call i32* @fgets(i8* %7, i32 4096, i32 %34)
%36 = icmp eq i32* %35, null
br i1 %36, label %41, label %37
37: ; preds = %32
%38 = call i32 @strlen(i8* %7)
%39 = call i64 @send(i32 %10, i8* %7, i32 %38, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37, %32
%42 = call i32 @recv_all(i32 %10)
br label %32
43: ; preds = %37, %14
%44 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %14 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), %37 ]
%45 = call i32 @perror(i8* %44)
ret i32 1
}