#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*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
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;
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;
  int port = 3000;
  if (argc != 3) {
    fprintf(stderr, "USAGE: client <server_ip> <word>\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(port);
  if (connect(sock, (struct sockaddr *)&echoserver, sizeof(echoserver)) < 0) {
    Die("Failed to connect with server");
  } else {
    printf("Connected to 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)
movl   $0xbb8,-0x4c(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     121d <main+0x4d>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1255 <main+0x85>
lea    0xdd2(%rip),%rdi
call   10c0 <Die@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4c(%rbp),%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    0xd8c(%rip),%rdi
call   10c0 <Die@plt>
jmp    12c4 <main+0xf4>
lea    0xd99(%rip),%rdi
call   1070 <printf@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     12fe <main+0x12e>
lea    0xd73(%rip),%rdi
call   10c0 <Die@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
call   10b0 <fprintf@plt>
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jae    137a <main+0x1aa>
movl   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x50(%rbp)
cmp    $0x1,%eax
jge    1351 <main+0x181>
lea    0xd4c(%rip),%rdi
call   10c0 <Die@plt>
mov    -0x50(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
movslq -0x50(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   10b0 <fprintf@plt>
jmp    1313 <main+0x143>
mov    0x2c37(%rip),%rax
mov    (%rax),%edi
lea    0xc96(%rip),%rsi
call   10b0 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1090 <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    $0x3,%edi
jne    1279 <main+0xa9>
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    0xe0a(%rip),%rdi
call   10c0 <Die@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0xbb8,%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    1298 <main+0xc8>
lea    0xdc8(%rip),%rdi
call   10c0 <Die@plt>
jmp    12a4 <main+0xd4>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xdb9(%rip),%rdi
call   1070 <printf@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     12d1 <main+0x101>
lea    0xda0(%rip),%rdi
call   10c0 <Die@plt>
mov    0x2ce0(%rip),%r13
mov    0x0(%r13),%edi
lea    0xdaa(%rip),%rsi
call   10b0 <fprintf@plt>
test   %r12d,%r12d
jne    130c <main+0x13c>
mov    0x0(%r13),%edi
lea    0xd28(%rip),%rsi
call   10b0 <fprintf@plt>
mov    %r15d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
xor    %r14d,%r14d
lea    0x10(%rsp),%rbx
jmp    133c <main+0x16c>
cs nopw 0x0(%rax,%rax,1)
add    %ebp,%r14d
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   10b0 <fprintf@plt>
cmp    %r12d,%r14d
jae    12ed <main+0x11d>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1320 <main+0x150>
lea    0xd3d(%rip),%rdi
call   10c0 <Die@plt>
jmp    1320 <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 = [34 x i8] c"USAGE: client <server_ip> <word>\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 = [20 x i8] c"Connected to server\00"
@.str.4 = [33 x i8] c"Mismatch in number of sent bytes\00"
@stdout = global i32 0
@.str.5 = [11 x i8] c"Received: \00"
@.str.6 = [36 x i8] c"Failed to receive bytes from server\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 [32 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
store i32 3000, i32* %11
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = load i32, i32* @IPPROTO_TCP
%23 = call i32 @socket(i32 %20, i32 %21, i32 %22)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @inet_addr(i8* %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %11
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %27
%45 = call i32 @Die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %48
46: ; preds = %27
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %44
%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.4, 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.5, 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* %12
%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* %12
%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.6, i64 0, i64 0))
br label %77
77: ; preds = %75, %70
%78 = load i32, i32* %12
%79 = load i32, i32* %10
%80 = add nsw i32 %79, %78
store i32 %80, i32* %10
%81 = load i32, i32* %12
%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.7, 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 = [34 x i8] c"USAGE: client <server_ip> <word>\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 = [20 x i8] c"Connected to server\00"
@.str.4 = [33 x i8] c"Mismatch in number of sent bytes\00"
@stdout = global i32 0
@.str.5 = [11 x i8] c"Received: \00"
@.str.6 = [36 x i8] c"Failed to receive bytes from server\00"
@.str.7 = [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, 3
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 ([34 x i8], [34 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 = call i32 @htons(i32 3000)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @connect(i32 %16, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @Die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %37
35: ; preds = %20
%36 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
%38 = getelementptr inbounds i8*, i8** %1, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = load i8*, i8** %38
%42 = call i32 @send(i32 %16, i8* %41, i32 %40, i32 0)
%43 = icmp eq i32 %42, %40
br i1 %43, label %46, label %44
44: ; preds = %37
%45 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %37
%47 = load i32, i32* @stdout
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%49 = icmp eq i32 %40, 0
br i1 %49, label %63, label %50
50: ; preds = %46, %56
%51 = phi i32 [ %57, %56 ], [ 0, %46 ]
%52 = call i32 @recv(i32 %16, i8* %6, i32 31, i32 0)
%53 = icmp slt i32 %52, 1
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @Die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = add nsw i32 %52, %51
%58 = sext i32 %52 to i64
%59 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %58
store i8 0, i8* %59
%60 = load i32, i32* @stdout
%61 = call i32 @fprintf(i32 %60, i8* %6)
%62 = icmp ult i32 %57, %40
br i1 %62, label %50, label %63
63: ; preds = %56, %46
%64 = load i32, i32* @stdout
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%66 = call i32 @close(i32 %16)
%67 = call i32 @exit(i32 0)
unreachable
}