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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int strlen(char *);
int write(int, ...);
int write_to_socket(int sock, char *buffer) {
  int m_size = 0;
  m_size = strlen(buffer) + 1;
  if ((write(sock, &m_size, sizeof(int))) < sizeof(int)) {
    fprintf(stderr, "ERROR: could no write on socket\n");
    return -1;
  }
  if (write(sock, buffer, m_size) < 0) {
    fprintf(stderr, "ERROR: could no write on socket %d\n", sock);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
jae    1197 <write_to_socket+0x67>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <write_to_socket+0xae>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x0,%eax
jge    11d7 <write_to_socket+0xa7>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <write_to_socket+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,0xc(%rsp)
xor    %ebp,%ebp
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
mov    %r14d,%edi
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x3,%eax
ja     1181 <write_to_socket+0x51>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11b0 <write_to_socket+0x80>
mov    0xc(%rsp),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <write@plt>
test   %eax,%eax
jns    11b5 <write_to_socket+0x85>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"ERROR: could no write on socket\0A\00"
@.str.1 = [36 x i8] c"ERROR: could no write on socket %d\0A\00"
define i32 @write_to_socket(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%7 = load i8*, i8** %5
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
store i32 %9, i32* %6
%10 = load i32, i32* %4
%11 = call i32 (i32, ...) @write(i32 %10, i32* %6, i64 4)
%12 = sext i32 %11 to i64
%13 = icmp ult i64 %12, 4
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
17: ; preds = %2
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = call i32 (i32, ...) @write(i32 %18, i8* %19, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* %4
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 -1, i32* %3
br label %28
27: ; preds = %17
store i32 0, i32* %3
br label %28
28: ; preds = %27, %23, %14
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"ERROR: could no write on socket\0A\00"
@.str.1 = [36 x i8] c"ERROR: could no write on socket %d\0A\00"
define i32 @write_to_socket(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @strlen(i8* %1)
%6 = add nsw i32 %5, 1
store i32 %6, i32* %3
%7 = call i32 (i32, ...) @write(i32 %0, i32* %3, i64 4)
%8 = icmp ult i32 %7, 4
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %19
12: ; preds = %2
%13 = load i32, i32* %3
%14 = call i32 (i32, ...) @write(i32 %0, i8* %1, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %19
19: ; preds = %12, %16, %9
%20 = phi i32 [ -1, %9 ], [ -1, %16 ], [ 0, %12 ]
ret i32 %20
}