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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, scalar_t__);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  char buf[256];
  struct sockaddr_in sock_addr;
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  sock_addr.sin_port = htons(31337);
  int connect_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (connect(connect_sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) <
      0)
    exit(1);
  while (1) {
    fgets(buf, sizeof(buf), stdin);
    write(connect_sock, buf, strlen(buf) + 1);
    read(connect_sock, buf, strlen(buf) + 1);
    printf("%s\n", buf);
  }
  close(connect_sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x118(%rbp)
lea    0xe38(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x11c(%rbp)
mov    $0x7a69,%edi
call   1050 <htons@plt>
mov    %eax,-0x120(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1234 <main+0x94>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1239 <main+0x99>
lea    -0x110(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    -0x124(%rbp),%eax
mov    %eax,-0x154(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x154(%rbp),%edi
mov    -0x150(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x148(%rbp)
call   1030 <write@plt>
mov    -0x124(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x130(%rbp)
call   1070 <read@plt>
lea    -0x110(%rbp),%rsi
lea    0xd1d(%rip),%rdi
call   1060 <printf@plt>
jmp    1239 <main+0x99>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    0x2e34(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xe3f(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x7a69,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     1269 <main+0xc9>
mov    0x2dd3(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0xdf1(%rip),%r14
nopl   0x0(%rax)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
jmp    1220 <main+0x80>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stdin = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %5, i32* %6
%7 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 0
store i32 %7, i32* %9
%10 = call i32 @htons(i32 31337)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i64 @connect(i32 %16, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %0
br label %23
23: ; preds = %22, %23
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%25 = load i32, i32* @stdin
%26 = call i32 @fgets(i8* %24, i32 256, i32 %25)
%27 = load i32, i32* %4
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%30 = call i64 @strlen(i8* %29)
%31 = add nsw i64 %30, 1
%32 = call i32 @write(i32 %27, i8* %28, i64 %31)
%33 = load i32, i32* %4
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%36 = call i64 @strlen(i8* %35)
%37 = add nsw i64 %36, 1
%38 = call i32 @read(i32 %33, i8* %34, i64 %37)
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%40 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stdin = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 31337)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i64 @connect(i32 %14, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0, %20
%21 = load i32, i32* @stdin
%22 = call i32 @fgets(i8* %3, i32 256, i32 %21)
%23 = call i64 @strlen(i8* %3)
%24 = add nsw i64 %23, 1
%25 = call i32 @write(i32 %14, i8* %3, i64 %24)
%26 = call i64 @strlen(i8* %3)
%27 = add nsw i64 %26, 1
%28 = call i32 @read(i32 %14, i8* %3, i64 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %20
}