#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_addr;
  int /*<<< orphan*/ sin_port;
  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*/ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_line(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write_line(int, char *, int /*<<< orphan*/);
void connect_to_server(int port) {
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  inet_aton("127.0.0.1", &server_addr.sin_addr);
  int client_fd = socket(AF_INET, SOCK_STREAM, 0);
  connect(client_fd, (const struct sockaddr *)&server_addr,
          sizeof(server_addr));
  printf("String to echo: ");
  char input[64];
  fgets(input, 64, stdin);
  write_line(client_fd, input, strlen(input));
  char reply[64];
  read_line(client_fd, reply, 64);
  printf("Server replied: %s\n", reply);
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1080 <inet_aton@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    $0x40,%esi
call   1070 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x60(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
call   10b0 <write_line@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x40,%edx
call   1090 <read_line@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e21(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe20(%rip),%rdi
mov    %r14,%rsi
call   1080 <inet_aton@plt>
mov    (%r15),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
lea    0xdf7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0x50(%rsp),%r14
mov    %r14,%rdi
mov    $0x40,%esi
call   1070 <fgets@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   10b0 <write_line@plt>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   1090 <read_line@plt>
lea    0xdb7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, 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
@.str.1 = [17 x i8] c"String to echo: \00"
@stdin = global i32 0
@.str.2 = [20 x i8] c"Server replied: %s\0A\00"
define void @connect_to_server(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca [64 x i8]
store i32 %0, i32* %2
%7 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* %2
%11 = call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%14 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %13)
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i32 @connect(i32 %18, %struct.sockaddr* %19, i32 12)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* @stdin
%24 = call i32 @fgets(i8* %22, i32 64, i32 %23)
%25 = load i32, i32* %4
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @write_line(i32 %25, i8* %26, i32 %28)
%30 = load i32, i32* %4
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%32 = call i32 @read_line(i32 %30, i8* %31, i32 64)
%33 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %33)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, 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
@.str.1 = [17 x i8] c"String to echo: \00"
@stdin = global i32 0
@.str.2 = [20 x i8] c"Server replied: %s\0A\00"
define void @connect_to_server(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = call i32 @htons(i32 %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%12 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%17 = call i32 @connect(i32 %15, %struct.sockaddr* %16, i32 12)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* @stdin
%21 = call i32 @fgets(i8* %19, i32 64, i32 %20)
%22 = call i32 @strlen(i8* %19)
%23 = call i32 @write_line(i32 %15, i8* %19, i32 %22)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%25 = call i32 @read_line(i32 %15, i8* %24, i32 64)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %24)
ret void
}