#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*/ remote_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
unsigned int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, char *);
unsigned int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  int client_sockfd;
  unsigned int len;
  struct sockaddr_in remote_addr;
  char buf[BUFSIZ];
  memset(&remote_addr, 0, sizeof(remote_addr));
  remote_addr.sin_family = AF_INET;
  char ip_address[] = "127.0.0.1";
  remote_addr.sin_addr.s_addr = inet_addr(ip_address);
  remote_addr.sin_port = htons(8000);
  if ((client_sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return 1;
  }
  if (connect(client_sockfd, (struct sockaddr *)&remote_addr,
              sizeof(struct sockaddr)) < 0) {
    perror("connect");
    return 1;
  }
  printf("connected to server\n");
  while (1) {
    scanf("%s", buf);
    len = send(client_sockfd, buf, strlen(buf), 0);
    len = recv(client_sockfd, buf, BUFSIZ, 0);
    buf[len] = '\0';
    printf("Received:%s\n", buf);
  }
  close(client_sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0xdf2(%rip),%rax
mov    %rax,-0x32(%rbp)
mov    0xdef(%rip),%ax
mov    %ax,-0x2a(%rbp)
lea    -0x32(%rbp),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    127a <main+0xca>
lea    0xda8(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1331 <main+0x181>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12b4 <main+0x104>
lea    0xd75(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1331 <main+0x181>
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12c7 <main+0x117>
mov    -0x40(%rbp),%rsi
lea    0xd5c(%rip),%rdi
call   10a0 <scanf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cc9(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
movb   $0x0,(%rsi,%rax,1)
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12c2 <main+0x112>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsp,%r15
mov    0x2df5(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movabs $0x2e302e302e373231,%rax
2e 30 2e
mov    %rax,-0x48(%rbp)
movw   $0x31,-0x40(%rbp)
lea    -0x48(%rbp),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x34(%rbp)
mov    $0x1f40,%edi
call   1060 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12d8 <main+0x118>
mov    %eax,%r14d
lea    -0x38(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     12e1 <main+0x121>
lea    0xdb0(%rip),%rdi
call   1040 <puts@plt>
lea    0xd94(%rip),%r15
lea    0xd90(%rip),%r12
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <scanf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    0x0(%r13),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1290 <main+0xd0>
lea    0xd2b(%rip),%rdi
jmp    12e8 <main+0x128>
lea    0xd29(%rip),%rdi
call   10c0 <perror@plt>
mov    %r15,%rsp
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@__const.main.ip_address = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [21 x i8] c"connected to server\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [13 x i8] c"Received:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [10 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %14, i32* %15
%16 = bitcast [10 x i8]* %7 to i8*
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%18 = call i32 @inet_addr(i8* %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = call i32 @htons(i32 8000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %0
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %8
br label %51
29: ; preds = %0
%30 = load i32, i32* %2
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %8
br label %51
36: ; preds = %29
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %39, %36
br label %39
39: ; preds = %38
%40 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%41 = load i32, i32* %2
%42 = call i32 @strlen(i8* %12)
%43 = call i32 @send(i32 %41, i8* %12, i32 %42, i32 0)
store i32 %43, i32* %3
%44 = load i32, i32* %2
%45 = load i32, i32* @BUFSIZ
%46 = call i32 @recv(i32 %44, i8* %12, i32 %45, i32 0)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %12, i64 %48
store i8 0, i8* %49
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %12)
br label %38
51: ; preds = %34, %27
%52 = load i8*, i8** %5
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@__const.main.ip_address = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [13 x i8] c"Received:%s\0A\00"
@str = [20 x i8] c"connected to server\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [10 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%12 = call i32 @inet_addr(i8* %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = call i32 @htons(i32 8000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %0
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @connect(i32 %18, %struct.sockaddr* %21, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %35, label %24
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %26, %24
%27 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%28 = call i32 @strlen(i8* %7)
%29 = call i32 @send(i32 %18, i8* %7, i32 %28, i32 0)
%30 = load i32, i32* @BUFSIZ
%31 = call i32 @recv(i32 %18, i8* %7, i32 %30, i32 0)
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %7, i64 %32
store i8 0, i8* %33
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %7)
br label %26
35: ; preds = %20, %0
%36 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %20 ]
%37 = call i32 @perror(i8* %36)
ret i32 1
}