#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ STDIN_FILENO;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  int ret = -1;
  int sock_fd = -1;
  struct sockaddr_in server_addr;
  struct sockaddr_in client_addr;
  socklen_t addr_len = 0;
  sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_fd < 0) {
    printf("error to sock_fd\n");
  }
  memset(&server_addr, 0, sizeof(server_addr));
  memset(&client_addr, 0, sizeof(client_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8888);
  server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  char buf[1024];
  int recv_len = 0;
  int send_len = 0;
  addr_len = sizeof(server_addr);
  ret = connect(sock_fd, (struct sockaddr *)&server_addr, addr_len);
  if (ret < 0) {
    printf("error to accept\n");
  }
  while (1) {
    recv_len = read(STDIN_FILENO, buf, sizeof(buf) - 1);
    if (recv_len < 0) {
      printf("error to read\n");
      exit(0);
    }
    send_len = write(sock_fd, buf, recv_len);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f0 <main+0x60>
lea    0xe15(%rip),%rdi
call   1050 <printf@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    0xde2(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x444(%rbp)
movl   $0x0,-0x448(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   10a0 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    127b <main+0xeb>
lea    0xda6(%rip),%rdi
call   1050 <printf@plt>
jmp    1280 <main+0xf0>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    -0x440(%rbp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jge    12c0 <main+0x130>
lea    0xd79(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x448(%rbp)
jmp    1280 <main+0xf0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    0x2e46(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c6 <main+0x36>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xdfc(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
jns    122b <main+0x9b>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d9e(%rip),%r14
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1270 <main+0xe0>
lea    0x20(%rsp),%rbx
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    1250 <main+0xc0>
lea    0xd93(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [18 x i8] c"error to sock_fd\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [17 x i8] c"error to accept\0A\00"
@STDIN_FILENO = global i32 0
@.str.3 = [15 x i8] c"error to read\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %10
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%23 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 8888)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
store i32 0, i32* %12
store i32 0, i32* %13
store i32 12, i32* %10
%31 = load i32, i32* %7
%32 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%33 = load i32, i32* %10
%34 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 %33)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %21
%38 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %21
br label %40
40: ; preds = %39, %49
%41 = load i32, i32* @STDIN_FILENO
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 1023)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %40
%50 = load i32, i32* %7
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%52 = load i32, i32* %12
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
store i32 %53, i32* %13
br label %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@STDIN_FILENO = global i32 0
@str = [14 x i8] c"error to read\00"
@str.4 = [16 x i8] c"error to accept\00"
@str.5 = [17 x i8] c"error to sock_fd\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%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 %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%16 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 8888)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %19, i32* %20
%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 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %10, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %14
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @read(i32 %30, i8* %23, i32 1023)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %36, %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %29, %36
%37 = phi i32 [ %40, %36 ], [ %31, %29 ]
%38 = call i32 @write(i32 %10, i8* %23, i32 %37)
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @read(i32 %39, i8* %23, i32 1023)
%41 = icmp slt i32 %40, 0
br i1 %41, label %33, label %36
}