#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned short);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int main() {
  int sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  struct hostent *hp = gethostbyname("localhost");
  if (hp == NULL) {
    perror("gethostbyname() failed");
    exit(EXIT_FAILURE);
  }
  struct sockaddr_in server;
  server.sin_family = PF_INET;
  memcpy((void *)&server.sin_addr, (void *)hp->h_addr, hp->h_length);
  unsigned short port = 8765;
  server.sin_port = htons(port);
  printf("server address is %s\n", inet_ntoa(server.sin_addr));
  if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("connect() failed");
    exit(EXIT_FAILURE);
  }
  while (1) {
    char *msg = "READ sample.txt 1000 5000\n";
    int n = write(sock, msg, strlen(msg));
    fflush(NULL);
    if (n < strlen(msg)) {
      perror("write() failed");
      exit(EXIT_FAILURE);
    }
    char buffer[1025];
    n = read(sock, buffer, 1025);
    if (n < 0) {
      perror("read() failed");
      exit(EXIT_FAILURE);
    } else {
      buffer[n] = '\0';
      printf("Received message from server: %s\n", buffer);
    }
  }
  close(sock);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1222 <main+0x52>
lea    0xdf1(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xde7(%rip),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1257 <main+0x87>
lea    0xdd6(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
movw   $0x223d,-0x22(%rbp)
movzwl -0x22(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd93(%rip),%rdi
call   1070 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    12d8 <main+0x108>
lea    0xd82(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    12dd <main+0x10d>
lea    0xd74(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <fflush@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x444(%rbp),%eax
cmp    %ecx,%eax
jge    1361 <main+0x191>
lea    0xd25(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x401,%edx
call   1080 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    139c <main+0x1cc>
lea    0xcf9(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c3b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
movslq -0x34(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rsi
lea    0xcda(%rip),%rdi
call   1070 <printf@plt>
jmp    12dd <main+0x10d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    0x2df2(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
jns    1208 <main+0x38>
lea    0xdfd(%rip),%rdi
jmp    1330 <main+0x160>
mov    %eax,%r15d
lea    0xdfe(%rip),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
jne    1228 <main+0x58>
lea    0xdf7(%rip),%rdi
jmp    1330 <main+0x160>
mov    (%rbx),%ecx
mov    %ecx,0x8(%rsp)
mov    0x8(%rax),%rsi
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <memcpy@plt>
mov    $0x223d,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xdd5(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
jns    1285 <main+0xb5>
lea    0xdc7(%rip),%rdi
jmp    1330 <main+0x160>
lea    0xdcc(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   10b0 <fflush@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebp
jl     1320 <main+0x150>
lea    0x10(%rsp),%r12
lea    0xdce(%rip),%r14
lea    0xd8f(%rip),%rbp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x401,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1329 <main+0x159>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   10b0 <fflush@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebx
jge    12d0 <main+0x100>
lea    0xd4c(%rip),%rdi
jmp    1330 <main+0x160>
lea    0xd52(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [23 x i8] c"gethostbyname() failed\00"
@.str.3 = [22 x i8] c"server address is %s\0A\00"
@.str.4 = [17 x i8] c"connect() failed\00"
@.str.5 = [27 x i8] c"READ sample.txt 1000 5000\0A\00"
@.str.6 = [15 x i8] c"write() failed\00"
@.str.7 = [14 x i8] c"read() failed\00"
@.str.8 = [34 x i8] c"Received message from server: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.hostent*
%4 = alloca %struct.sockaddr_in
%5 = alloca i16
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1025 x i8]
store i32 0, i32* %1
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store %struct.hostent* %19, %struct.hostent** %3
%20 = load %struct.hostent*, %struct.hostent** %3
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = load i32, i32* @PF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%30 = bitcast i32* %29 to i8*
%31 = load %struct.hostent*, %struct.hostent** %3
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i8*
%35 = load %struct.hostent*, %struct.hostent** %3
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @memcpy(i8* %30, i8* %34, i32 %37)
store i16 8765, i16* %5
%39 = load i16, i16* %5
%40 = call i32 @htons(i16 zeroext %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i8* @inet_ntoa(i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = load i32, i32* %2
%47 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%48 = call i64 @connect(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %26
%51 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %26
br label %55
55: ; preds = %54, %86
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8** %6
%56 = load i32, i32* %2
%57 = load i8*, i8** %6
%58 = load i8*, i8** %6
%59 = call i32 @strlen(i8* %58)
%60 = call i32 @write(i32 %56, i8* %57, i32 %59)
store i32 %60, i32* %7
%61 = call i32 @fflush(i32* null)
%62 = load i32, i32* %7
%63 = load i8*, i8** %6
%64 = call i32 @strlen(i8* %63)
%65 = icmp slt i32 %62, %64
br i1 %65, label %66, label %70
66: ; preds = %55
%67 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %55
%71 = load i32, i32* %2
%72 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%73 = call i32 @read(i32 %71, i8* %72, i32 1025)
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %70
%77 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%78 = load i32, i32* @EXIT_FAILURE
%79 = call i32 @exit(i32 %78)
unreachable
80: ; preds = %70
%81 = load i32, i32* %7
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 %82
store i8 0, i8* %83
%84 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%85 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %80
br label %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [23 x i8] c"gethostbyname() failed\00"
@.str.3 = [22 x i8] c"server address is %s\0A\00"
@.str.4 = [17 x i8] c"connect() failed\00"
@.str.5 = [27 x i8] c"READ sample.txt 1000 5000\0A\00"
@.str.6 = [15 x i8] c"write() failed\00"
@.str.7 = [14 x i8] c"read() failed\00"
@.str.8 = [34 x i8] c"Received message from server: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1025 x i8]
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %11
%19 = bitcast %struct.sockaddr_in* %1 to i8*
%20 = load i32, i32* @PF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to i8*
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i8* %19, i8* %25, i32 %27)
%29 = call i32 @htons(i16 zeroext 8765)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* %22
%32 = call i8* @inet_ntoa(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%35 = call i64 @connect(i32 %5, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %18
%38 = call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @write(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = call i32 @fflush(i32* null)
%41 = call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp slt i32 %39, %41
br i1 %42, label %49, label %43
43: ; preds = %37
%44 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
br label %53
45: ; preds = %18
%46 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %60, %37
%50 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %43, %60
%54 = call i32 @read(i32 %5, i8* %44, i32 1025)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %53
%61 = zext i32 %54 to i64
%62 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %61
store i8 0, i8* %62
%63 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i8* %44)
%64 = call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @write(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %64)
%66 = call i32 @fflush(i32* null)
%67 = call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%68 = icmp slt i32 %65, %67
br i1 %68, label %49, label %53
}