#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char const *, int /*<<< orphan*/,
                        int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int main() {
  const char *server_name = "localhost";
  const int server_port = 8877;
  struct sockaddr_in server_address;
  memset(&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  inet_pton(AF_INET, server_name, &server_address.sin_addr);
  server_address.sin_port = htons(server_port);
  int sock;
  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    printf("could not create socket\n");
    return 1;
  }
  if (connect(sock, (struct sockaddr *)&server_address,
              sizeof(server_address)) < 0) {
    printf("could not connect to server\n");
    return 1;
  }
  const char *data_to_send = "Gangadhar Hi Shaktimaan hai";
  send(sock, data_to_send, strlen(data_to_send), 0);
  int n = 0;
  int len = 0, maxlen = 100;
  char buffer[maxlen];
  char *pbuffer = buffer;
  while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
    pbuffer += n;
    maxlen -= n;
    len += n;
    buffer[len] = '\0';
    printf("received: '%s'\n", buffer);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x22ad,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
add    $0x4,%rdx
call   10a0 <inet_pton@plt>
mov    $0x22ad,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1243 <main+0xa3>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135e <main+0x1be>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1278 <main+0xd8>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135e <main+0x1be>
lea    0xdc1(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x30(%rbp),%rdi
mov    %rdi,-0x68(%rbp)
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x64,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x48(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x60(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x50(%rbp)
mov    %rax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x3c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jle    1348 <main+0x1a8>
mov    -0x60(%rbp),%rsi
mov    -0x34(%rbp),%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x3c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
movslq -0x38(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12e7 <main+0x147>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    %edi,-0x30(%rbp)
lea    -0x34(%rbp),%rdx
lea    0xe18(%rip),%rsi
call   10b0 <inet_pton@plt>
mov    $0x22ad,%edi
call   1060 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12d6 <main+0x126>
mov    %eax,%r15d
lea    -0x38(%rbp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     12df <main+0x12f>
lea    0xdcc(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %rsp,-0x40(%rbp)
mov    %rsp,%r14
add    $0xffffffffffffff90,%r14
mov    %r14,%rsp
mov    $0x64,%ebx
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12c6 <main+0x116>
mov    %r14,%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%ecx
add    %rcx,%r12
sub    %eax,%ebx
add    %eax,%r13d
movslq %r13d,%rax
movb   $0x0,(%r14,%rax,1)
lea    0xd7d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1290 <main+0xe0>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    -0x40(%rbp),%rsp
xor    %eax,%eax
jmp    12f0 <main+0x140>
lea    0xd75(%rip),%rdi
jmp    12e6 <main+0x136>
lea    0xd50(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"could not create socket\0A\00"
@.str.2 = [29 x i8] c"could not connect to server\0A\00"
@.str.3 = [28 x i8] c"Gangadhar Hi Shaktimaan hai\00"
@.str.4 = [16 x i8] c"received: '%s'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %2
store i32 8877, i32* %3
%13 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i8*, i8** %2
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%19 = call i32 @inet_pton(i32 %16, i8* %17, i32* %18)
%20 = call i32 @htons(i32 8877)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %70
28: ; preds = %0
%29 = load i32, i32* %5
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %70
35: ; preds = %28
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8** %6
%36 = load i32, i32* %5
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
%40 = call i32 @send(i32 %36, i8* %37, i32 %39, i32 0)
store i32 0, i32* %7
store i32 0, i32* %8
store i32 100, i32* %9
%41 = load i32, i32* %9
%42 = zext i32 %41 to i64
%43 = call i8* @llvm.stacksave()
store i8* %43, i8** %10
%44 = alloca i8, i64 %42
store i64 %42, i64* %11
store i8* %44, i8** %12
br label %45
45: ; preds = %51, %35
%46 = load i32, i32* %5
%47 = load i8*, i8** %12
%48 = load i32, i32* %9
%49 = call i32 @recv(i32 %46, i8* %47, i32 %48, i32 0)
store i32 %49, i32* %7
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %66
51: ; preds = %45
%52 = load i32, i32* %7
%53 = load i8*, i8** %12
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8* %55, i8** %12
%56 = load i32, i32* %7
%57 = load i32, i32* %9
%58 = sub nsw i32 %57, %56
store i32 %58, i32* %9
%59 = load i32, i32* %7
%60 = load i32, i32* %8
%61 = add nsw i32 %60, %59
store i32 %61, i32* %8
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %44, i64 %63
store i8 0, i8* %64
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %45
66: ; preds = %45
%67 = load i32, i32* %5
%68 = call i32 @close(i32 %67)
store i32 0, i32* %1
%69 = load i8*, i8** %10
br label %70
70: ; preds = %66, %33, %26
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [28 x i8] c"Gangadhar Hi Shaktimaan hai\00"
@.str.4 = [16 x i8] c"received: '%s'\0A\00"
@str = [28 x i8] c"could not connect to server\00"
@str.5 = [24 x i8] c"could not create socket\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%7 = call i32 @inet_pton(i32 %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %6)
%8 = call i32 @htons(i32 8877)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
br label %46
16: ; preds = %0
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @connect(i32 %12, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %46
22: ; preds = %16
%23 = call i32 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @send(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 0)
%25 = call i8* @llvm.stacksave()
%26 = alloca [100 x i8]
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %26, i64 0, i64 0
%28 = call i32 @recv(i32 %12, i8* %27, i32 100, i32 0)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %22, %30
%31 = phi i32 [ %42, %30 ], [ %28, %22 ]
%32 = phi i8* [ %36, %30 ], [ %27, %22 ]
%33 = phi i32 [ %37, %30 ], [ 100, %22 ]
%34 = phi i32 [ %38, %30 ], [ 0, %22 ]
%35 = zext i32 %31 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = sub nsw i32 %33, %31
%38 = add nsw i32 %31, %34
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %26, i64 0, i64 %39
store i8 0, i8* %40
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%42 = call i32 @recv(i32 %12, i8* %36, i32 %37, i32 0)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %30, label %44
44: ; preds = %30, %22
%45 = call i32 @close(i32 %12)
br label %46
46: ; preds = %44, %20, %14
%47 = phi i32 [ 1, %14 ], [ 1, %20 ], [ 0, %44 ]
ret i32 %47
}