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

/* Type definitions */
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main() {
  int sock, len;
  char buf[1024];
  struct sockaddr_in server_addr;
  struct hostent *host;
  signal(SIGPIPE, SIG_IGN);
  host = gethostbyname("localhost");
  if (NULL == host) {
    perror("gethostbyname");
    exit(-1);
  }
  if (0 > (sock = socket(AF_INET, SOCK_STREAM, 0))) {
    perror("socket");
    exit(-1);
  }
  memset(&server_addr, '\0', sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(7842);
  server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  if (0 >
      connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))) {
    perror("connect");
    exit(-1);
  }
  while (1) {
    if (-1 == send(sock, "Hi Pawan Prakash Sharma!\n",
                   sizeof("Hi Pawan Prakash Sharma!\n") - 1, 0)) {
      perror("send");
      break;
    }
    if (0 > (len = recv(sock, buf, 1024, 0))) {
      perror("recv");
      break;
    }
    if (0 == len) {
      printf("connection closed by peer\n");
      break;
    }
    if (0 > write(1, buf, len)) {
      perror("write");
      break;
    }
    sleep(1);
  }
  close(sock);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
lea    0xdf0(%rip),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x428(%rbp)
xor    %eax,%eax
cmp    -0x428(%rbp),%rax
jne    1241 <main+0x61>
lea    0xdd8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    127f <main+0x9f>
lea    0xda8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    $0x1ea2,%edi
call   1050 <htons@plt>
mov    %eax,-0x41c(%rbp)
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    12fb <main+0x11b>
lea    0xd33(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    1300 <main+0x120>
mov    -0x8(%rbp),%edi
lea    0xd1d(%rip),%rsi
mov    $0x19,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1336 <main+0x156>
lea    0xd15(%rip),%rdi
call   10c0 <perror@plt>
jmp    13ca <main+0x1ea>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    136c <main+0x18c>
lea    0xce4(%rip),%rdi
call   10c0 <perror@plt>
jmp    13ca <main+0x1ea>
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
jne    1388 <main+0x1a8>
lea    0xccd(%rip),%rdi
call   1070 <printf@plt>
jmp    13ca <main+0x1ea>
lea    -0x410(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    13bb <main+0x1db>
lea    0xcb5(%rip),%rdi
call   10c0 <perror@plt>
jmp    13ca <main+0x1ea>
mov    $0x1,%edi
call   10f0 <sleep@plt>
jmp    1300 <main+0x120>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
lea    0xdf4(%rip),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
je     133f <main+0x15f>
mov    %rax,%r14
mov    0x2dc4(%rip),%r12
mov    (%r12),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1348 <main+0x168>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    $0x1ea2,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     1351 <main+0x171>
lea    0xd96(%rip),%rsi
mov    %ebx,%edi
mov    $0x19,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x11b>
lea    0x10(%rsp),%r14
lea    0xd77(%rip),%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1312 <main+0x132>
je     1304 <main+0x124>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
js     131b <main+0x13b>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x19,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0xd0>
lea    0xd3f(%rip),%rdi
jmp    1322 <main+0x142>
lea    0xd46(%rip),%rdi
call   1040 <puts@plt>
jmp    1327 <main+0x147>
lea    0xd2d(%rip),%rdi
jmp    1322 <main+0x142>
lea    0xd29(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xcc4(%rip),%rdi
jmp    1358 <main+0x178>
lea    0xcc9(%rip),%rdi
jmp    1358 <main+0x178>
lea    0xcc7(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [26 x i8] c"Hi Pawan Prakash Sharma!\0A\00"
@.str.5 = [5 x i8] c"send\00"
@.str.6 = [5 x i8] c"recv\00"
@.str.7 = [27 x i8] c"connection closed by peer\0A\00"
@.str.8 = [6 x i8] c"write\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.hostent*
store i32 0, i32* %1
%7 = load i32, i32* @SIGPIPE
%8 = load i32, i32* @SIG_IGN
%9 = call i32 @signal(i32 %7, i32 %8)
%10 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %10, %struct.hostent** %6
%11 = load %struct.hostent*, %struct.hostent** %6
%12 = icmp eq %struct.hostent* null, %11
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %2
%20 = icmp sgt i32 0, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %16
%25 = call i32 @memset(%struct.sockaddr_in* %5, i8 signext 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @htons(i32 7842)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%31 = load %struct.hostent*, %struct.hostent** %6
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to %struct.in_addr*
%35 = bitcast %struct.in_addr* %30 to i8*
%36 = bitcast %struct.in_addr* %34 to i8*
%37 = load i32, i32* %2
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 4)
%40 = icmp sgt i64 0, %39
br i1 %40, label %41, label %44
41: ; preds = %24
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %24
br label %45
45: ; preds = %44, %70
%46 = load i32, i32* %2
%47 = call i32 @send(i32 %46, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 25, i32 0)
%48 = icmp eq i32 -1, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %72
51: ; preds = %45
%52 = load i32, i32* %2
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%54 = call i32 @recv(i32 %52, i8* %53, i32 1024, i32 0)
store i32 %54, i32* %3
%55 = icmp sgt i32 0, %54
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %72
58: ; preds = %51
%59 = load i32, i32* %3
%60 = icmp eq i32 0, %59
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
br label %72
63: ; preds = %58
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%65 = load i32, i32* %3
%66 = call i64 @write(i32 1, i8* %64, i32 %65)
%67 = icmp sgt i64 0, %66
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %72
70: ; preds = %63
%71 = call i32 @sleep(i32 1)
br label %45
72: ; preds = %68, %61, %56, %49
%73 = load i32, i32* %2
%74 = call i32 @close(i32 %73)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [26 x i8] c"Hi Pawan Prakash Sharma!\0A\00"
@.str.5 = [5 x i8] c"send\00"
@.str.6 = [5 x i8] c"recv\00"
@.str.8 = [6 x i8] c"write\00"
@str = [26 x i8] c"connection closed by peer\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @SIGPIPE
%6 = load i32, i32* @SIG_IGN
%7 = tail call i32 @signal(i32 %5, i32 %6)
%8 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%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 ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %13
%22 = call i32 @memset(%struct.sockaddr_in* %2, i8 signext 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 7842)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i32*
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%31 = load i32, i32* %29
store i32 %31, i32* %30
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i64 @connect(i32 %16, %struct.sockaddr* %32, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %21
%36 = call i32 @send(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 25, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %41, label %43
38: ; preds = %21
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %57, %35
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %61
43: ; preds = %35, %57
%44 = call i32 @recv(i32 %16, i8* %3, i32 1024, i32 0)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %61
48: ; preds = %43
%49 = icmp eq i32 %44, 0
br i1 %49, label %50, label %52
50: ; preds = %48
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %61
52: ; preds = %48
%53 = call i64 @write(i32 1, i8* %3, i32 %44)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %61
57: ; preds = %52
%58 = call i32 @sleep(i32 1)
%59 = call i32 @send(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 25, i32 0)
%60 = icmp eq i32 %59, -1
br i1 %60, label %41, label %43
61: ; preds = %55, %50, %46, %41
%62 = call i32 @close(i32 %16)
ret i32 0
}