#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char **, int);
int /*<<< orphan*/ snprintf(char *, int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int sock;
  struct sockaddr_in conn;
  struct hostent host;
  char buf[512 + 1];
  sock = socket(PF_INET, SOCK_STREAM, 0);
  host = *gethostbyname("localhost");
  conn.sin_family = AF_INET;
  conn.sin_port = htons(1100);
  conn.sin_addr = *((struct in_addr *)host.h_addr);
  memset(&(conn.sin_zero), 0, 8);
  connect(sock, (struct sockaddr *)&conn, sizeof(struct sockaddr));
  snprintf(buf, 512, "data from client\n");
  printf("sending to server: %s", buf);
  write(sock, buf, strlen(buf));
  read(sock, &buf, 512);
  printf("got from server: %s", buf);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    0xe1b(%rip),%rdi
call   10b0 <gethostbyname@plt>
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x44c,%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
lea    -0x230(%rbp),%rdi
mov    $0x200,%esi
lea    0xdc3(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x230(%rbp),%rsi
lea    0xdc2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x23c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x23c(%rbp),%edi
mov    -0x238(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
lea    -0x230(%rbp),%rsi
lea    0xd7a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x218,%rsp
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebp
lea    0xe25(%rip),%rdi
call   10b0 <gethostbyname@plt>
mov    (%rax),%rbx
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x44c,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
lea    0xdde(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1070 <snprintf@plt>
lea    0xdd7(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
lea    0xdbb(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@.str.1 = [18 x i8] c"data from client\0A\00"
@.str.2 = [22 x i8] c"sending to server: %s\00"
@.str.3 = [20 x i8] c"got from server: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.hostent
%5 = alloca [513 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = bitcast %struct.hostent* %4 to i8*
%11 = bitcast %struct.hostent* %9 to i8*
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 3
store i32 %12, i32* %13
%14 = call i32 @htons(i32 1100)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i32 0, i32 0
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to %struct.in_addr*
%20 = bitcast %struct.in_addr* %16 to i8*
%21 = bitcast %struct.in_addr* %19 to i8*
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = call i32 @memset(i32* %22, i32 0, i32 8)
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 4)
%27 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%28 = call i32 @snprintf(i8* %27, i32 512, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%29 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %2
%32 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
%36 = load i32, i32* %2
%37 = bitcast [513 x i8]* %5 to i8**
%38 = call i32 @read(i32 %36, i8** %37, i32 512)
%39 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%40 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@.str.1 = [18 x i8] c"data from client\0A\00"
@.str.2 = [22 x i8] c"sending to server: %s\00"
@.str.3 = [20 x i8] c"got from server: %s\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [513 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 1100)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = inttoptr i64 %10 to i32*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%17 = load i32, i32* %15
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%19 = call i32 @memset(i32* %18, i32 0, i32 8)
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @connect(i32 %7, %struct.sockaddr* %20, i32 4)
%22 = call i32 @snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%24 = call i32 @strlen(i8* %4)
%25 = call i32 @write(i32 %7, i8* %4, i32 %24)
%26 = bitcast [513 x i8]* %2 to i8**
%27 = call i32 @read(i32 %7, i8** %26, i32 512)
%28 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%29 = call i32 @close(i32 %7)
ret i32 0
}