#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int Write_LCD(int fd, char *c, int size) {
  int rc, wrote = 0;
  int retries = 30;
  do {
    rc = write(fd, c, size);
    if (rc > 0) {
      c += rc;
      size -= rc;
      wrote += rc;
    } else if (rc == 0 || (rc < 0 && errno == EAGAIN)) {
      usleep(2000);
    } else {
      break;
    }
  } while (size > 0 && --retries > 0);
  return wrote;
}


x86 assembly (clang, -O0):
<Write_LCD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1e,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1186 <Write_LCD+0x66>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11cd <Write_LCD+0xad>
cmpl   $0x0,-0x18(%rbp)
je     11b4 <Write_LCD+0x94>
cmpl   $0x0,-0x18(%rbp)
jge    11c3 <Write_LCD+0xa3>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11c3 <Write_LCD+0xa3>
mov    $0x7d0,%edi
call   1040 <usleep@plt>
jmp    11c8 <Write_LCD+0xa8>
jmp    11fe <Write_LCD+0xde>
jmp    11cd <Write_LCD+0xad>
jmp    11d2 <Write_LCD+0xb2>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jle    11f3 <Write_LCD+0xd3>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1140 <Write_LCD+0x20>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Write_LCD>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x1e,%r12d
xor    %r14d,%r14d
mov    0x2e8d(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1180 <Write_LCD+0x60>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
add    %eax,%r14d
test   %ebp,%ebp
jg     11a0 <Write_LCD+0x80>
jmp    11ae <Write_LCD+0x8e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
je     1192 <Write_LCD+0x72>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    11ae <Write_LCD+0x8e>
mov    $0x7d0,%edi
call   1040 <usleep@plt>
test   %ebp,%ebp
jle    11ae <Write_LCD+0x8e>
lea    -0x1(%r12),%eax
cmp    $0x1,%r12d
mov    %eax,%r12d
ja     1150 <Write_LCD+0x30>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @Write_LCD(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
store i32 30, i32* %9
br label %10
10: ; preds = %50, %3
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = call i32 @write(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %10
%18 = load i32, i32* %7
%19 = load i8*, i8** %5
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8* %21, i8** %5
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, %22
store i32 %24, i32* %6
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = add nsw i32 %26, %25
store i32 %27, i32* %8
br label %42
28: ; preds = %10
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %28
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EAGAIN
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34, %28
%39 = call i32 @usleep(i32 2000)
br label %41
40: ; preds = %34, %31
br label %52
41: ; preds = %38
br label %42
42: ; preds = %41, %17
br label %43
43: ; preds = %42
%44 = load i32, i32* %6
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i32, i32* %9
%48 = add nsw i32 %47, -1
store i32 %48, i32* %9
%49 = icmp sgt i32 %48, 0
br label %50
50: ; preds = %46, %43
%51 = phi i1 [ false, %43 ], [ %49, %46 ]
br i1 %51, label %10, label %52
52: ; preds = %50, %40
%53 = load i32, i32* %8
ret i32 %53
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @Write_LCD(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %24, %3
%5 = phi i32 [ %2, %3 ], [ %25, %24 ]
%6 = phi i8* [ %1, %3 ], [ %26, %24 ]
%7 = phi i32 [ 0, %3 ], [ %27, %24 ]
%8 = phi i32 [ 30, %3 ], [ %29, %24 ]
%9 = tail call i32 @write(i32 %0, i8* %6, i32 %5)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %6, i64 %12
%14 = sub nsw i32 %5, %9
%15 = add nsw i32 %9, %7
br label %24
16: ; preds = %4
%17 = icmp eq i32 %9, 0
br i1 %17, label %22, label %18
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 %32
22: ; preds = %18, %16
%23 = tail call i32 @usleep(i32 2000)
br label %24
24: ; preds = %11, %22
%25 = phi i32 [ %14, %11 ], [ %5, %22 ]
%26 = phi i8* [ %13, %11 ], [ %6, %22 ]
%27 = phi i32 [ %15, %11 ], [ %7, %22 ]
%28 = icmp sgt i32 %25, 0
%29 = add nsw i32 %8, -1
%30 = icmp ugt i32 %8, 1
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %4, label %32
32: ; preds = %24, %18
%33 = phi i32 [ %27, %24 ], [ %7, %18 ]
ret i32 %33
}