#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;
scalar_t__ errno;
size_t strlen(char const *);
scalar_t__ write(int, char const *, size_t);
void sendcommand(int fd, const char *buf) {
  ssize_t out_count;
  size_t total = 0;
  unsigned errs = 10;
  size_t count = strlen(buf);
  while (count > 0) {
    out_count = write(fd, buf, count);
    if (out_count <= 0) {
      if (errno == EINTR || errno == EAGAIN) {
        errs--;
        if (errs != 0)
          continue;
      }
      break;
    }
    errs = 10;
    count -= out_count;
    buf += out_count;
    total += out_count;
  }
}


x86 assembly (clang, -O0):
<sendcommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xa,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11fe <sendcommand+0xde>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     11cb <sendcommand+0xab>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <sendcommand+0x89>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <sendcommand+0xa6>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c1 <sendcommand+0xa1>
jmp    114b <sendcommand+0x2b>
jmp    11c6 <sendcommand+0xa6>
jmp    11fe <sendcommand+0xde>
movl   $0xa,-0x24(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <sendcommand+0x2b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendcommand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     11a8 <sendcommand+0x88>
mov    %rax,%rbp
mov    0x2e90(%rip),%r12
mov    0x2e79(%rip),%r13
mov    $0xfffffff7,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
jg     11a0 <sendcommand+0x80>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
setne  %cl
cmp    0x0(%r13),%rax
setne  %al
test   %al,%cl
jne    11a8 <sendcommand+0x88>
lea    0x1(%r15),%eax
test   %r15d,%r15d
mov    %eax,%r15d
jne    1160 <sendcommand+0x40>
jmp    11a8 <sendcommand+0x88>
nopl   (%rax)
add    %rax,%rbx
sub    %rax,%rbp
jne    114f <sendcommand+0x2f>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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