#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 */
typedef int /*<<< orphan*/ len1;

/* Variables and functions */
scalar_t__ bsend(int, void *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect_sock(int, char *, int);
int /*<<< orphan*/ exit(int);
int htonl(int);
int new_sock();
int /*<<< orphan*/ perror(char *);
void send_handle_buffer(char *server, void *ptr, int len) {
  int sockfd = new_sock(), len1;
  if (sockfd < 0) {
    perror("new_sock: failed");
    exit(1);
  }
  if (connect_sock(sockfd, server, 4567) < 0) {
    perror("connect: failed");
    close(sockfd);
    exit(1);
  }
  len1 = htonl(len);
  if (bsend(sockfd, &len1, sizeof(len1)) < 0) {
    perror("bsend1: failed");
    close(sockfd);
    exit(1);
  }
  if (bsend(sockfd, ptr, len) < 0) {
    perror("bsend2: failed");
    close(sockfd);
    exit(1);
  }
  close(sockfd);
}


x86 assembly (clang, -O0):
<send_handle_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1030 <new_sock@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ad <send_handle_buffer+0x3d>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x11d7,%edx
call   1060 <connect_sock@plt>
cmp    $0x0,%rax
jge    11e6 <send_handle_buffer+0x76>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bsend@plt>
cmp    $0x0,%rax
jge    122a <send_handle_buffer+0xba>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <bsend@plt>
cmp    $0x0,%rax
jge    1261 <send_handle_buffer+0xf1>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_handle_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %eax,%eax
call   1030 <new_sock@plt>
test   %eax,%eax
js     11e7 <send_handle_buffer+0x77>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0x11d7,%edx
call   1060 <connect_sock@plt>
test   %rax,%rax
js     11fd <send_handle_buffer+0x8d>
mov    %r15d,%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <bsend@plt>
test   %rax,%rax
js     1206 <send_handle_buffer+0x96>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1080 <bsend@plt>
test   %rax,%rax
js     120f <send_handle_buffer+0x9f>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe0d(%rip),%rdi
jmp    1216 <send_handle_buffer+0xa6>
lea    0xe14(%rip),%rdi
jmp    1216 <send_handle_buffer+0xa6>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"new_sock: failed\00"
@.str.1 = [16 x i8] c"connect: failed\00"
@.str.2 = [15 x i8] c"bsend1: failed\00"
@.str.3 = [15 x i8] c"bsend2: failed\00"
define void @send_handle_buffer(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i32 (...) @new_sock()
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = load i32, i32* %7
%17 = load i8*, i8** %4
%18 = call i64 @connect_sock(i32 %16, i8* %17, i32 4567)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i32, i32* %6
%27 = call i32 @htonl(i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %7
%29 = bitcast i32* %8 to i8*
%30 = call i64 @bsend(i32 %28, i8* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %25
%38 = load i32, i32* %7
%39 = load i8*, i8** %5
%40 = load i32, i32* %6
%41 = call i64 @bsend(i32 %38, i8* %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %37
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"new_sock: failed\00"
@.str.1 = [16 x i8] c"connect: failed\00"
@.str.2 = [15 x i8] c"bsend1: failed\00"
@.str.3 = [15 x i8] c"bsend2: failed\00"
define void @send_handle_buffer(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = tail call i32 (...) @new_sock()
%6 = bitcast i32* %4 to i8*
%7 = icmp slt i32 %5, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i64 @connect_sock(i32 %5, i8* %0, i32 4567)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @close(i32 %5)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = tail call i32 @htonl(i32 %2)
store i32 %19, i32* %4
%20 = call i64 @bsend(i32 %5, i8* %6, i32 4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %5)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = call i64 @bsend(i32 %5, i8* %1, i32 %2)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @close(i32 %5)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = call i32 @close(i32 %5)
ret void
}