#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sprintf(char *, char *);
int write(int, char *, int);
int main(void) {
  struct addrinfo hints;
  struct addrinfo *server_address = NULL;
  char buffer[512] = {0};
  int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    perror("socket");
    exit(1);
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  getaddrinfo("localhost", "1234", &hints, &server_address);
  connect(socket_fd, server_address->ai_addr, server_address->ai_addrlen);
  if (errno != 0) {
    perror("connect");
    exit(2);
  }
  int message_length = sprintf(buffer, "hello server");
  message_length = write(socket_fd, buffer, message_length);
  if (message_length < 0) {
    perror("write");
    exit(3);
  }
  message_length = read(socket_fd, buffer, 512 - 1);
  if (message_length < 0) {
    perror("read");
    exit(4);
  }
  printf("from server: %s\n", buffer);
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x224(%rbp)
jge    121f <main+0x6f>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xdb9(%rip),%rdi
lea    0xdbc(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    -0x224(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
mov    0x2d4f(%rip),%rax
cmpq   $0x0,(%rax)
je     12a1 <main+0xf1>
lea    0xd84(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    -0x220(%rbp),%rdi
lea    0xd6f(%rip),%rsi
call   1090 <sprintf@plt>
mov    %eax,-0x228(%rbp)
mov    -0x224(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x228(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    12fb <main+0x14b>
lea    0xd3f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x224(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x1ff,%edx
call   1070 <read@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    133b <main+0x18b>
lea    0xd05(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
lea    -0x220(%rbp),%rsi
lea    0xced(%rip),%rdi
call   1040 <printf@plt>
mov    -0x224(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x228,%rsp
movq   $0x0,0x8(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e19(%rip),%r15
mov    (%r15),%edi
mov    0x2dff(%rip),%r12
mov    (%r12),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12bf <main+0x11f>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x1c(%rsp)
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
lea    0xdef(%rip),%rdi
lea    0xdf2(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdx
call   10b0 <getaddrinfo@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   10a0 <connect@plt>
mov    0x2d8c(%rip),%rax
cmpq   $0x0,(%rax)
jne    12d5 <main+0x135>
movabs $0x72657672657320,%rax
mov    %rax,0x25(%rsp)
movabs $0x6573206f6c6c6568,%rax
20 73 65
mov    %rax,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1030 <write@plt>
test   %eax,%eax
js     12eb <main+0x14b>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1ff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1301 <main+0x161>
lea    0xd99(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xd39(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
lea    0xd29(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [5 x i8] c"1234\00"
@errno = global i64 0
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [13 x i8] c"hello server\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [17 x i8] c"from server: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store %struct.addrinfo* null, %struct.addrinfo** %3
%7 = bitcast [512 x i8]* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %18, i32* %19
%20 = load i32, i32* @SOCK_STREAM
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%23 = load i32, i32* %5
%24 = load %struct.addrinfo*, %struct.addrinfo** %3
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = load %struct.addrinfo*, %struct.addrinfo** %3
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @connect(i32 %23, i32 %26, i32 %29)
%31 = load i64, i64* @errno
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %16
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %16
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%38 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 %38, i32* %6
%39 = load i32, i32* %5
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%41 = load i32, i32* %6
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 3)
unreachable
48: ; preds = %36
%49 = load i32, i32* %5
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 511)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 @exit(i32 4)
unreachable
57: ; preds = %48
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%59 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [5 x i8] c"1234\00"
@errno = global i64 0
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [13 x i8] c"hello server\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [17 x i8] c"from server: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = alloca [512 x i8]
%4 = bitcast %struct.addrinfo* %1 to i8*
%5 = bitcast %struct.addrinfo** %2 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %2
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 16)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%21 = load %struct.addrinfo*, %struct.addrinfo** %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @connect(i32 %9, i32 %23, i32 %25)
%27 = load i64, i64* @errno
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %14
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %14
%33 = call i32 @write(i32 %9, i8* %6, i32 12)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @exit(i32 3)
unreachable
38: ; preds = %32
%39 = call i32 @read(i32 %9, i8* %6, i32 511)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 @exit(i32 4)
unreachable
44: ; preds = %38
%45 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%46 = call i32 @close(i32 %9)
ret i32 0
}