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

/* Variables and functions */
int SSL_write(int /*<<< orphan*/ *, char *, int);
int SSL_write_all(SSL *ssl, char *buf, int sz) {
  int written = 0;
  int n;
  while (written < sz) {
    n = SSL_write(ssl, buf + written, sz - written);
    if (n > 0)
      written += n;
    else if (n < 0)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<SSL_write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118b <SSL_write_all+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1030 <SSL_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    116b <SSL_write_all+0x5b>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1186 <SSL_write_all+0x76>
cmpl   $0x0,-0x24(%rbp)
jge    1181 <SSL_write_all+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <SSL_write_all+0x82>
jmp    1186 <SSL_write_all+0x76>
jmp    112a <SSL_write_all+0x1a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SSL_write_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1156 <SSL_write_all+0x46>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
jmp    1136 <SSL_write_all+0x26>
nopw   0x0(%rax,%rax,1)
add    %eax,%ebp
cmp    %ebx,%ebp
jge    1156 <SSL_write_all+0x46>
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15,%rdi
call   1030 <SSL_write@plt>
test   %eax,%eax
jg     1130 <SSL_write_all+0x20>
jns    1132 <SSL_write_all+0x22>
mov    $0xffffffff,%eax
jmp    1158 <SSL_write_all+0x48>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SSL_write_all(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %35, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %36
14: ; preds = %10
%15 = load i32*, i32** %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @SSL_write(i32* %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = add nsw i32 %28, %27
store i32 %29, i32* %8
br label %35
30: ; preds = %14
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 -1, i32* %4
br label %37
34: ; preds = %30
br label %35
35: ; preds = %34, %26
br label %10
36: ; preds = %10
store i32 0, i32* %4
br label %37
37: ; preds = %36, %33
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @SSL_write_all(i32* %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3, %16
%6 = phi i32 [ %17, %16 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @SSL_write(i32* %0, i8* %8, i32 %9)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = add nsw i32 %10, %6
br label %16
14: ; preds = %5
%15 = icmp slt i32 %10, 0
br i1 %15, label %19, label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %6, %14 ]
%18 = icmp slt i32 %17, %2
br i1 %18, label %5, label %19
19: ; preds = %14, %16, %3
%20 = phi i32 [ 0, %3 ], [ 0, %16 ], [ -1, %14 ]
ret i32 %20
}