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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_in servaddr;
  char sendline[4096], recvline[4096];
  if (argc != 2) {
    perror("Usage: TCPClient <IP address of the server>\n");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Problem in creating the socket");
    exit(2);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr(argv[1]);
  servaddr.sin_port = htons(3000);
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("Problem in connecting to the server");
    exit(3);
  }
  while (1) {
    printf("Enter the string to send : ");
    if (fgets(sendline, 4096, stdin) == NULL)
      exit(1);
    send(sockfd, sendline, strlen(sendline), 0);
    if (recv(sockfd, recvline, 4096, 0) == 0) {
      perror("The server terminated prematurely");
      exit(4);
    }
    fprintf(stdout, "String received back from the server: %s\n", recvline);
    bzero(recvline, 4096);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1219 <main+0x39>
lea    0xdf6(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1254 <main+0x74>
lea    0xde8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d7d(%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    $0xbb8,%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    12be <main+0xde>
lea    0xd9d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
jmp    12c3 <main+0xe3>
lea    0xda6(%rip),%rdi
call   1070 <printf@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2d03(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
jne    12fd <main+0x11d>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x202c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x2028(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x202c(%rbp),%edi
mov    -0x2028(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jne    136c <main+0x18c>
lea    0xd2f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
lea    -0x2020(%rbp),%rdx
lea    0xd2b(%rip),%rsi
call   10b0 <fprintf@plt>
lea    -0x2020(%rbp),%rdi
mov    $0x1000,%esi
call   10d0 <bzero@plt>
jmp    12c3 <main+0xe3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
cmp    $0x2,%edi
jne    122d <main+0x4d>
mov    %rsi,%rbx
mov    0x2de8(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1243 <main+0x63>
lea    0xe0f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    0xdcc(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%r14d
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0xbb8,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
jns    12a4 <main+0xc4>
lea    0xdb7(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
lea    0xdc5(%rip),%rdi
call   1070 <printf@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     136d <main+0x18d>
lea    0x10(%rsp),%rbx
lea    0x1010(%rsp),%rbp
mov    0x2ce3(%rip),%r13
lea    0xdc2(%rip),%r15
lea    0xd7d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1377 <main+0x197>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10b0 <fprintf@plt>
mov    %rbp,%rdi
mov    $0x1000,%esi
call   10d0 <bzero@plt>
mov    %r12,%rdi
call   1070 <printf@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1090 <fgets@plt>
test   %rax,%rax
jne    1300 <main+0x120>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd0e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [45 x i8] c"Usage: TCPClient <IP address of the server>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [31 x i8] c"Problem in creating the socket\00"
@.str.2 = [36 x i8] c"Problem in connecting to the server\00"
@.str.3 = [28 x i8] c"Enter the string to send : \00"
@stdin = global i32 0
@.str.4 = [34 x i8] c"The server terminated prematurely\00"
@stdout = global i32 0
@.str.5 = [42 x i8] c"String received back from the server: %s\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 [4096 x i8]
%9 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 2)
unreachable
23: ; preds = %15
%24 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @inet_addr(i8* %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = call i32 @htons(i32 3000)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i64 @connect(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %23
%40 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 3)
unreachable
42: ; preds = %23
br label %43
43: ; preds = %42, %64
%44 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%46 = load i32, i32* @stdin
%47 = call i32* @fgets(i8* %45, i32 4096, i32 %46)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = load i32, i32* %6
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%55 = call i32 @strlen(i8* %54)
%56 = call i32 @send(i32 %52, i8* %53, i32 %55, i32 0)
%57 = load i32, i32* %6
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%59 = call i64 @recv(i32 %57, i8* %58, i32 4096, i32 0)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %51
%62 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @exit(i32 4)
unreachable
64: ; preds = %51
%65 = load i32, i32* @stdout
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%67 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %66)
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%69 = call i32 @bzero(i8* %68, i32 4096)
br label %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [45 x i8] c"Usage: TCPClient <IP address of the server>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [31 x i8] c"Problem in creating the socket\00"
@.str.2 = [36 x i8] c"Problem in connecting to the server\00"
@.str.3 = [28 x i8] c"Enter the string to send : \00"
@stdin = global i32 0
@.str.4 = [34 x i8] c"The server terminated prematurely\00"
@stdout = global i32 0
@.str.5 = [42 x i8] c"String received back from the server: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = alloca [4096 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %13
%22 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @inet_addr(i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %27, i32* %28
%29 = call i32 @htons(i32 3000)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %16, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %21
%35 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @stdin
%37 = call i32* @fgets(i8* %7, i32 4096, i32 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %42, label %44
39: ; preds = %21
%40 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 3)
unreachable
42: ; preds = %52, %34
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34, %52
%45 = call i32 @strlen(i8* %7)
%46 = call i32 @send(i32 %16, i8* %7, i32 %45, i32 0)
%47 = call i64 @recv(i32 %16, i8* %8, i32 4096, i32 0)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 4)
unreachable
52: ; preds = %44
%53 = load i32, i32* @stdout
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%55 = call i32 @bzero(i8* %8, i32 4096)
%56 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* @stdin
%58 = call i32* @fgets(i8* %7, i32 4096, i32 %57)
%59 = icmp eq i32* %58, null
br i1 %59, label %42, label %44
}