#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*/ echoserver;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Die(char *);
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
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;
int /*<<< orphan*/ stdout;
unsigned int strlen(char *);
int main(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in echoserver;
  char buffer[32];
  unsigned int echolen;
  int received = 0;
  if (argc != 4) {
    fprintf(stderr, "USAGE: TCPecho <server_ip> <word> <port>\n");
    exit(1);
  }
  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    Die("Failed to create socket");
  }
  memset(&echoserver, 0, sizeof(echoserver));
  echoserver.sin_family = AF_INET;
  echoserver.sin_addr.s_addr = inet_addr(argv[1]);
  echoserver.sin_port = htons(atoi(argv[3]));
  if (connect(sock, (struct sockaddr *)&echoserver, sizeof(echoserver)) < 0) {
    Die("Failed to connect with server");
  }
  echolen = strlen(argv[2]);
  if (send(sock, argv[2], echolen, 0) != echolen) {
    Die("Mismatch in number of sent bytes");
  }
  fprintf(stdout, "Received: ");
  while (received < echolen) {
    int bytes = 0;
    if ((bytes = recv(sock, buffer, 32 - 1, 0)) < 1) {
      Die("Failed to receive bytes from server");
    }
    received += bytes;
    buffer[bytes] = '\0';
    fprintf(stdout, buffer);
  }
  fprintf(stdout, "\n");
  close(sock);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1216 <main+0x46>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124e <main+0x7e>
lea    0xde1(%rip),%rdi
call   10b0 <Die@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    12b8 <main+0xe8>
lea    0xd8f(%rip),%rdi
call   10b0 <Die@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x44(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    -0x44(%rbp),%eax
je     12f2 <main+0x122>
lea    0xd73(%rip),%rdi
call   10b0 <Die@plt>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jae    136e <main+0x19e>
movl   $0x0,-0x4c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x1,%eax
jge    1345 <main+0x175>
lea    0xd4c(%rip),%rdi
call   10b0 <Die@plt>
mov    -0x4c(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
movslq -0x4c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   10a0 <fprintf@plt>
jmp    1307 <main+0x137>
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
lea    0xcaa(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x4,%edi
jne    12e5 <main+0x115>
mov    %rsi,%r14
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    121d <main+0x4d>
lea    0xe12(%rip),%rdi
call   10b0 <Die@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x18(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
jns    127d <main+0xad>
lea    0xdca(%rip),%rdi
call   10b0 <Die@plt>
mov    0x10(%r14),%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    0x10(%r14),%rsi
mov    %r15d,%edi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    %r12d,%eax
je     12aa <main+0xda>
lea    0xdbb(%rip),%rdi
call   10b0 <Die@plt>
mov    0x2d07(%rip),%r13
mov    0x0(%r13),%edi
lea    0xdc5(%rip),%rsi
call   10a0 <fprintf@plt>
test   %r12d,%r12d
jne    1304 <main+0x134>
mov    0x0(%r13),%edi
lea    0xd57(%rip),%rsi
call   10a0 <fprintf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %r14d,%r14d
lea    0x10(%rsp),%rbx
jmp    132c <main+0x15c>
xchg   %ax,%ax
add    %ebp,%r14d
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   10a0 <fprintf@plt>
cmp    %r12d,%r14d
jae    12c6 <main+0xf6>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1310 <main+0x140>
lea    0xd41(%rip),%rdi
call   10b0 <Die@plt>
jmp    1310 <main+0x140>

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 = [42 x i8] c"USAGE: TCPecho <server_ip> <word> <port>\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"Failed to create socket\00"
@AF_INET = global i32 0
@.str.2 = [30 x i8] c"Failed to connect with server\00"
@.str.3 = [33 x i8] c"Mismatch in number of sent bytes\00"
@stdout = global i32 0
@.str.4 = [11 x i8] c"Received: \00"
@.str.5 = [36 x i8] c"Failed to receive bytes from 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 [32 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* @IPPROTO_TCP
%22 = call i32 @socket(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @inet_addr(i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %26
%47 = call i32 @Die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %26
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = call i32 @strlen(i8* %51)
store i32 %52, i32* %9
%53 = load i32, i32* %6
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = load i32, i32* %9
%58 = call i32 @send(i32 %53, i8* %56, i32 %57, i32 0)
%59 = load i32, i32* %9
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %48
%62 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %61, %48
%64 = load i32, i32* @stdout
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %77, %63
%67 = load i32, i32* %10
%68 = load i32, i32* %9
%69 = icmp ult i32 %67, %68
br i1 %69, label %70, label %87
70: ; preds = %66
store i32 0, i32* %11
%71 = load i32, i32* %6
%72 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%73 = call i32 @recv(i32 %71, i8* %72, i32 31, i32 0)
store i32 %73, i32* %11
%74 = icmp slt i32 %73, 1
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = call i32 @Die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %77
77: ; preds = %75, %70
%78 = load i32, i32* %11
%79 = load i32, i32* %10
%80 = add nsw i32 %79, %78
store i32 %80, i32* %10
%81 = load i32, i32* %11
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 %82
store i8 0, i8* %83
%84 = load i32, i32* @stdout
%85 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%86 = call i32 @fprintf(i32 %84, i8* %85)
br label %66
87: ; preds = %66
%88 = load i32, i32* @stdout
%89 = call i32 @fprintf(i32 %88, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
%92 = 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 = [42 x i8] c"USAGE: TCPecho <server_ip> <word> <port>\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"Failed to create socket\00"
@AF_INET = global i32 0
@.str.2 = [30 x i8] c"Failed to connect with server\00"
@.str.3 = [33 x i8] c"Mismatch in number of sent bytes\00"
@stdout = global i32 0
@.str.4 = [11 x i8] c"Received: \00"
@.str.5 = [36 x i8] c"Failed to receive bytes from server\00"
@.str.6 = [2 x i8] c"\0A\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 = icmp eq i32 %0, 4
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = load i32, i32* @IPPROTO_TCP
%16 = tail call i32 @socket(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = getelementptr inbounds i8*, i8** %1, i64 3
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %31, i32* %32
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @connect(i32 %16, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %20
%37 = call i32 @Die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %20
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = call i32 @strlen(i8* %40)
%42 = load i8*, i8** %39
%43 = call i32 @send(i32 %16, i8* %42, i32 %41, i32 0)
%44 = icmp eq i32 %43, %41
br i1 %44, label %47, label %45
45: ; preds = %38
%46 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %38
%48 = load i32, i32* @stdout
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%50 = icmp eq i32 %41, 0
br i1 %50, label %64, label %51
51: ; preds = %47, %57
%52 = phi i32 [ %58, %57 ], [ 0, %47 ]
%53 = call i32 @recv(i32 %16, i8* %6, i32 31, i32 0)
%54 = icmp slt i32 %53, 1
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @Die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %55, %51
%58 = add nsw i32 %53, %52
%59 = sext i32 %53 to i64
%60 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %59
store i8 0, i8* %60
%61 = load i32, i32* @stdout
%62 = call i32 @fprintf(i32 %61, i8* %6)
%63 = icmp ult i32 %58, %41
br i1 %63, label %51, label %64
64: ; preds = %57, %47
%65 = load i32, i32* @stdout
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%67 = call i32 @close(i32 %16)
%68 = call i32 @exit(i32 0)
unreachable
}