#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__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int write(int, unsigned char *, int);
int carmen_serial_writen(int dev_fd, unsigned char *buf, int nChars) {
  int amountWritten = 0;
  while (nChars > 0) {
    amountWritten = write(dev_fd, buf, nChars);
    if (amountWritten < 0) {
      if (errno == EWOULDBLOCK) {
        fprintf(stderr, "\nWARNING: writeN: EWOULDBLOCK: trying again!\n");
      } else {
        return -1;
      }
    } else {
      nChars -= amountWritten;
      buf += amountWritten;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<carmen_serial_writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11c5 <carmen_serial_writen+0xa5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a4 <carmen_serial_writen+0x84>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
jne    1193 <carmen_serial_writen+0x73>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1040 <fprintf@plt>
jmp    119f <carmen_serial_writen+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <carmen_serial_writen+0xac>
jmp    11c0 <carmen_serial_writen+0xa0>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1139 <carmen_serial_writen+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<carmen_serial_writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %edx,%edx
jle    1193 <carmen_serial_writen+0x73>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e98(%rip),%r13
mov    0x2e89(%rip),%r14
lea    0xeb2(%rip),%r12
jmp    115b <carmen_serial_writen+0x3b>
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
test   %ebp,%ebp
jle    118a <carmen_serial_writen+0x6a>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1150 <carmen_serial_writen+0x30>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    118e <carmen_serial_writen+0x6e>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1040 <fprintf@plt>
test   %ebp,%ebp
jg     115b <carmen_serial_writen+0x3b>
xor    %eax,%eax
jmp    1193 <carmen_serial_writen+0x73>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [46 x i8] c"\0AWARNING: writeN: EWOULDBLOCK: trying again!\0A\00"
define i32 @carmen_serial_writen(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %36, %3
%10 = load i32, i32* %7
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %37
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %27
26: ; preds = %19
store i32 -1, i32* %4
br label %38
27: ; preds = %23
br label %36
28: ; preds = %12
%29 = load i32, i32* %8
%30 = load i32, i32* %7
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %7
%32 = load i32, i32* %8
%33 = load i8*, i8** %6
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %6
br label %36
36: ; preds = %28, %27
br label %9
37: ; preds = %9
store i32 0, i32* %4
br label %38
38: ; preds = %37, %26
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [46 x i8] c"\0AWARNING: writeN: EWOULDBLOCK: trying again!\0A\00"
define i32 @carmen_serial_writen(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %25
5: ; preds = %3, %21
%6 = phi i32 [ %23, %21 ], [ %2, %3 ]
%7 = phi i8* [ %22, %21 ], [ %1, %3 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EWOULDBLOCK
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %21
17: ; preds = %5
%18 = sub nsw i32 %6, %8
%19 = zext i32 %8 to i64
%20 = getelementptr inbounds i8, i8* %7, i64 %19
br label %21
21: ; preds = %17, %14
%22 = phi i8* [ %7, %14 ], [ %20, %17 ]
%23 = phi i32 [ %6, %14 ], [ %18, %17 ]
%24 = icmp sgt i32 %23, 0
br i1 %24, label %5, label %25
25: ; preds = %10, %21, %3
%26 = phi i32 [ 0, %3 ], [ 0, %21 ], [ -1, %10 ]
ret i32 %26
}