#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_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Connect(int, struct sockaddr_in *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ Servaddr(struct sockaddr_in *, char *, char *);
int Socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd, n;
  char recvline[4096 + 1];
  char sendline[4096 + 1];
  char error[4096 + 1];
  struct sockaddr_in servaddr;
  socklen_t addrlen = sizeof(servaddr);
  if (argc != 3) {
    strcpy(error, "uso: ");
    strcat(error, argv[0]);
    strcat(error, " <IPaddress> <Port>");
    perror(error);
    exit(1);
  }
  sockfd = Socket(AF_INET, SOCK_STREAM, 0);
  Servaddr(&servaddr, argv[1], argv[2]);
  Connect(sockfd, &servaddr);
  getsockname(sockfd, (struct sockaddr *)&servaddr, &addrlen);
  printf("Endereço local: %s:%u \n", inet_ntoa(servaddr.sin_addr),
         ntohs(servaddr.sin_port));
  getpeername(sockfd, (struct sockaddr *)&servaddr, &addrlen);
  printf("Endereço do host: %s:%u \n", inet_ntoa(servaddr.sin_addr),
         ntohs(servaddr.sin_port));
  for (;;) {
    fgets(sendline, 4096 - 1, stdin);
    write(sockfd, sendline, strlen(sendline));
    if (strcmp(sendline, "bye\n") == 0) {
      printf("Conexao encerrada.\n");
      break;
    }
    n = read(sockfd, recvline, 4096);
    recvline[n] = 0;
    printf("%s", recvline);
    if (n < 0) {
      perror("read error");
      exit(1);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3070,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x8,-0x304c(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     128c <main+0x7c>
lea    -0x3040(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x3040(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1120 <strcat@plt>
lea    -0x3040(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   1120 <strcat@plt>
lea    -0x3040(%rbp),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <Socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    -0x3048(%rbp),%rdi
call   1060 <Servaddr@plt>
mov    -0x14(%rbp),%edi
lea    -0x3048(%rbp),%rsi
call   1110 <Connect@plt>
mov    -0x14(%rbp),%edi
lea    -0x3048(%rbp),%rsi
lea    -0x304c(%rbp),%rdx
call   1100 <getsockname@plt>
mov    -0x3044(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x3060(%rbp)
mov    -0x3048(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0x3060(%rbp),%rsi
mov    %eax,%edx
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x3048(%rbp),%rsi
lea    -0x304c(%rbp),%rdx
call   1070 <getpeername@plt>
mov    -0x3044(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x3058(%rbp)
mov    -0x3048(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0x3058(%rbp),%rsi
mov    %eax,%edx
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x2030(%rbp),%rdi
mov    0x2c6b(%rip),%rax
mov    (%rax),%edx
mov    $0xfff,%esi
call   10b0 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x306c(%rbp)
lea    -0x2030(%rbp),%rax
mov    %rax,-0x3068(%rbp)
lea    -0x2030(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x306c(%rbp),%edi
mov    -0x3068(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    -0x2030(%rbp),%rdi
lea    0xc88(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    13e8 <main+0x1d8>
lea    0xc77(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1445 <main+0x235>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rsi
lea    0xc4e(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    1440 <main+0x230>
lea    0xc39(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
jmp    1367 <main+0x157>
xor    %edi,%edi
call   1130 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
mov    %rsi,%rbx
movl   $0x8,0xc(%rsp)
cmp    $0x3,%edi
jne    137d <main+0x15d>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <Socket@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
mov    0x10(%rbx),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
call   1070 <Servaddr@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1120 <Connect@plt>
lea    0xc(%rsp),%r14
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1110 <getsockname@plt>
mov    0x4(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%rsp),%edi
call   10e0 <ntohs@plt>
lea    0xd70(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <getpeername@plt>
mov    0x4(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%rsp),%edi
call   10e0 <ntohs@plt>
lea    0xd55(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0x1020(%rsp),%rbx
lea    0xd55(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xd4e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2cc9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xfff,%esi
call   10c0 <fgets@plt>
mov    %rbx,%rdi
call   1090 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     13bd <main+0x19d>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
movslq %eax,%r13
movb   $0x0,0x10(%rsp,%r13,1)
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
test   %r13d,%r13d
jns    1310 <main+0xf0>
lea    0xcdb(%rip),%rdi
jmp    13ae <main+0x18e>
lea    0xc7c(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    (%rbx),%rsi
mov    %rbp,%rdi
call   1130 <strcat@plt>
lea    0xc63(%rip),%rsi
mov    %rbp,%rdi
call   1130 <strcat@plt>
mov    %rbp,%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xc9d(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1140 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c"uso: \00"
@.str.1 = [20 x i8] c" <IPaddress> <Port>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [25 x i8] c"Endere\C3\A7o local: %s:%u \0A\00"
@.str.3 = [27 x i8] c"Endere\C3\A7o do host: %s:%u \0A\00"
@stdin = global i32 0
@.str.4 = [5 x i8] c"bye\0A\00"
@.str.5 = [20 x i8] c"Conexao encerrada.\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [11 x i8] c"read error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4097 x i8]
%9 = alloca [4097 x i8]
%10 = alloca [4097 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 8, i32* %12
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %28
15: ; preds = %2
%16 = getelementptr inbounds [4097 x i8], [4097 x i8]* %10, i64 0, i64 0
%17 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%18 = getelementptr inbounds [4097 x i8], [4097 x i8]* %10, i64 0, i64 0
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @strcat(i8* %18, i8* %21)
%23 = getelementptr inbounds [4097 x i8], [4097 x i8]* %10, i64 0, i64 0
%24 = call i32 @strcat(i8* %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds [4097 x i8], [4097 x i8]* %10, i64 0, i64 0
%26 = call i32 @perror(i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @Socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %6
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 @Servaddr(%struct.sockaddr_in* %11, i8* %34, i8* %37)
%39 = load i32, i32* %6
%40 = call i32 @Connect(i32 %39, %struct.sockaddr_in* %11)
%41 = load i32, i32* %6
%42 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%43 = call i32 @getsockname(i32 %41, %struct.sockaddr* %42, i32* %12)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i8* @inet_ntoa(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @ntohs(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %46, i32 %49)
%51 = load i32, i32* %6
%52 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%53 = call i32 @getpeername(i32 %51, %struct.sockaddr* %52, i32* %12)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%55 = load i32, i32* %54
%56 = call i8* @inet_ntoa(i32 %55)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @ntohs(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %56, i32 %59)
br label %61
61: ; preds = %89, %28
%62 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%63 = load i32, i32* @stdin
%64 = call i32 @fgets(i8* %62, i32 4095, i32 %63)
%65 = load i32, i32* %6
%66 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%67 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%68 = call i32 @strlen(i8* %67)
%69 = call i32 @write(i32 %65, i8* %66, i32 %68)
%70 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%71 = call i64 @strcmp(i8* %70, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %61
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %90
75: ; preds = %61
%76 = load i32, i32* %6
%77 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%78 = call i32 @read(i32 %76, i8* %77, i32 4096)
store i32 %78, i32* %7
%79 = load i32, i32* %7
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 %80
store i8 0, i8* %81
%82 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %82)
%84 = load i32, i32* %7
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %75
%87 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %75
br label %61
90: ; preds = %73
%91 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c"uso: \00"
@.str.1 = [20 x i8] c" <IPaddress> <Port>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [25 x i8] c"Endere\C3\A7o local: %s:%u \0A\00"
@.str.3 = [27 x i8] c"Endere\C3\A7o do host: %s:%u \0A\00"
@stdin = global i32 0
@.str.4 = [5 x i8] c"bye\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [11 x i8] c"read error\00"
@str = [19 x i8] c"Conexao encerrada.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4097 x i8]
%4 = alloca [4097 x i8]
%5 = alloca [4097 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 0
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = bitcast i32* %7 to i8*
store i32 8, i32* %7
%13 = icmp eq i32 %0, 3
br i1 %13, label %21, label %14
14: ; preds = %2
%15 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %1
%17 = call i32 @strcat(i8* %10, i8* %16)
%18 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @perror(i8* %10)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = tail call i32 @Socket(i32 %22, i32 %23, i32 0)
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @Servaddr(%struct.sockaddr_in* %6, i8* %26, i8* %28)
%30 = call i32 @Connect(i32 %24, %struct.sockaddr_in* %6)
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i32 @getsockname(i32 %24, %struct.sockaddr* %31, i32* %7)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%34 = load i32, i32* %33
%35 = call i8* @inet_ntoa(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @ntohs(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 %38)
%40 = call i32 @getpeername(i32 %24, %struct.sockaddr* %31, i32* %7)
%41 = load i32, i32* %33
%42 = call i8* @inet_ntoa(i32 %41)
%43 = load i32, i32* %36
%44 = call i32 @ntohs(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %42, i32 %44)
br label %46
46: ; preds = %56, %21
%47 = load i32, i32* @stdin
%48 = call i32 @fgets(i8* %9, i32 4095, i32 %47)
%49 = call i32 @strlen(i8* %9)
%50 = call i32 @write(i32 %24, i8* %9, i32 %49)
%51 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %46
%57 = call i32 @read(i32 %24, i8* %8, i32 4096)
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 %58
store i8 0, i8* %59
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%61 = icmp slt i32 %57, 0
br i1 %61, label %62, label %46
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
}