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

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int pwrite(int, void const *, size_t, int);
ssize_t safe_pwrite(int fd, const void *b, size_t c, off_t off) {
  int res;
  while (c > 0) {
    res = pwrite(fd, b, c, off);
    if (res < 0) {
      if (errno != EINTR)
        return -errno;
      continue;
    }
    c -= res;
    b = (char *)b + res;
    off += res;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_pwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ad <safe_pwrite+0x9d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <pwrite@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1181 <safe_pwrite+0x71>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%eax
je     117c <safe_pwrite+0x6c>
xor    %eax,%eax
mov    0x2e6e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <safe_pwrite+0xa4>
jmp    1126 <safe_pwrite+0x16>
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1126 <safe_pwrite+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_pwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdx,%rdx
je     1173 <safe_pwrite+0x63>
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r12d
mov    0x2eab(%rip),%r13
mov    0x2e9c(%rip),%r14
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <pwrite@plt>
test   %eax,%eax
jns    1160 <safe_pwrite+0x50>
mov    0x0(%r13),%eax
cmp    (%r14),%eax
je     1140 <safe_pwrite+0x30>
jmp    1171 <safe_pwrite+0x61>
mov    %eax,%ecx
add    %rcx,%rbp
add    %eax,%r15d
sub    %rcx,%rbx
jne    1140 <safe_pwrite+0x30>
xor    %eax,%eax
jmp    1173 <safe_pwrite+0x63>
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
define i32 @safe_pwrite(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %25, label %6
6: ; preds = %4, %19
%7 = phi i32 [ %23, %19 ], [ %3, %4 ]
%8 = phi i64 [ %21, %19 ], [ %2, %4 ]
%9 = phi i8* [ %22, %19 ], [ %1, %4 ]
br label %10
10: ; preds = %6, %13
%11 = tail call i32 @pwrite(i32 %0, i8* %9, i64 %8, i32 %7)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINTR
%16 = icmp eq i32 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = sub nsw i32 0, %14
br label %25
19: ; preds = %10
%20 = zext i32 %11 to i64
%21 = sub i64 %8, %20
%22 = getelementptr inbounds i8, i8* %9, i64 %20
%23 = add nsw i32 %11, %7
%24 = icmp eq i64 %21, 0
br i1 %24, label %25, label %6
25: ; preds = %19, %4, %17
%26 = phi i32 [ %18, %17 ], [ 0, %4 ], [ 0, %19 ]
ret i32 %26
}