#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sock_check_opened(int);
scalar_t__ write(int, char *, int);
int sock_data_write(int socket, char *buf, int len) {
  ssize_t size;
  int wlen = 0;
  if ((socket < 0) || (buf == NULL))
    return -1;
  if (!sock_check_opened(socket))
    return -1;
  while (len) {
    size = write(socket, buf, len);
    if (size <= 0) {
      printf("error writing to socket %d\n", socket);
      return -1;
    }
    len -= size;
    wlen += size;
    buf += size;
  }
  return wlen;
}


x86 assembly (clang, -O0):
<sock_data_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     115e <sock_data_write+0x2e>
cmpq   $0x0,-0x10(%rbp)
jne    116a <sock_data_write+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <sock_data_write+0xd2>
mov    -0x8(%rbp),%edi
call   1050 <sock_check_opened@plt>
cmp    $0x0,%eax
jne    1187 <sock_data_write+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <sock_data_write+0xd2>
jmp    118c <sock_data_write+0x5c>
cmpl   $0x0,-0x14(%rbp)
je     11fc <sock_data_write+0xcc>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     11cf <sock_data_write+0x9f>
mov    -0x8(%rbp),%esi
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <sock_data_write+0xd2>
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    118c <sock_data_write+0x5c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_data_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
test   %edi,%edi
js     1193 <sock_data_write+0x63>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1193 <sock_data_write+0x63>
mov    %edx,%ebp
mov    %edi,%r14d
call   1050 <sock_check_opened@plt>
test   %eax,%eax
je     1193 <sock_data_write+0x63>
xor    %r15d,%r15d
test   %ebp,%ebp
je     1193 <sock_data_write+0x63>
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %rax,%rax
jle    117e <sock_data_write+0x4e>
add    %eax,%r15d
add    %rax,%rbx
sub    %eax,%ebp
jne    1160 <sock_data_write+0x30>
jmp    1193 <sock_data_write+0x63>
lea    0xe7b(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"error writing to socket %d\0A\00"
define i32 @sock_data_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 -1, i32* %4
br label %51
16: ; preds = %12
%17 = load i32, i32* %5
%18 = call i32 @sock_check_opened(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %16
store i32 -1, i32* %4
br label %51
21: ; preds = %16
br label %22
22: ; preds = %35, %21
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %49
25: ; preds = %22
%26 = load i32, i32* %5
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = call i64 @write(i32 %26, i8* %27, i32 %28)
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = icmp sle i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* %5
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 -1, i32* %4
br label %51
35: ; preds = %25
%36 = load i64, i64* %8
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = sub nsw i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %7
%41 = load i64, i64* %8
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = add nsw i64 %43, %41
%45 = trunc i64 %44 to i32
store i32 %45, i32* %9
%46 = load i64, i64* %8
%47 = load i8*, i8** %6
%48 = getelementptr inbounds i8, i8* %47, i64 %46
store i8* %48, i8** %6
br label %22
49: ; preds = %22
%50 = load i32, i32* %9
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %32, %20, %15
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [28 x i8] c"error writing to socket %d\0A\00"
define i32 @sock_data_write(i32 %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %0, 0
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %26, label %7
7: ; preds = %3
%8 = tail call i32 @sock_check_opened(i32 %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %7
%11 = icmp eq i32 %2, 0
br i1 %11, label %26, label %12
12: ; preds = %10, %20
%13 = phi i32 [ %23, %20 ], [ 0, %10 ]
%14 = phi i32 [ %22, %20 ], [ %2, %10 ]
%15 = phi i8* [ %24, %20 ], [ %1, %10 ]
%16 = tail call i64 @write(i32 %0, i8* %15, i32 %14)
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %26
20: ; preds = %12
%21 = trunc i64 %16 to i32
%22 = sub i32 %14, %21
%23 = add i32 %13, %21
%24 = getelementptr inbounds i8, i8* %15, i64 %16
%25 = icmp eq i32 %22, 0
br i1 %25, label %26, label %12
26: ; preds = %20, %10, %7, %3, %18
%27 = phi i32 [ -1, %18 ], [ -1, %3 ], [ -1, %7 ], [ 0, %10 ], [ %23, %20 ]
ret i32 %27
}