#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*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main() {
  int sock, bytes_recieved;
  char send_data[1024], recv_data[1024];
  struct hostent *host;
  struct sockaddr_in server_addr;
  host = gethostbyname("127.0.0.1");
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Socket");
    exit(1);
  }
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(5000);
  server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(server_addr.sin_zero), 8);
  if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("Connect");
    exit(1);
  }
  while (1) {
    bytes_recieved = recv(sock, recv_data, 1024, 0);
    recv_data[bytes_recieved] = '\0';
    if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0) {
      close(sock);
      break;
    } else
      printf("\nRecieved data = %s ", recv_data);
    printf("\nSEND (q or Q to quit) : ");
    gets(send_data);
    if (strcmp(send_data, "q") != 0 && strcmp(send_data, "Q") != 0)
      send(sock, send_data, strlen(send_data), 0);
    else {
      send(sock, send_data, strlen(send_data), 0);
      close(sock);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe07(%rip),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x818(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1240 <main+0x60>
lea    0xdd9(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x81c(%rbp)
mov    $0x1388,%edi
call   1050 <htons@plt>
mov    %eax,-0x820(%rbp)
mov    -0x818(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x824(%rbp)
lea    -0x828(%rbp),%rdi
mov    $0x8,%esi
call   10d0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x828(%rbp),%rsi
mov    $0x4,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0xd5>
lea    0xd6b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12ba <main+0xda>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0x0,-0x810(%rbp,%rax,1)
lea    -0x810(%rbp),%rdi
lea    0xd2c(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1319 <main+0x139>
lea    -0x810(%rbp),%rdi
lea    0xd11(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1326 <main+0x146>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    1418 <main+0x238>
lea    -0x810(%rbp),%rsi
lea    0xce9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x410(%rbp),%rdi
call   10b0 <gets@plt>
lea    -0x410(%rbp),%rdi
lea    0xcb6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     13cd <main+0x1ed>
lea    -0x410(%rbp),%rdi
lea    0xc9b(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     13cd <main+0x1ed>
mov    -0x8(%rbp),%eax
mov    %eax,-0x834(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x834(%rbp),%edi
mov    -0x830(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1413 <main+0x233>
mov    -0x8(%rbp),%eax
mov    %eax,-0x844(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x844(%rbp),%edi
mov    -0x840(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    1418 <main+0x238>
jmp    12ba <main+0xda>
xor    %eax,%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
lea    0xe08(%rip),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,%rbx
mov    0x2de1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
je     13a8 <main+0x1c8>
mov    %eax,%r13d
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    $0x1388,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   10d0 <bzero@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
je     13b1 <main+0x1d1>
lea    0x10(%rsp),%rbx
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd87(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     138c <main+0x1ac>
lea    0xd71(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xd7c(%rip),%r12
lea    0x410(%rsp),%rbp
lea    0xd54(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     138c <main+0x1ac>
lea    0xd32(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   10b0 <gets@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     136d <main+0x18d>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     136d <main+0x18d>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    12d0 <main+0xf0>
jmp    138c <main+0x1ac>
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r13d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc5b(%rip),%rdi
jmp    13b8 <main+0x1d8>
lea    0xc59(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"Socket\00"
@.str.2 = [8 x i8] c"Connect\00"
@.str.3 = [2 x i8] c"q\00"
@.str.4 = [2 x i8] c"Q\00"
@.str.5 = [21 x i8] c"\0ARecieved data = %s \00"
@.str.6 = [26 x i8] c"\0ASEND (q or Q to quit) : \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%8 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %8, %struct.hostent** %6
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 3
store i32 %17, i32* %18
%19 = call i32 @htons(i32 5000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%22 = load %struct.hostent*, %struct.hostent** %6
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to %struct.in_addr*
%26 = bitcast %struct.in_addr* %21 to i8*
%27 = bitcast %struct.in_addr* %25 to i8*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%29 = call i32 @bzero(i32* %28, i32 8)
%30 = load i32, i32* %2
%31 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%32 = call i32 @connect(i32 %30, %struct.sockaddr* %31, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %16
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %16
br label %38
38: ; preds = %37, %83
%39 = load i32, i32* %2
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = call i32 @recv(i32 %39, i8* %40, i32 1024, i32 0)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %38
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48, %38
%53 = load i32, i32* %2
%54 = call i32 @close(i32 %53)
br label %84
55: ; preds = %48
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%61 = call i32 @gets(i8* %60)
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%63 = call i64 @strcmp(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %75
65: ; preds = %58
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%67 = call i64 @strcmp(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %65
%70 = load i32, i32* %2
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%73 = call i32 @strlen(i8* %72)
%74 = call i32 @send(i32 %70, i8* %71, i32 %73, i32 0)
br label %83
75: ; preds = %65, %58
%76 = load i32, i32* %2
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%79 = call i32 @strlen(i8* %78)
%80 = call i32 @send(i32 %76, i8* %77, i32 %79, i32 0)
%81 = load i32, i32* %2
%82 = call i32 @close(i32 %81)
br label %84
83: ; preds = %69
br label %38
84: ; preds = %75, %52
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 }
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"Socket\00"
@.str.2 = [8 x i8] c"Connect\00"
@.str.3 = [2 x i8] c"q\00"
@.str.4 = [2 x i8] c"Q\00"
@.str.5 = [21 x i8] c"\0ARecieved data = %s \00"
@.str.6 = [26 x i8] c"\0ASEND (q or Q to quit) : \00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 5000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i32*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%24 = load i32, i32* %22
store i32 %24, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%26 = call i32 @bzero(i32* %25, i32 8)
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @connect(i32 %10, %struct.sockaddr* %27, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %36, label %30
30: ; preds = %15
%31 = call i32 @recv(i32 %10, i8* %5, i32 1024, i32 0)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %62, label %39
36: ; preds = %15
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30, %51
%40 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %62, label %42
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%45 = call i32 @gets(i8* %4)
%46 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %59, label %48
48: ; preds = %42
%49 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %59, label %51
51: ; preds = %48
%52 = call i32 @strlen(i8* %4)
%53 = call i32 @send(i32 %10, i8* %4, i32 %52, i32 0)
%54 = call i32 @recv(i32 %10, i8* %5, i32 1024, i32 0)
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %55
store i8 0, i8* %56
%57 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %62, label %39
59: ; preds = %48, %42
%60 = call i32 @strlen(i8* %4)
%61 = call i32 @send(i32 %10, i8* %4, i32 %60, i32 0)
br label %62
62: ; preds = %39, %51, %30, %59
%63 = call i32 @close(i32 %10)
ret i32 0
}