#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*/ opal_output(int /*<<< orphan*/, char *);
int write(int, char const *, int);
int write_all(int socket_id, const char *buffer, int size) {
  int bytes_written = 0;
  int retval;
  if (size < 0) {
    opal_output(0, "size is <= 0, please check.\n");
    return -1;
  }
  if (!buffer) {
    opal_output(0, "buffer is null, will return.\n");
    return 0;
  }
  while (bytes_written < size) {
    retval = write(socket_id, buffer + bytes_written, size - bytes_written);
    if (retval >= 0) {
      bytes_written += retval;
    } else {
      opal_output(0, "error in writting data.\n");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    115d <write_all+0x3d>
xor    %edi,%edi
lea    0xeb4(%rip),%rsi
call   1040 <opal_output@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <write_all+0xcd>
cmpq   $0x0,-0x10(%rbp)
jne    1182 <write_all+0x62>
xor    %edi,%edi
lea    0xeac(%rip),%rsi
call   1040 <opal_output@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ed <write_all+0xcd>
jmp    1187 <write_all+0x67>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e6 <write_all+0xc6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11c7 <write_all+0xa7>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11e1 <write_all+0xc1>
xor    %edi,%edi
lea    0xe6b(%rip),%rsi
call   1040 <opal_output@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <write_all+0xcd>
jmp    1187 <write_all+0x67>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
test   %edx,%edx
js     1172 <write_all+0x52>
mov    %rsi,%r13
test   %rsi,%rsi
je     117b <write_all+0x5b>
mov    %edx,%ebp
test   %edx,%edx
je     116d <write_all+0x4d>
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xeec(%rip),%r14
nop
movslq %ebx,%rax
lea    (%rax,%r13,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     1185 <write_all+0x65>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1150 <write_all+0x30>
xor    %r15d,%r15d
jmp    118f <write_all+0x6f>
lea    0xe87(%rip),%r14
jmp    1185 <write_all+0x65>
xor    %r15d,%r15d
lea    0xe98(%rip),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <opal_output@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"size is <= 0, please check.\0A\00"
@.str.1 = [30 x i8] c"buffer is null, will return.\0A\00"
@.str.2 = [25 x i8] c"error in writting data.\0A\00"
define i32 @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
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @opal_output(i32 0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @opal_output(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %44
19: ; preds = %14
br label %20
20: ; preds = %42, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = call i32 @write(i32 %25, i8* %29, i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %24
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = add nsw i32 %38, %37
store i32 %39, i32* %8
br label %42
40: ; preds = %24
%41 = call i32 @opal_output(i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
42: ; preds = %36
br label %20
43: ; preds = %20
store i32 0, i32* %4
br label %44
44: ; preds = %43, %40, %17, %12
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@.str = [29 x i8] c"size is <= 0, please check.\0A\00"
@.str.1 = [30 x i8] c"buffer is null, will return.\0A\00"
@.str.2 = [25 x i8] c"error in writting data.\0A\00"
define i32 @write_all(i32 %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3
%6 = icmp eq i8* %1, null
br i1 %6, label %19, label %7
7: ; preds = %5
%8 = icmp eq i32 %2, 0
br i1 %8, label %23, label %11
9: ; preds = %11
%10 = icmp slt i32 %18, %2
br i1 %10, label %11, label %23
11: ; preds = %7, %9
%12 = phi i32 [ %18, %9 ], [ 0, %7 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = sub nsw i32 %2, %12
%16 = tail call i32 @write(i32 %0, i8* %14, i32 %15)
%17 = icmp sgt i32 %16, -1
%18 = add nsw i32 %16, %12
br i1 %17, label %9, label %19
19: ; preds = %11, %5, %3
%20 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %11 ]
%21 = phi i32 [ -1, %3 ], [ 0, %5 ], [ -1, %11 ]
%22 = tail call i32 @opal_output(i32 0, i8* %20)
br label %23
23: ; preds = %9, %19, %7
%24 = phi i32 [ 0, %7 ], [ %21, %19 ], [ 0, %9 ]
ret i32 %24
}