#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ echoServAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ DieWithError(char *);
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
unsigned short atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
unsigned int send(int, char *, unsigned int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
unsigned int strlen(char *);
int main(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in echoServAddr;
  unsigned short echoServPort;
  char *serverIP;
  char *echoString;
  char echoBuffer[32];
  unsigned int echoStringLen;
  int bytesRecvd, totalBytesRecvd;
  if ((argc < 3) || (argc > 4)) {
    fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port.]\n",
            argv[0]);
    exit(1);
  }
  serverIP = argv[1];
  echoString = argv[2];
  if (argc == 4) {
    echoServPort = atoi(argv[3]);
  } else {
    echoServPort = 7;
  }
  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0)
    DieWithError("Unable to create socket");
  memset(&echoServAddr, 0, sizeof(echoServAddr));
  echoServAddr.sin_family = AF_INET;
  echoServAddr.sin_addr.s_addr = inet_addr(serverIP);
  echoServAddr.sin_port = htons(echoServPort);
  if (connect(sock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("connect() is failed!");
  echoStringLen = strlen(echoString);
  if (send(sock, echoString, echoStringLen, 0) != echoStringLen) {
    DieWithError("Something is just wrong with the sending of message!");
  }
  totalBytesRecvd = 0;
  printf("Recieved: ");
  while (totalBytesRecvd < echoStringLen) {
    if ((bytesRecvd = recv(sock, echoBuffer, 32 - 1, 0)) <= 0)
      DieWithError("Couldnt get the message from the server!");
    totalBytesRecvd += bytesRecvd;
    echoBuffer[bytesRecvd] = '\0';
    printf(echoBuffer);
  }
  printf("\n");
  close(sock);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     120a <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    1230 <main+0x50>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xddf(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    1268 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10d0 <atoi@plt>
mov    %ax,-0x22(%rbp)
jmp    126e <main+0x8e>
movw   $0x7,-0x22(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a7 <main+0xc7>
lea    0xd8f(%rip),%rdi
call   10c0 <DieWithError@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
movzwl -0x22(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    1302 <main+0x122>
lea    0xd4c(%rip),%rdi
call   10c0 <DieWithError@plt>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x64(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x64(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    -0x64(%rbp),%eax
je     1334 <main+0x154>
lea    0xd2f(%rip),%rdi
call   10c0 <DieWithError@plt>
movl   $0x0,-0x6c(%rbp)
lea    0xd51(%rip),%rdi
call   1070 <printf@plt>
mov    -0x6c(%rbp),%eax
cmp    -0x64(%rbp),%eax
jae    139e <main+0x1be>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x68(%rbp)
cmp    $0x0,%eax
jg     137e <main+0x19e>
lea    0xd25(%rip),%rdi
call   10c0 <DieWithError@plt>
mov    -0x68(%rbp),%eax
add    -0x6c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
movslq -0x68(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rdi
call   1070 <printf@plt>
jmp    1347 <main+0x167>
lea    0xc8a(%rip),%rdi
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     1228 <main+0x38>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xde7(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%r14
mov    $0x7,%bp
cmp    $0x4,%edi
jne    1244 <main+0x54>
mov    0x18(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebp
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1277 <main+0x87>
lea    0xdbf(%rip),%rdi
call   10d0 <DieWithError@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r15,%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
movzwl %bp,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
test   %rax,%rax
jns    12ce <main+0xde>
lea    0xd80(%rip),%rdi
call   10d0 <DieWithError@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %r12d,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    %r15d,%eax
je     12f9 <main+0x109>
lea    0xd6a(%rip),%rdi
call   10d0 <DieWithError@plt>
lea    0xd93(%rip),%rdi
call   1080 <printf@plt>
test   %r15d,%r15d
jne    1323 <main+0x133>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
xor    %ebx,%ebx
lea    0x10(%rsp),%r13
lea    0xd6d(%rip),%r14
jmp    1357 <main+0x167>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %ebp,%ebx
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r13,%rdi
call   1080 <printf@plt>
cmp    %r15d,%ebx
jae    130a <main+0x11a>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1340 <main+0x150>
mov    %r14,%rdi
call   10d0 <DieWithError@plt>
jmp    1340 <main+0x150>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [49 x i8] c"Usage: %s <Server IP> <Echo Word> [<Echo Port.]\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"Unable to create socket\00"
@AF_INET = global i32 0
@.str.2 = [21 x i8] c"connect() is failed!\00"
@.str.3 = [53 x i8] c"Something is just wrong with the sending of message!\00"
@.str.4 = [11 x i8] c"Recieved: \00"
@.str.5 = [41 x i8] c"Couldnt get the message from the server!\00"
@.str.6 = [2 x i8] c"\0A\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 i16
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [32 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 4
br i1 %19, label %20, label %27
20: ; preds = %17, %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %9
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
store i8* %33, i8** %10
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 4
br i1 %35, label %36, label %41
36: ; preds = %27
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 3
%39 = load i8*, i8** %38
%40 = call zeroext i16 @atoi(i8* %39)
store i16 %40, i16* %8
br label %42
41: ; preds = %27
store i16 7, i16* %8
br label %42
42: ; preds = %41, %36
%43 = load i32, i32* @PF_INET
%44 = load i32, i32* @SOCK_STREAM
%45 = load i32, i32* @IPPROTO_TCP
%46 = call i32 @socket(i32 %43, i32 %44, i32 %45)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @DieWithError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
%52 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %53, i32* %54
%55 = load i8*, i8** %9
%56 = call i32 @inet_addr(i8* %55)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 0
store i32 %56, i32* %58
%59 = load i16, i16* %8
%60 = call i32 @htons(i16 zeroext %59)
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %60, i32* %61
%62 = load i32, i32* %6
%63 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%64 = call i64 @connect(i32 %62, %struct.sockaddr* %63, i32 12)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %51
%67 = call i32 @DieWithError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %68
68: ; preds = %66, %51
%69 = load i8*, i8** %10
%70 = call i32 @strlen(i8* %69)
store i32 %70, i32* %12
%71 = load i32, i32* %6
%72 = load i8*, i8** %10
%73 = load i32, i32* %12
%74 = call i32 @send(i32 %71, i8* %72, i32 %73, i32 0)
%75 = load i32, i32* %12
%76 = icmp ne i32 %74, %75
br i1 %76, label %77, label %79
77: ; preds = %68
%78 = call i32 @DieWithError(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %79
79: ; preds = %77, %68
store i32 0, i32* %14
%80 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %81
81: ; preds = %92, %79
%82 = load i32, i32* %14
%83 = load i32, i32* %12
%84 = icmp ult i32 %82, %83
br i1 %84, label %85, label %101
85: ; preds = %81
%86 = load i32, i32* %6
%87 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%88 = call i32 @recv(i32 %86, i8* %87, i32 31, i32 0)
store i32 %88, i32* %13
%89 = icmp sle i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 @DieWithError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %92
92: ; preds = %90, %85
%93 = load i32, i32* %13
%94 = load i32, i32* %14
%95 = add nsw i32 %94, %93
store i32 %95, i32* %14
%96 = load i32, i32* %13
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 %97
store i8 0, i8* %98
%99 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%100 = call i32 @printf(i8* %99)
br label %81
101: ; preds = %81
%102 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%103 = load i32, i32* %6
%104 = call i32 @close(i32 %103)
%105 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [49 x i8] c"Usage: %s <Server IP> <Echo Word> [<Echo Port.]\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"Unable to create socket\00"
@AF_INET = global i32 0
@.str.2 = [21 x i8] c"connect() is failed!\00"
@.str.3 = [53 x i8] c"Something is just wrong with the sending of message!\00"
@.str.4 = [11 x i8] c"Recieved: \00"
@.str.5 = [41 x i8] c"Couldnt get the message from the server!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [32 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = add i32 %0, -5
%8 = icmp ult i32 %7, -2
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = icmp eq i32 %0, 4
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = getelementptr inbounds i8*, i8** %1, i64 3
%22 = load i8*, i8** %21
%23 = tail call zeroext i16 @atoi(i8* %22)
br label %24
24: ; preds = %14, %20
%25 = phi i16 [ %23, %20 ], [ 7, %14 ]
%26 = load i32, i32* @PF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = load i32, i32* @IPPROTO_TCP
%29 = tail call i32 @socket(i32 %26, i32 %27, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 @DieWithError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
%34 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %35, i32* %36
%37 = call i32 @inet_addr(i8* %16)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %37, i32* %38
%39 = call i32 @htons(i16 zeroext %25)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i64 @connect(i32 %29, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = call i32 @DieWithError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %33
%47 = call i32 @strlen(i8* %18)
%48 = call i32 @send(i32 %29, i8* %18, i32 %47, i32 0)
%49 = icmp eq i32 %48, %47
br i1 %49, label %52, label %50
50: ; preds = %46
%51 = call i32 @DieWithError(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%54 = icmp eq i32 %47, 0
br i1 %54, label %67, label %55
55: ; preds = %52, %61
%56 = phi i32 [ %62, %61 ], [ 0, %52 ]
%57 = call i32 @recv(i32 %29, i8* %6, i32 31, i32 0)
%58 = icmp slt i32 %57, 1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @DieWithError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %59, %55
%62 = add nsw i32 %57, %56
%63 = sext i32 %57 to i64
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %63
store i8 0, i8* %64
%65 = call i32 @printf(i8* %6)
%66 = icmp ult i32 %62, %47
br i1 %66, label %55, label %67
67: ; preds = %61, %52
%68 = call i32 @putchar(i32 10)
%69 = call i32 @close(i32 %29)
%70 = call i32 @exit(i32 0)
unreachable
}