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

/* Variables and functions */
int safe_ssl_read(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int safe_ssl_read_all(SSL *ssl, uint8_t *buf, int bufsize) {
  int ret, n = 0;
  while (n < bufsize) {
    ret = safe_ssl_read(ssl, &buf[n], bufsize - n);
    if (ret == 0)
      continue;
    else if (ret < 0)
      return ret;
    n += ret;
  }
  return 1;
}


x86 assembly (clang, -O0):
<safe_ssl_read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118e <safe_ssl_read_all+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1030 <safe_ssl_read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1166 <safe_ssl_read_all+0x56>
jmp    112a <safe_ssl_read_all+0x1a>
cmpl   $0x0,-0x20(%rbp)
jge    117b <safe_ssl_read_all+0x6b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <safe_ssl_read_all+0x85>
jmp    1180 <safe_ssl_read_all+0x70>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    112a <safe_ssl_read_all+0x1a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_ssl_read_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%eax
test   %edx,%edx
jle    116a <safe_ssl_read_all+0x5a>
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r13
xor    %r14d,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <safe_ssl_read@plt>
test   %eax,%eax
je     1140 <safe_ssl_read_all+0x30>
js     116a <safe_ssl_read_all+0x5a>
add    %eax,%r14d
mov    %r12d,%ebp
movslq %r14d,%rax
lea    (%r15,%rax,4),%rbx
sub    %r14d,%ebp
jg     1140 <safe_ssl_read_all+0x30>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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