#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;
  int /*<<< orphan*/ member_0;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
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*/ 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*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
int main(void) {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd >= 0) {
    struct sockaddr_in serv_addr = {0};
    struct timeval tv = {0};
    tv.tv_sec = 10;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
               sizeof(struct timeval));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("192.168.1.2");
    serv_addr.sin_port = htons(666);
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) >=
        0) {
      char header[2] = {0x01, 0x00};
      char buffer[256];
      int n;
      printf("Please enter the message: ");
      memset((void *)buffer, 0, 256);
      fgets(buffer, 255, stdin);
      header[1] = strlen(buffer) - 1;
      write(sockfd, header, 2);
      n = write(sockfd, buffer, strlen(buffer));
      if (n >= 0) {
        memset((void *)buffer, 0, 256);
        n = read(sockfd, buffer, 255);
        if (n >= 0) {
          printf("%s\n", buffer);
        } else {
          perror("ERROR reading from socket");
        }
      } else {
        perror("ERROR writing to socket");
      }
    } else {
      perror("ERROR connecting");
    }
    close(sockfd);
  } else {
    perror("ERROR opening socket");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     13d8 <main+0x208>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
movl   $0xa,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xd9a(%rip),%rdi
call   10c0 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x29a,%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jl     13bf <main+0x1ef>
mov    0xd6f(%rip),%ax
mov    %ax,-0x22(%rbp)
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x130(%rbp),%rdi
mov    0x2d08(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   10b0 <fgets@plt>
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
mov    %al,-0x21(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x144(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x140(%rbp)
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x144(%rbp),%edi
mov    -0x140(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jl     13ae <main+0x1de>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   10a0 <read@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jl     139d <main+0x1cd>
lea    -0x130(%rbp),%rsi
lea    0xc98(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13a9 <main+0x1d9>
lea    0xc89(%rip),%rdi
call   10d0 <perror@plt>
jmp    13ba <main+0x1ea>
lea    0xc92(%rip),%rdi
call   10d0 <perror@plt>
jmp    13cb <main+0x1fb>
lea    0xc99(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
jmp    13e4 <main+0x214>
lea    0xc91(%rip),%rdi
call   10d0 <perror@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x128,%rsp
mov    0x2df0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1339 <main+0x159>
mov    %eax,%ebp
movl   $0x0,0x14(%rsp)
movq   $0xa,0x18(%rsp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebp,%edi
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
lea    0xdb2(%rip),%rdi
call   10d0 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0x29a,%edi
call   1070 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   10f0 <connect@plt>
test   %rax,%rax
js     1347 <main+0x167>
movw   $0x1,0x6(%rsp)
lea    0xd7f(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10c0 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    $0xff,%al
mov    %al,0x7(%rsp)
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <write@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %eax,%eax
js     1350 <main+0x170>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   10b0 <read@plt>
test   %eax,%eax
js     1359 <main+0x179>
lea    0x20(%rsp),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd2a(%rip),%rdi
call   10e0 <perror@plt>
jmp    132d <main+0x14d>
lea    0xd0b(%rip),%rdi
jmp    1360 <main+0x180>
lea    0xcea(%rip),%rdi
jmp    1360 <main+0x180>
lea    0xcc7(%rip),%rdi
call   10e0 <perror@plt>
jmp    1326 <main+0x146>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [12 x i8] c"192.168.1.2\00"
@__const.main.header = [2 x i8] c"\01\00"
@.str.1 = [27 x i8] c"Please enter the message: \00"
@stdin = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@.str.4 = [24 x i8] c"ERROR writing to socket\00"
@.str.5 = [17 x i8] c"ERROR connecting\00"
@.str.6 = [21 x i8] c"ERROR opening socket\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.timeval
%5 = alloca [2 x i8]
%6 = alloca [256 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %78
13: ; preds = %0
%14 = bitcast %struct.sockaddr_in* %3 to i8*
%15 = bitcast %struct.timeval* %4 to i8*
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 10, i32* %16
%17 = load i32, i32* %2
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_RCVTIMEO
%20 = bitcast %struct.timeval* %4 to i8*
%21 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i32 8)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @inet_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 666)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %2
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 16)
%32 = icmp sge i64 %31, 0
br i1 %32, label %33, label %73
33: ; preds = %13
%34 = bitcast [2 x i8]* %5 to i8*
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 256)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* @stdin
%40 = call i32 @fgets(i8* %38, i32 255, i32 %39)
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = sub nsw i32 %42, 1
%44 = trunc i32 %43 to i8
%45 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %44, i8* %45
%46 = load i32, i32* %2
%47 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%48 = call i32 @write(i32 %46, i8* %47, i32 2)
%49 = load i32, i32* %2
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @write(i32 %49, i8* %50, i32 %52)
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %70
56: ; preds = %33
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i32 0, i32 256)
%59 = load i32, i32* %2
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 255)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp sge i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %56
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %65)
br label %69
67: ; preds = %56
%68 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %69
69: ; preds = %67, %64
br label %72
70: ; preds = %33
%71 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %72
72: ; preds = %70, %69
br label %75
73: ; preds = %13
%74 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %75
75: ; preds = %73, %72
%76 = load i32, i32* %2
%77 = call i32 @close(i32 %76)
br label %80
78: ; preds = %0
%79 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %80
80: ; preds = %78, %75
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [12 x i8] c"192.168.1.2\00"
@.str.1 = [27 x i8] c"Please enter the message: \00"
@stdin = global i32 0
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@.str.4 = [24 x i8] c"ERROR writing to socket\00"
@.str.5 = [17 x i8] c"ERROR connecting\00"
@.str.6 = [21 x i8] c"ERROR opening socket\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i64
%3 = alloca i16
%4 = bitcast i16* %3 to [2 x i8]*
%5 = alloca [256 x i8]
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %56
10: ; preds = %0
%11 = bitcast %struct.sockaddr_in* %1 to i8*
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 0, i32* %12
%13 = bitcast i64* %2 to i8*
store i64 10, i64* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVTIMEO
%16 = call i32 @setsockopt(i32 %8, i32 %14, i32 %15, i8* %13, i32 8)
%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 = call i32 @inet_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 666)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i64 @connect(i32 %8, %struct.sockaddr* %23, i32 16)
%25 = icmp sgt i64 %24, -1
br i1 %25, label %26, label %52
26: ; preds = %10
%27 = bitcast i16* %3 to i8*
store i16 1, i16* %3
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @memset(i8* %28, i32 0, i32 256)
%31 = load i32, i32* @stdin
%32 = call i32 @fgets(i8* %28, i32 255, i32 %31)
%33 = call i32 @strlen(i8* %28)
%34 = trunc i32 %33 to i8
%35 = add i8 %34, -1
%36 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 %35, i8* %36
%37 = call i32 @write(i32 %8, i8* %27, i32 2)
%38 = call i32 @strlen(i8* %28)
%39 = call i32 @write(i32 %8, i8* %28, i32 %38)
%40 = icmp sgt i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %26
%42 = call i32 @memset(i8* %28, i32 0, i32 256)
%43 = call i32 @read(i32 %8, i8* %28, i32 255)
%44 = icmp sgt i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @puts(i8* %28)
br label %51
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %51
49: ; preds = %26
%50 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %45, %47, %49
br label %54
52: ; preds = %10
%53 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %51
%55 = call i32 @close(i32 %8)
br label %58
56: ; preds = %0
%57 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %58
58: ; preds = %56, %54
ret i32 0
}