#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*/ uint8_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
size_t SSIZE_MAX;
scalar_t__ errno;
int /*<<< orphan*/ ssh_set_error(char *);
int write(int, int /*<<< orphan*/ const *, int);
ssize_t ssh_net_write(int sock, const void *data, size_t len) {
  ssize_t len_left;
  const uint8_t *p;
  if (len > SSIZE_MAX) {
    ssh_set_error("write too large");
    errno = 0;
    return -1;
  }
  p = data;
  len_left = len;
  while (len_left > 0) {
    ssize_t s = write(sock, p, len_left);
    if (s < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EWOULDBLOCK || errno == EAGAIN)
        return len - len_left;
      ssh_set_error("write error");
      return -1;
    }
    if (s == 0)
      return len - len_left;
    p += s;
    len_left -= s;
  }
  return len;
}


x86 assembly (clang, -O0):
<ssh_net_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2eaa(%rip),%rcx
cmp    (%rcx),%rax
jbe    116d <ssh_net_write+0x4d>
lea    0xeb2(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    0x2e7e(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <ssh_net_write+0x149>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1262 <ssh_net_write+0x142>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1220 <ssh_net_write+0x100>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <ssh_net_write+0xa1>
jmp    117c <ssh_net_write+0x5c>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <ssh_net_write+0xd5>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <ssh_net_write+0xe8>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1269 <ssh_net_write+0x149>
lea    0xe01(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <ssh_net_write+0x149>
cmpl   $0x0,-0x2c(%rbp)
jne    123d <ssh_net_write+0x11d>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1269 <ssh_net_write+0x149>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <ssh_net_write+0x5c>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssh_net_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2eb6(%rip),%rax
cmp    %rdx,(%rax)
jae    115c <ssh_net_write+0x3c>
lea    0xec2(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    0x2e8e(%rip),%rax
movq   $0x0,(%rax)
mov    $0xffffffff,%r14d
jmp    11de <ssh_net_write+0xbe>
mov    %rdx,%r14
test   %r14d,%r14d
jle    11de <ssh_net_write+0xbe>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e67(%rip),%r12
mov    0x2e50(%rip),%r13
mov    %r14d,%ebp
jmp    118e <ssh_net_write+0x6e>
nopl   (%rax)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11af <ssh_net_write+0x8f>
test   %ebp,%ebp
jle    11de <ssh_net_write+0xbe>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1180 <ssh_net_write+0x60>
je     11db <ssh_net_write+0xbb>
mov    %eax,%ecx
lea    (%rbx,%rcx,4),%rbx
sub    %eax,%ebp
test   %ebp,%ebp
jg     118e <ssh_net_write+0x6e>
jmp    11de <ssh_net_write+0xbe>
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
je     11db <ssh_net_write+0xbb>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     11db <ssh_net_write+0xbb>
lea    0xe42(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    $0xffffffff,%r14d
jmp    11de <ssh_net_write+0xbe>
sub    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SSIZE_MAX = global i64 0
@.str = [16 x i8] c"write too large\00"
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.1 = [12 x i8] c"write error\00"
define i32 @ssh_net_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
%12 = load i64, i64* @SSIZE_MAX
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @ssh_set_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
store i32 -1, i32* %4
br label %72
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = bitcast i8* %17 to i32*
store i32* %18, i32** %9
%19 = load i64, i64* %7
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
br label %21
21: ; preds = %61, %35, %16
%22 = load i32, i32* %8
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %69
24: ; preds = %21
%25 = load i32, i32* %5
%26 = load i32*, i32** %9
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %25, i32* %26, i32 %27)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %52
31: ; preds = %24
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %21
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EWOULDBLOCK
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = load i64, i64* @EAGAIN
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %40, %36
%45 = load i64, i64* %7
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = sub i64 %45, %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %4
br label %72
50: ; preds = %40
%51 = call i32 @ssh_set_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %72
52: ; preds = %24
%53 = load i32, i32* %10
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i64, i64* %7
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = sub i64 %56, %58
%60 = trunc i64 %59 to i32
store i32 %60, i32* %4
br label %72
61: ; preds = %52
%62 = load i32, i32* %10
%63 = load i32*, i32** %9
%64 = sext i32 %62 to i64
%65 = getelementptr inbounds i32, i32* %63, i64 %64
store i32* %65, i32** %9
%66 = load i32, i32* %10
%67 = load i32, i32* %8
%68 = sub nsw i32 %67, %66
store i32 %68, i32* %8
br label %21
69: ; preds = %21
%70 = load i64, i64* %7
%71 = trunc i64 %70 to i32
store i32 %71, i32* %4
br label %72
72: ; preds = %69, %55, %50, %44, %14
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
@SSIZE_MAX = global i64 0
@.str = [16 x i8] c"write too large\00"
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.1 = [12 x i8] c"write error\00"
define i32 @ssh_net_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = load i64, i64* @SSIZE_MAX
%5 = icmp ult i64 %4, %2
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @ssh_set_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
br label %44
8: ; preds = %3
%9 = trunc i64 %2 to i32
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %44
11: ; preds = %8
%12 = bitcast i8* %1 to i32*
br label %13
13: ; preds = %11, %40
%14 = phi i32* [ %42, %40 ], [ %12, %11 ]
%15 = phi i32 [ %41, %40 ], [ %9, %11 ]
%16 = tail call i32 @write(i32 %0, i32* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %40, label %22
22: ; preds = %18
%23 = load i64, i64* @EWOULDBLOCK
%24 = icmp eq i64 %19, %23
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %19, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = sub i32 %9, %15
br label %44
30: ; preds = %22
%31 = tail call i32 @ssh_set_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %44
32: ; preds = %13
%33 = icmp eq i32 %16, 0
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = sub i32 %9, %15
br label %44
36: ; preds = %32
%37 = zext i32 %16 to i64
%38 = getelementptr inbounds i32, i32* %14, i64 %37
%39 = sub nsw i32 %15, %16
br label %40
40: ; preds = %18, %36
%41 = phi i32 [ %39, %36 ], [ %15, %18 ]
%42 = phi i32* [ %38, %36 ], [ %14, %18 ]
%43 = icmp sgt i32 %41, 0
br i1 %43, label %13, label %44
44: ; preds = %40, %8, %34, %30, %28, %6
%45 = phi i32 [ -1, %6 ], [ %35, %34 ], [ -1, %30 ], [ %29, %28 ], [ %9, %8 ], [ %9, %40 ]
ret i32 %45
}