#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 scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ SEEK_SET;
int errno;
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
scalar_t__ write(int, char *, size_t);
void dwrite_c(int fd, char *buffer, off_t offset, size_t length, int *iostat) {
  ssize_t nwrite;
  if ((off_t)lseek(fd, (off_t)(offset), SEEK_SET) < 0) {
    *iostat = errno;
    return;
  }
  while (length) {
    nwrite = write(fd, buffer, length);
    if (nwrite < 0) {
      if (errno == EINTR)
        nwrite = 0;
      else {
        *iostat = errno;
        return;
      }
    }
    length -= nwrite;
  }
}


x86 assembly (clang, -O0):
<dwrite_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    116e <dwrite_c+0x4e>
mov    0x2e7f(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ef <dwrite_c+0xcf>
jmp    1173 <dwrite_c+0x53>
cmpq   $0x0,-0x20(%rbp)
je     11ef <dwrite_c+0xcf>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11db <dwrite_c+0xbb>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%eax
jne    11c2 <dwrite_c+0xa2>
movq   $0x0,-0x30(%rbp)
jmp    11d6 <dwrite_c+0xb6>
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ef <dwrite_c+0xcf>
jmp    11db <dwrite_c+0xbb>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1173 <dwrite_c+0x53>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dwrite_c>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %rdx,%rsi
mov    %eax,%edx
call   1040 <lseek@plt>
test   %rax,%rax
js     1195 <dwrite_c+0x75>
test   %rbx,%rbx
je     11a1 <dwrite_c+0x81>
mov    0x2e86(%rip),%r12
mov    0x2e6f(%rip),%r13
jmp    1177 <dwrite_c+0x57>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
sub    %rax,%rbx
je     11a1 <dwrite_c+0x81>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1172 <dwrite_c+0x52>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     1170 <dwrite_c+0x50>
jmp    119e <dwrite_c+0x7e>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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