#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 *servIP;
  char *echoString;
  char echoBuffer[32];
  unsigned int echoStringLen;
  int bytesRcvd, totalBytesRcvd;
  if ((argc < 3) || (argc > 4)) {
    fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",
            argv[0]);
    exit(1);
  }
  servIP = argv[1];
  echoString = argv[2];
  if (argc == 4)
    echoServPort = atoi(argv[3]);
  else
    echoServPort = 7;
  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    DieWithError("socket() failed");
  memset(&echoServAddr, 0, sizeof(echoServAddr));
  echoServAddr.sin_family = AF_INET;
  echoServAddr.sin_addr.s_addr = inet_addr(servIP);
  echoServAddr.sin_port = htons(echoServPort);
  if (connect(sock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("connect() failed");
  echoStringLen = strlen(echoString);
  if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
    DieWithError("send() sent a different number of bytes than expected");
  totalBytesRcvd = 0;
  printf("Received: ");
  while (totalBytesRcvd < echoStringLen) {
    if ((bytesRcvd = recv(sock, echoBuffer, 32 - 1, 0)) <= 0)
      DieWithError("recv() failed or connection closed prematurely");
    totalBytesRcvd += bytesRcvd;
    echoBuffer[bytesRcvd] = '\0';
    printf("%s", 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)
cmp    $0x0,%eax
jge    12a6 <main+0xc6>
lea    0xd90(%rip),%rdi
call   10c0 <DieWithError@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d2b(%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    1301 <main+0x121>
lea    0xd45(%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     1333 <main+0x153>
lea    0xd24(%rip),%rdi
call   10c0 <DieWithError@plt>
movl   $0x0,-0x6c(%rbp)
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x6c(%rbp),%eax
cmp    -0x64(%rbp),%eax
jae    13a8 <main+0x1c8>
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     137f <main+0x19f>
lea    0xd19(%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),%rsi
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1348 <main+0x168>
lea    0xc80(%rip),%rdi
mov    $0x0,%al
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    0xd78(%rip),%rdi
call   10d0 <DieWithError@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    %r12d,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    %r13d,%eax
je     12f9 <main+0x109>
lea    0xd5e(%rip),%rdi
call   10d0 <DieWithError@plt>
lea    0xd88(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
test   %r13d,%r13d
jne    1325 <main+0x135>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0x10(%rsp),%rbx
lea    0xd91(%rip),%r15
xor    %r14d,%r14d
jmp    135d <main+0x16d>
cs nopw 0x0(%rax,%rax,1)
add    %ebp,%r14d
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
cmp    %r13d,%r14d
jae    130c <main+0x11c>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1340 <main+0x150>
lea    0xd17(%rip),%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 = [16 x i8] c"socket() failed\00"
@AF_INET = global i32 0
@.str.2 = [17 x i8] c"connect() failed\00"
@.str.3 = [54 x i8] c"send() sent a different number of bytes than expected\00"
@.str.4 = [11 x i8] c"Received: \00"
@.str.5 = [47 x i8] c"recv() failed or connection closed prematurely\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [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 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
%51 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%52 = load i32, i32* @AF_INET
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %52, i32* %53
%54 = load i8*, i8** %9
%55 = call i32 @inet_addr(i8* %54)
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
store i32 %55, i32* %57
%58 = load i16, i16* %8
%59 = call i32 @htons(i16 zeroext %58)
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %59, i32* %60
%61 = load i32, i32* %6
%62 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%63 = call i64 @connect(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %50
%66 = call i32 @DieWithError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %50
%68 = load i8*, i8** %10
%69 = call i32 @strlen(i8* %68)
store i32 %69, i32* %12
%70 = load i32, i32* %6
%71 = load i8*, i8** %10
%72 = load i32, i32* %12
%73 = call i32 @send(i32 %70, i8* %71, i32 %72, i32 0)
%74 = load i32, i32* %12
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %78
76: ; preds = %67
%77 = call i32 @DieWithError(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
br label %78
78: ; preds = %76, %67
store i32 0, i32* %14
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %91, %78
%81 = load i32, i32* %14
%82 = load i32, i32* %12
%83 = icmp ult i32 %81, %82
br i1 %83, label %84, label %100
84: ; preds = %80
%85 = load i32, i32* %6
%86 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%87 = call i32 @recv(i32 %85, i8* %86, i32 31, i32 0)
store i32 %87, i32* %13
%88 = icmp sle i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %84
%90 = call i32 @DieWithError(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0))
br label %91
91: ; preds = %89, %84
%92 = load i32, i32* %13
%93 = load i32, i32* %14
%94 = add nsw i32 %93, %92
store i32 %94, i32* %14
%95 = load i32, i32* %13
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 %96
store i8 0, i8* %97
%98 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %98)
br label %80
100: ; preds = %80
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%102 = load i32, i32* %6
%103 = call i32 @close(i32 %102)
%104 = 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 = [16 x i8] c"socket() failed\00"
@AF_INET = global i32 0
@.str.2 = [17 x i8] c"connect() failed\00"
@.str.3 = [54 x i8] c"send() sent a different number of bytes than expected\00"
@.str.4 = [11 x i8] c"Received: \00"
@.str.5 = [47 x i8] c"recv() failed or connection closed prematurely\00"
@.str.6 = [3 x i8] c"%s\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 ([16 x i8], [16 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 ([17 x i8], [17 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 ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = call i32 (i8*, ...) @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 ([47 x i8], [47 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), 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
}