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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char const *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int /*<<< orphan*/, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
int main(int argc, char const *argv[]) {
  int retour = 0;
  struct sockaddr_in adress_sock;
  struct addrinfo *first_info;
  struct addrinfo hints;
  bzero(&hints, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  retour = getaddrinfo("localhost", argv[1], &hints, &first_info);
  if (retour != 0) {
    printf("%s\n", gai_strerror(retour));
    exit(1);
  }
  if (first_info != NULL) {
    adress_sock = *(struct sockaddr_in *)first_info->ai_addr;
  }
  int desc_sock = socket(PF_INET, SOCK_STREAM, 0);
  retour = connect(desc_sock, (struct sockaddr *)&adress_sock,
                   sizeof(struct sockaddr_in));
  if (retour == -1) {
    perror(strerror(errno));
    exit(1);
  }
  char buffer[1024];
  char mess[1024];
  int rc;
  int wc;
  do {
    wc = write(1, "$", 1);
    rc = read(0, buffer, 1023);
    buffer[rc] = 0;
    retour = send(desc_sock, buffer, strlen(buffer), 0);
    if (retour == -1) {
      perror(strerror(errno));
      exit(1);
    }
    int recu = recv(desc_sock, mess, 1023 * sizeof(char), 0);
    mess[recu] = '\0';
    printf("%s\n", mess);
  } while (strncmp(buffer, "quit", 4) != 0);
  close(desc_sock);
  freeaddrinfo(first_info);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
call   10d0 <bzero@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd9b(%rip),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1100 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12a0 <main+0x90>
mov    -0x14(%rbp),%edi
call   1090 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xd79(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     12b7 <main+0xa7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10f0 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1310 <main+0x100>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %eax,%edi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    1315 <main+0x105>
mov    $0x1,%edx
lea    0xced(%rip),%rsi
mov    %edx,%edi
call   1050 <write@plt>
mov    %eax,-0x848(%rbp)
lea    -0x440(%rbp),%rsi
xor    %edi,%edi
mov    $0x3ff,%edx
call   10b0 <read@plt>
mov    %eax,-0x844(%rbp)
movslq -0x844(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
mov    -0x34(%rbp),%eax
mov    %eax,-0x85c(%rbp)
lea    -0x440(%rbp),%rax
mov    %rax,-0x858(%rbp)
lea    -0x440(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x85c(%rbp),%edi
mov    -0x858(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    13bb <main+0x1ab>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %eax,%edi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x34(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x84c(%rbp)
movslq -0x84c(%rbp),%rax
movb   $0x0,-0x840(%rbp,%rax,1)
lea    -0x840(%rbp),%rsi
lea    0xc16(%rip),%rdi
call   1080 <printf@plt>
lea    -0x440(%rbp),%rdi
lea    0xc09(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1315 <main+0x105>
mov    -0x34(%rbp),%edi
call   10a0 <close@plt>
mov    -0x20(%rbp),%rdi
call   1120 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rsi,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x10,%esi
call   10d0 <bzero@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2d8e(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rbx),%rsi
lea    0xda4(%rip),%rdi
lea    0x8(%rsp),%rcx
mov    %rbp,%rdx
call   1100 <getaddrinfo@plt>
test   %eax,%eax
jne    13a2 <main+0x192>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1283 <main+0x73>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
je     1383 <main+0x173>
lea    0xd57(%rip),%r14
lea    0x420(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xd3f(%rip),%r12
lea    0xd3e(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   10b0 <read@plt>
cltq
movb   $0x0,0x420(%rsp,%rax,1)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     1383 <main+0x173>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <printf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    12e0 <main+0xd0>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    0x8(%rsp),%rdi
call   1120 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %eax,%edi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%edi
call   1090 <gai_strerror@plt>
lea    0xc5a(%rip),%rdi
mov    %rax,%rsi
call   1080 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@PF_INET = global i32 0
@errno = global i32 0
@.str.2 = [2 x i8] c"$\00"
@.str.3 = [5 x i8] c"quit\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca [1024 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%16 = call i32 @bzero(%struct.addrinfo* %9, i32 16)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %23, %struct.addrinfo* %9, %struct.addrinfo** %8)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %2
%28 = load i32, i32* %6
%29 = call i8* @gai_strerror(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %2
%33 = load %struct.addrinfo*, %struct.addrinfo** %8
%34 = icmp ne %struct.addrinfo* %33, null
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 0
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to %struct.sockaddr_in*
%40 = bitcast %struct.sockaddr_in* %7 to i8*
%41 = bitcast %struct.sockaddr_in* %39 to i8*
br label %42
42: ; preds = %35, %32
%43 = load i32, i32* @PF_INET
%44 = load i32, i32* @SOCK_STREAM
%45 = call i32 @socket(i32 %43, i32 %44, i32 0)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%48 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i32 4)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %42
%52 = load i32, i32* @errno
%53 = call i32 @strerror(i32 %52)
%54 = call i32 @perror(i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %42
br label %57
57: ; preds = %85, %56
%58 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 %58, i32* %14
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%60 = call i32 @read(i32 0, i8* %59, i32 1023)
store i32 %60, i32* %13
%61 = load i32, i32* %13
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %10
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%67 = call i32 @strlen(i8* %66)
%68 = call i32 @send(i32 %64, i8* %65, i32 %67, i32 0)
store i32 %68, i32* %6
%69 = load i32, i32* %6
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %76
71: ; preds = %57
%72 = load i32, i32* @errno
%73 = call i32 @strerror(i32 %72)
%74 = call i32 @perror(i32 %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %57
%77 = load i32, i32* %10
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%79 = call i32 @recv(i32 %77, i8* %78, i32 1023, i32 0)
store i32 %79, i32* %15
%80 = load i32, i32* %15
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %81
store i8 0, i8* %82
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%84 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %83)
br label %85
85: ; preds = %76
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%87 = call i64 @strncmp(i8* %86, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%88 = icmp ne i64 %87, 0
br i1 %88, label %57, label %89
89: ; preds = %85
%90 = load i32, i32* %10
%91 = call i32 @close(i32 %90)
%92 = load %struct.addrinfo*, %struct.addrinfo** %8
%93 = call i32 @freeaddrinfo(%struct.addrinfo* %92)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@PF_INET = global i32 0
@errno = global i32 0
@.str.2 = [2 x i8] c"$\00"
@.str.3 = [5 x i8] c"quit\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.addrinfo** %4 to i8*
%10 = bitcast %struct.addrinfo* %5 to i8*
%11 = call i32 @bzero(%struct.addrinfo* %5, i32 16)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %17, %struct.addrinfo* %5, %struct.addrinfo** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %2
%21 = call i8* @gai_strerror(i32 %18)
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to i32*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%32 = load i32, i32* %30
store i32 %32, i32* %31
br label %33
33: ; preds = %27, %24
%34 = load i32, i32* @PF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i32 @connect(i32 %36, %struct.sockaddr* %37, i32 4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 @perror(i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %33
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
br label %48
48: ; preds = %61, %45
%49 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%50 = call i32 @read(i32 0, i8* %46, i32 1023)
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %51
store i8 0, i8* %52
%53 = call i32 @strlen(i8* %46)
%54 = call i32 @send(i32 %36, i8* %46, i32 %53, i32 0)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %61
56: ; preds = %48
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 @perror(i32 %58)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %48
%62 = call i32 @recv(i32 %36, i8* %47, i32 1023, i32 0)
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %63
store i8 0, i8* %64
%65 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%66 = call i64 @strncmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %48
68: ; preds = %61
%69 = call i32 @close(i32 %36)
%70 = load %struct.addrinfo*, %struct.addrinfo** %4
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
ret i32 0
}