#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 sin_port;
  int /*<<< orphan*/ sin_addr;
  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*/ close(int const);
int /*<<< orphan*/ connect(int const, struct sockaddr *, int);
int /*<<< orphan*/ getchar();
int htons(int);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  const int client_sock = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &(server_addr.sin_addr));
  server_addr.sin_port = htons(20000);
  connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
  printf("Connection established; server %s:%i\n",
         inet_ntoa(server_addr.sin_addr), server_addr.sin_port);
  getchar();
  close(client_sock);
  printf("Connection closed.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rdx
add    $0x4,%rdx
lea    0xe18(%rip),%rsi
call   1090 <inet_pton@plt>
mov    $0x4e20,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    -0xc(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
mov    -0x10(%rbp),%edx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e38(%rip),%r15
mov    (%r15),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
lea    0xe16(%rip),%rsi
call   10a0 <inet_pton@plt>
mov    $0x4e20,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    0x4(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    (%rsp),%edx
lea    0xdec(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xdf3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%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
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [38 x i8] c"Connection established; server %s:%i\0A\00"
@.str.2 = [20 x i8] c"Connection closed.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = call i32 @socket(i32 %3, i32 %4, i32 0)
store i32 %5, i32* %1
%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, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%11 = call i32 @inet_pton(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %10)
%12 = call i32 @htons(i32 20000)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %1
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i32 @connect(i32 %14, %struct.sockaddr* %15, i32 12)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i8* @inet_ntoa(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %21)
%23 = call i32 (...) @getchar()
%24 = load i32, i32* %1
%25 = call i32 @close(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [38 x i8] c"Connection established; server %s:%i\0A\00"
@str = [19 x i8] c"Connection closed.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%10 = call i32 @inet_pton(i32 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %9)
%11 = call i32 @htons(i32 20000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %11, i32* %12
%13 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%14 = call i32 @connect(i32 %4, %struct.sockaddr* %13, i32 12)
%15 = load i32, i32* %9
%16 = call i8* @inet_ntoa(i32 %15)
%17 = load i32, i32* %12
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %16, i32 %17)
%19 = call i32 (...) @getchar()
%20 = call i32 @close(i32 %4)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
ret i32 0
}