#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;
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*/ perror(char *);
int printf(char *, ...);
int 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*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_in servaddr;
  char sendLine[4096], recvline[4096];
  int n;
  if (argc != 2) {
    fprintf(stderr, "Usage: TCPClient <IP address of the server>\n");
    exit(12);
  }
  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(15828);
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("Problem in connecting to the server");
    exit(3);
  }
  strncpy(sendLine, "1", 4096);
  send(sockfd, sendLine, strlen(sendLine), 0);
  while (printf("waiting for incoming msg\n"),
         n = recv(sockfd, recvline, 4096, 0), n > 0) {
    if (n < 0) {
      perror("The server terminated prematurely\n");
      exit(4);
    }
    printf("String [%d] received %s\n", n, recvline);
  }
  printf("n= [%d]\n", n);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1212 <main+0x42>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0xc,%edi
call   10d0 <exit@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124d <main+0x7d>
lea    0xdef(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d84(%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    $0x3dd4,%edi
call   1060 <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    12b7 <main+0xe7>
lea    0xda4(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
lea    -0x1020(%rbp),%rdi
lea    0xdab(%rip),%rsi
mov    $0x1000,%edx
call   1040 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x2030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2034(%rbp),%edi
mov    -0x2030(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2024(%rbp)
cmpl   $0x0,-0x2024(%rbp)
jle    1382 <main+0x1b2>
cmpl   $0x0,-0x2024(%rbp)
jge    1362 <main+0x192>
lea    0xd39(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x4,%edi
call   10d0 <exit@plt>
mov    -0x2024(%rbp),%esi
lea    -0x2020(%rbp),%rdx
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1308 <main+0x138>
mov    -0x2024(%rbp),%esi
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
cmp    $0x2,%edi
jne    1229 <main+0x49>
mov    %rsi,%r14
mov    0x2dec(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1248 <main+0x68>
lea    0xe13(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0xc,%edi
call   10e0 <exit@plt>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x3dd4,%edi
call   1070 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
jns    12a9 <main+0xc9>
lea    0xdb2(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
lea    0xdc0(%rip),%rsi
lea    0x1010(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
lea    0xdb1(%rip),%rdi
call   1050 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    134d <main+0x16d>
lea    0xd6b(%rip),%r14
lea    0x10(%rsp),%rbp
lea    0xd81(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1320 <main+0x140>
lea    0xd37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %edi,%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 }
@stderr = global i32 0
@.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 = [2 x i8] c"1\00"
@.str.4 = [26 x i8] c"waiting for incoming msg\0A\00"
@.str.5 = [35 x i8] c"The server terminated prematurely\0A\00"
@.str.6 = [25 x i8] c"String [%d] received %s\0A\00"
@.str.7 = [9 x i8] c"n= [%d]\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]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 12)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %17
%26 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @inet_addr(i8* %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = call i32 @htons(i32 15828)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %25
%42 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 3)
unreachable
44: ; preds = %25
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%46 = call i32 @strncpy(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 4096)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @send(i32 %47, i8* %48, i32 %50, i32 0)
br label %52
52: ; preds = %65, %44
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* %6
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%56 = call i32 @recv(i32 %54, i8* %55, i32 4096, i32 0)
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %52
%60 = load i32, i32* %10
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%64 = call i32 @exit(i32 4)
unreachable
65: ; preds = %59
%66 = load i32, i32* %10
%67 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %66, i8* %67)
br label %52
69: ; preds = %52
%70 = load i32, i32* %10
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %70)
%72 = 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 = [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 = [2 x i8] c"1\00"
@.str.6 = [25 x i8] c"String [%d] received %s\0A\00"
@.str.7 = [9 x i8] c"n= [%d]\0A\00"
@str = [25 x i8] c"waiting for incoming msg\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 %14, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 12)
unreachable
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %14
%23 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = call i32 @htons(i32 15828)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @connect(i32 %17, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %22
%36 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 3)
unreachable
38: ; preds = %22
%39 = call i32 @strncpy(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 4096)
%40 = call i32 @strlen(i8* %7)
%41 = call i32 @send(i32 %17, i8* %7, i32 %40, i32 0)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%43 = call i32 @recv(i32 %17, i8* %8, i32 4096, i32 0)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %38, %45
%46 = phi i32 [ %49, %45 ], [ %43, %38 ]
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %46, i8* %8)
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%49 = call i32 @recv(i32 %17, i8* %8, i32 4096, i32 0)
%50 = icmp sgt i32 %49, 0
br i1 %50, label %45, label %51
51: ; preds = %45, %38
%52 = phi i32 [ %43, %38 ], [ %49, %45 ]
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %52)
%54 = call i32 @exit(i32 0)
unreachable
}