#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
int xserial_port_write_packet(int serline, unsigned char *buffer, int cnt) {
  unsigned char c;
  int err, i = 0;
  c = buffer[0];
  while (1) {
    if (i >= cnt) {
      return cnt;
    }
    c = buffer[i++];
    err = write(serline, &c, 1);
    if (err < 0) {
      perror("error writing to serial port");
      exit(2);
    }
  }
}


x86 assembly (clang, -O0):
<xserial_port_write_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x15(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1167 <xserial_port_write_packet+0x37>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x20(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x15(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b3 <xserial_port_write_packet+0x83>
lea    0xe5c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
jmp    1152 <xserial_port_write_packet+0x22>

x86 assembly (clang, -O2):
<xserial_port_write_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%r13d
xor    %ebx,%ebx
test   %edx,%edx
mov    $0x0,%ebp
cmovg  %edx,%ebp
lea    0x7(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %rbx,%rbp
je     119c <xserial_port_write_packet+0x6c>
movzbl (%r12,%rbx,1),%eax
add    $0x1,%rbx
mov    %al,0x7(%rsp)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1160 <xserial_port_write_packet+0x30>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"error writing to serial port\00"
define i32 @xserial_port_write_packet(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%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* %9
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
store i8 %12, i8* %7
br label %13
13: ; preds = %3, %33
%14 = load i32, i32* %9
%15 = load i32, i32* %6
%16 = icmp sge i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* %6
ret i32 %18
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i32, i32* %9
%22 = add nsw i32 %21, 1
store i32 %22, i32* %9
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* %7
%26 = load i32, i32* %4
%27 = call i32 @write(i32 %26, i8* %7, i32 1)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %19
br label %13
}

LLVM IR (-O2):
@.str = [29 x i8] c"error writing to serial port\00"
define i32 @xserial_port_write_packet(i32 %0, i8* nocapture %1, i32 returned %2) {
%4 = alloca i8
%5 = call i32 @llvm.smax.i32(i32 %2, i32 0)
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %11, %3
%8 = phi i64 [ %12, %11 ], [ 0, %3 ]
%9 = icmp eq i64 %8, %6
br i1 %9, label %10, label %11
10: ; preds = %7
ret i32 %2
11: ; preds = %7
%12 = add nuw nsw i64 %8, 1
%13 = getelementptr inbounds i8, i8* %1, i64 %8
%14 = load i8, i8* %13
store i8 %14, i8* %4
%15 = call i32 @write(i32 %0, i8* %4, i32 1)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 2)
unreachable
}