#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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
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*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int send_to_server(char *server_name, int portno, char *send_buffer,
                   char *receive_buffer, int max_buf) {
  int sockfd;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  int n;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Client cannot create socket");
    return -1;
  }
  if ((server = gethostbyname(server_name)) == NULL) {
    perror("Client cannot gethostbyname");
    return -1;
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portno);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("Client: ERROR connecting");
    exit(1);
  }
  if (write(sockfd, send_buffer, strlen(send_buffer)) < 0) {
    printf("Client: error writing to server\n");
    return -1;
  }
  *receive_buffer = '\0';
  n = read(sockfd, receive_buffer, max_buf - 1);
  if (n >= 0)
    receive_buffer[n] = '\0';
  close(sockfd);
  return n;
}


x86 assembly (clang, -O0):
<send_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    1228 <send_to_server+0x58>
lea    0xde9(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <send_to_server+0x17f>
mov    -0x10(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    1257 <send_to_server+0x87>
lea    0xdd6(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <send_to_server+0x17f>
lea    -0x40(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    12c8 <send_to_server+0xf8>
lea    0xd7f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    130f <send_to_server+0x13f>
lea    0xd53(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <send_to_server+0x17f>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
sub    $0x1,%edx
call   1080 <read@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jl     1341 <send_to_server+0x171>
mov    -0x28(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_to_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,0xc(%rsp)
mov    %rcx,0x20(%rsp)
mov    %rdx,%r13
mov    %esi,%r12d
mov    %rdi,%rbp
mov    0x2df0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12cf <send_to_server+0xff>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     12d8 <send_to_server+0x108>
mov    %rax,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rbp),%rsi
lea    0x14(%rsp),%rdi
mov    0x0(%rbp),%edx
call   10a0 <memmove@plt>
mov    %r12d,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     12fa <send_to_server+0x12a>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
js     12c1 <send_to_server+0xf1>
mov    0x20(%rsp),%rbx
movb   $0x0,(%rbx)
mov    0xc(%rsp),%edx
add    $0xffffffff,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12b7 <send_to_server+0xe7>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r15d,%edi
call   1070 <close@plt>
jmp    12e9 <send_to_server+0x119>
lea    0xd89(%rip),%rdi
call   1030 <puts@plt>
jmp    12e4 <send_to_server+0x114>
lea    0xd2a(%rip),%rdi
jmp    12df <send_to_server+0x10f>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd37(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"Client cannot create socket\00"
@.str.1 = [28 x i8] c"Client cannot gethostbyname\00"
@.str.2 = [25 x i8] c"Client: ERROR connecting\00"
@.str.3 = [33 x i8] c"Client: error writing to server\0A\00"
define i32 @send_to_server(i8* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca %struct.hostent*
%15 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%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* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %81
22: ; preds = %5
%23 = load i8*, i8** %7
%24 = call %struct.hostent* @gethostbyname(i8* %23)
store %struct.hostent* %24, %struct.hostent** %14
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %81
28: ; preds = %22
%29 = bitcast %struct.sockaddr_in* %13 to i8*
%30 = call i32 @bzero(i8* %29, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %31, i32* %32
%33 = load %struct.hostent*, %struct.hostent** %14
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to i8*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
%39 = bitcast i32* %38 to i8*
%40 = load %struct.hostent*, %struct.hostent** %14
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @bcopy(i8* %36, i8* %39, i32 %42)
%44 = load i32, i32* %8
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %12
%48 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%49 = call i64 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %28
%52 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %28
%55 = load i32, i32* %12
%56 = load i8*, i8** %9
%57 = load i8*, i8** %9
%58 = call i32 @strlen(i8* %57)
%59 = call i64 @write(i32 %55, i8* %56, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %81
63: ; preds = %54
%64 = load i8*, i8** %10
store i8 0, i8* %64
%65 = load i32, i32* %12
%66 = load i8*, i8** %10
%67 = load i32, i32* %11
%68 = sub nsw i32 %67, 1
%69 = call i32 @read(i32 %65, i8* %66, i32 %68)
store i32 %69, i32* %15
%70 = load i32, i32* %15
%71 = icmp sge i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %63
%73 = load i8*, i8** %10
%74 = load i32, i32* %15
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
br label %77
77: ; preds = %72, %63
%78 = load i32, i32* %12
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %15
store i32 %80, i32* %6
br label %81
81: ; preds = %77, %61, %26, %20
%82 = load i32, i32* %6
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"Client cannot create socket\00"
@.str.1 = [28 x i8] c"Client cannot gethostbyname\00"
@.str.2 = [25 x i8] c"Client: ERROR connecting\00"
@str = [32 x i8] c"Client: error writing to server\00"
define i32 @send_to_server(i8* %0, i32 %1, i8* %2, i8* nocapture %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %53
14: ; preds = %5
%15 = tail call %struct.hostent* @gethostbyname(i8* %0)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %53
19: ; preds = %14
%20 = call i32 @bzero(i8* %7, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to i8*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
%27 = bitcast i32* %26 to i8*
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%29 = load i32, i32* %28
%30 = tail call i32 @htons(i32 %1)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i64 @connect(i32 %10, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %19
%39 = call i32 @strlen(i8* %2)
%40 = call i64 @write(i32 %10, i8* %2, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %53
44: ; preds = %38
store i8 0, i8* %3
%45 = add nsw i32 %4, -1
%46 = call i32 @read(i32 %10, i8* %3, i32 %45)
%47 = icmp sgt i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = zext i32 %46 to i64
%50 = getelementptr inbounds i8, i8* %3, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %48, %44
%52 = call i32 @close(i32 %10)
br label %53
53: ; preds = %51, %42, %17, %12
%54 = phi i32 [ -1, %12 ], [ -1, %17 ], [ -1, %42 ], [ %46, %51 ]
ret i32 %54
}