#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ cbreak();
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ endwin();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char getch();
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ initscr();
int /*<<< orphan*/ noecho();
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printw(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int serverfd, flag, n = 0, rcv_len;
  struct sockaddr_in server_addr;
  char buffer[1024], temp;
  initscr();
  cbreak();
  noecho();
  if ((serverfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Socket");
    exit(errno);
  }
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(17231);
  if (inet_aton("127.0.0.1", &(server_addr.sin_addr)) == 0) {
    perror("127.0.0.1");
    exit(errno);
  }
  if (connect(serverfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) !=
      0) {
    perror("Connect");
    while (1)
      ;
    exit(errno);
  }
  printw("Connected to %s:%d...\n", inet_ntoa(server_addr.sin_addr),
         ntohs(server_addr.sin_port));
  while (1) {
    n = 0;
    flag = 0;
    printw("Enter string to send to the server for echoing (ESC to exit):\n");
    while (1) {
      temp = getch();
      if (temp == 27) {
        flag = -1;
        break;
      } else if (temp == 10) {
        break;
      } else {
        buffer[n++] = temp;
        printw("%c", temp);
      }
    }
    if (flag == -1) {
      break;
    }
    send(serverfd, buffer, n, 0);
    rcv_len = recv(serverfd, buffer, 1024, 0);
    buffer[rcv_len] = '\0';
    printw("\nString echoed by the server: \"%s\"\n", buffer);
  }
  close(serverfd);
  endwin();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1080 <initscr@plt>
mov    $0x0,%al
call   1040 <cbreak@plt>
mov    $0x0,%al
call   1110 <noecho@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    128d <main+0x6d>
lea    0xd86(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1100 <bzero@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x434f,%edi
call   1060 <htons@plt>
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    0xd44(%rip),%rdi
call   10b0 <inet_aton@plt>
cmp    $0x0,%rax
jne    12ec <main+0xcc>
lea    0xd2e(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1130 <connect@plt>
cmp    $0x0,%rax
je     1318 <main+0xf8>
lea    0xd03(%rip),%rdi
call   10f0 <perror@plt>
jmp    1313 <main+0xf3>
mov    -0x1c(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %eax,-0x428(%rbp)
mov    -0x20(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0x428(%rbp),%esi
mov    %eax,%edx
lea    0xcdc(%rip),%rdi
mov    $0x0,%al
call   10a0 <printw@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   10a0 <printw@plt>
mov    $0x0,%al
call   10d0 <getch@plt>
mov    %al,-0x421(%rbp)
movsbl -0x421(%rbp),%eax
cmp    $0x1b,%eax
jne    1389 <main+0x169>
movl   $0xffffffff,-0xc(%rbp)
jmp    13d7 <main+0x1b7>
movsbl -0x421(%rbp),%eax
cmp    $0xa,%eax
jne    139e <main+0x17e>
jmp    13d7 <main+0x1b7>
mov    -0x421(%rbp),%cl
mov    -0x10(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
cltq
mov    %cl,-0x420(%rbp,%rax,1)
movsbl -0x421(%rbp),%esi
lea    0xca9(%rip),%rdi
mov    $0x0,%al
call   10a0 <printw@plt>
jmp    13d2 <main+0x1b2>
jmp    1360 <main+0x140>
cmpl   $0xffffffff,-0xc(%rbp)
jne    13e6 <main+0x1c6>
jmp    1439 <main+0x219>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x10(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xc45(%rip),%rdi
mov    $0x0,%al
call   10a0 <printw@plt>
jmp    1344 <main+0x124>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    $0x0,%al
call   10e0 <endwin@plt>
xor    %edi,%edi
call   1120 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
xor    %eax,%eax
call   1080 <initscr@plt>
xor    %eax,%eax
call   1040 <cbreak@plt>
xor    %eax,%eax
call   1110 <noecho@plt>
mov    0x2d9b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
test   %eax,%eax
js     12c2 <main+0xa2>
mov    %eax,%r13d
mov    %rsp,%rdi
mov    $0xc,%esi
call   1100 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x434f,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
lea    0x4(%rsp),%rsi
lea    0xd75(%rip),%rdi
call   10b0 <inet_aton@plt>
test   %rax,%rax
je     12cb <main+0xab>
mov    %rsp,%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   1130 <connect@plt>
test   %rax,%rax
je     12e5 <main+0xc5>
lea    0xd59(%rip),%rdi
call   10f0 <perror@plt>
nopl   (%rax)
jmp    12c0 <main+0xa0>
lea    0xd37(%rip),%rdi
jmp    12d2 <main+0xb2>
lea    0xd35(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x4(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %eax,%ebx
mov    (%rsp),%edi
call   10c0 <ntohs@plt>
lea    0xd1a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <printw@plt>
lea    0xd1f(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xd55(%rip),%r12
lea    0xd4b(%rip),%rbx
jmp    1365 <main+0x145>
cs nopw 0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10a0 <printw@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   10a0 <printw@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10d0 <getch@plt>
cmp    $0xa,%al
je     1330 <main+0x110>
cmp    $0x1b,%al
je     13a6 <main+0x186>
movsbl %al,%esi
mov    %al,0x10(%rsp,%rbp,1)
add    $0x1,%rbp
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <printw@plt>
jmp    1380 <main+0x160>
mov    %r13d,%edi
call   1090 <close@plt>
xor    %eax,%eax
call   10e0 <endwin@plt>
xor    %edi,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"Socket\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [8 x i8] c"Connect\00"
@.str.3 = [23 x i8] c"Connected to %s:%d...\0A\00"
@.str.4 = [63 x i8] c"Enter string to send to the server for echoing (ESC to exit):\0A\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [36 x i8] c"\0AString echoed by the server: \22%s\22\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca [1024 x i8]
%8 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %4
%9 = call i32 (...) @initscr()
%10 = call i32 (...) @cbreak()
%11 = call i32 (...) @noecho()
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %0
%21 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 17231)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%27 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @errno
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %20
%34 = load i32, i32* %2
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %40
br label %40
41: ; preds = %33
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%43 = load i32, i32* %42
%44 = call i32 @inet_ntoa(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %47)
br label %49
49: ; preds = %41, %77
store i32 0, i32* %4
store i32 0, i32* %3
%50 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %72
%52 = call signext i8 (...) @getch()
store i8 %52, i8* %8
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 27
br i1 %55, label %56, label %57
56: ; preds = %51
store i32 -1, i32* %3
br label %73
57: ; preds = %51
%58 = load i8, i8* %8
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 10
br i1 %60, label %61, label %62
61: ; preds = %57
br label %73
62: ; preds = %57
%63 = load i8, i8* %8
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 1
store i32 %65, i32* %4
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %66
store i8 %63, i8* %67
%68 = load i8, i8* %8
%69 = sext i8 %68 to i32
%70 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %62
br label %72
72: ; preds = %71
br label %51
73: ; preds = %61, %56
%74 = load i32, i32* %3
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %77
76: ; preds = %73
br label %90
77: ; preds = %73
%78 = load i32, i32* %2
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%80 = load i32, i32* %4
%81 = call i32 @send(i32 %78, i8* %79, i32 %80, i32 0)
%82 = load i32, i32* %2
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%84 = call i32 @recv(i32 %82, i8* %83, i32 1024, i32 0)
store i32 %84, i32* %5
%85 = load i32, i32* %5
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %86
store i8 0, i8* %87
%88 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%89 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %88)
br label %49
90: ; preds = %76
%91 = load i32, i32* %2
%92 = call i32 @close(i32 %91)
%93 = call i32 (...) @endwin()
%94 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"Socket\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [8 x i8] c"Connect\00"
@.str.3 = [23 x i8] c"Connected to %s:%d...\0A\00"
@.str.4 = [63 x i8] c"Enter string to send to the server for echoing (ESC to exit):\0A\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [36 x i8] c"\0AString echoed by the server: \22%s\22\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (...) @initscr()
%6 = tail call i32 (...) @cbreak()
%7 = tail call i32 (...) @noecho()
%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 slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @errno
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 17231)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%23 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @errno
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %16
%30 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%31 = call i64 @connect(i32 %10, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %35, %33
br label %35
36: ; preds = %29
%37 = load i32, i32* %22
%38 = call i32 @inet_ntoa(i32 %37)
%39 = load i32, i32* %21
%40 = call i32 @ntohs(i32 %39)
%41 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %40)
br label %42
42: ; preds = %52, %36
%43 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %47, %42
%45 = phi i64 [ %49, %47 ], [ 0, %42 ]
%46 = call signext i8 (...) @getch()
switch i8 %46, label %47 [
i8 27, label %59
i8 10, label %52
]
47: ; preds = %44
%48 = sext i8 %46 to i32
%49 = add nuw i64 %45, 1
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %45
store i8 %46, i8* %50
%51 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %48)
br label %44
52: ; preds = %44
%53 = trunc i64 %45 to i32
%54 = call i32 @send(i32 %10, i8* %4, i32 %53, i32 0)
%55 = call i32 @recv(i32 %10, i8* %4, i32 1024, i32 0)
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %56
store i8 0, i8* %57
%58 = call i32 (i8*, ...) @printw(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %4)
br label %42
59: ; preds = %44
%60 = call i32 @close(i32 %10)
%61 = call i32 (...) @endwin()
%62 = call i32 @exit(i32 0)
unreachable
}