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

/* Variables and functions */
int write(int, void const *, int);
ssize_t secureWrite(int fd, const void *buf, ssize_t txLen) {
  int i, acc = 0;
  while (acc < txLen) {
    i = write(fd, buf + acc, txLen - acc);
    if (i <= 0)
      return i;
    acc += i;
  }
  return (ssize_t)acc;
}


x86 assembly (clang, -O0):
<secureWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1174 <secureWrite+0x64>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1166 <secureWrite+0x56>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117a <secureWrite+0x6a>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <secureWrite+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secureWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    114f <secureWrite+0x3f>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    1153 <secureWrite+0x43>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1130 <secureWrite+0x20>
jmp    1155 <secureWrite+0x45>
xor    %ebx,%ebx
jmp    1155 <secureWrite+0x45>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @secureWrite(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* %9
br label %10
10: ; preds = %28, %3
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %9
%22 = sub nsw i32 %20, %21
%23 = call i32 @write(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = load i32, i32* %8
store i32 %27, i32* %4
br label %34
28: ; preds = %14
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = add nsw i32 %30, %29
store i32 %31, i32* %9
br label %10
32: ; preds = %10
%33 = load i32, i32* %9
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %26
%35 = load i32, i32* %4
ret i32 %35
}

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