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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ die(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ usleep(int);
scalar_t__ write(int, void const *, size_t);
void write_blocking(int fd, const void *buf, size_t count) {
  ssize_t ret;
  int timeout = 0;
  while ((ret = write(fd, buf, count)) < (ssize_t)count) {
    if (ret < 0) {
      if (errno == EAGAIN) {
        if (++timeout > 10) {
          fprintf(stderr, "Packet timeout\n");
          break;
        }
        usleep(3000);
        continue;
      } else if (errno == EINTR) {
        continue;
      } else {
        die("write to device failed: %s\n", strerror(errno));
      }
    } else {
      buf = (const char *)buf + ret;
      count -= ret;
    }
  }
}


x86 assembly (clang, -O0):
<write_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    -0x18(%rbp),%rax
jge    124e <write_blocking+0xfe>
cmpq   $0x0,-0x20(%rbp)
jge    122e <write_blocking+0xde>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <write_blocking+0x98>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmp    $0xa,%eax
jle    11d9 <write_blocking+0x89>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1040 <fprintf@plt>
jmp    124e <write_blocking+0xfe>
mov    $0xbb8,%edi
call   1070 <usleep@plt>
jmp    116a <write_blocking+0x1a>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
jne    1207 <write_blocking+0xb7>
jmp    116a <write_blocking+0x1a>
mov    0x2dca(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xdf1(%rip),%rdi
call   1050 <die@plt>
jmp    1229 <write_blocking+0xd9>
jmp    1249 <write_blocking+0xf9>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    116a <write_blocking+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_blocking>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %eax,%eax
mov    0x2e6b(%rip),%r13
mov    0x2e4c(%rip),%r15
mov    0x2e55(%rip),%r12
cmp    $0xb,%eax
mov    $0xa,%ecx
mov    %eax,(%rsp)
cmovge %eax,%ecx
mov    %ecx,0x4(%rsp)
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
mov    %rbp,%rcx
sub    %rax,%rcx
jle    120b <write_blocking+0xbb>
test   %rax,%rax
jns    11e0 <write_blocking+0x90>
mov    0x0(%r13),%rdi
cmp    (%r15),%rdi
je     11bc <write_blocking+0x6c>
cmp    (%r12),%rdi
je     1190 <write_blocking+0x40>
jmp    11f0 <write_blocking+0xa0>
mov    (%rsp),%eax
cmp    0x4(%rsp),%eax
je     121a <write_blocking+0xca>
add    $0x1,%eax
mov    %eax,(%rsp)
mov    $0xbb8,%edi
call   1070 <usleep@plt>
jmp    1190 <write_blocking+0x40>
nopw   0x0(%rax,%rax,1)
add    %rax,%rbx
mov    %rcx,%rbp
mov    (%rsp),%eax
jmp    117b <write_blocking+0x2b>
nopl   0x0(%rax,%rax,1)
call   1060 <strerror@plt>
lea    0xe14(%rip),%rdi
mov    %eax,%esi
call   1050 <die@plt>
mov    (%rsp),%eax
jmp    117b <write_blocking+0x2b>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"Packet timeout\0A\00"
@EINTR = global i64 0
@.str.1 = [28 x i8] c"write to device failed: %s\0A\00"
define void @write_blocking(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %8
br label %9
9: ; preds = %50, %36, %30, %3
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = call i64 @write(i32 %10, i8* %11, i64 %12)
store i64 %13, i64* %7
%14 = load i64, i64* %6
%15 = icmp slt i64 %13, %14
br i1 %15, label %16, label %51
16: ; preds = %9
%17 = load i64, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %43
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = icmp sgt i32 %25, 10
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %51
30: ; preds = %23
%31 = call i32 @usleep(i32 3000)
br label %9
32: ; preds = %19
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
br label %9
37: ; preds = %32
%38 = load i64, i64* @errno
%39 = call i32 @strerror(i64 %38)
%40 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %37
br label %42
42: ; preds = %41
br label %50
43: ; preds = %16
%44 = load i8*, i8** %5
%45 = load i64, i64* %7
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %5
%47 = load i64, i64* %7
%48 = load i64, i64* %6
%49 = sub i64 %48, %47
store i64 %49, i64* %6
br label %50
50: ; preds = %43, %42
br label %9
51: ; preds = %27, %9
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"Packet timeout\0A\00"
@EINTR = global i64 0
@.str.1 = [28 x i8] c"write to device failed: %s\0A\00"
define void @write_blocking(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %36, %3
%5 = phi i64 [ %38, %36 ], [ %2, %3 ]
%6 = phi i8* [ %37, %36 ], [ %1, %3 ]
%7 = phi i32 [ %12, %36 ], [ 0, %3 ]
br label %8
8: ; preds = %4, %33
%9 = phi i32 [ %12, %33 ], [ %7, %4 ]
%10 = call i32 @llvm.smax.i32(i32 %9, i32 10)
br label %11
11: ; preds = %8, %27
%12 = phi i32 [ %9, %8 ], [ %28, %27 ]
br label %13
13: ; preds = %11, %30
%14 = tail call i64 @write(i32 %0, i8* %6, i64 %5)
%15 = icmp sgt i64 %5, %14
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = icmp slt i64 %14, 0
br i1 %17, label %18, label %36
18: ; preds = %16
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = icmp eq i32 %12, %10
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %39
27: ; preds = %22
%28 = add i32 %12, 1
%29 = tail call i32 @usleep(i32 3000)
br label %11
30: ; preds = %18
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %19, %31
br i1 %32, label %13, label %33
33: ; preds = %30
%34 = tail call i32 @strerror(i64 %19)
%35 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %34)
br label %8
36: ; preds = %16
%37 = getelementptr inbounds i8, i8* %6, i64 %14
%38 = sub i64 %5, %14
br label %4
39: ; preds = %13, %24
ret void
}