#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*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(scalar_t__);
int readport(int fd, char *result) {
  int iIn = read(fd, result, 254);
  result[iIn - 1] = 0x00;
  if (iIn < 0) {
    if (errno == EAGAIN) {
      printf("SERIAL EAGAIN ERROR\n");
      return -1;
    } else {
      printf("SERIAL read error %d %s\n", errno, strerror(errno));
      return -1;
    }
  }
  return iIn;
}


x86 assembly (clang, -O0):
<readport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xfe,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x14(%rbp)
jge    11e0 <readport+0xb0>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
jne    11a2 <readport+0x72>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <readport+0xb6>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <readport+0xb6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readport>:
push   %rbx
mov    %rsi,%rbx
mov    $0xfe,%edx
call   1050 <read@plt>
movslq %eax,%rcx
movb   $0x0,-0x1(%rcx,%rbx,1)
test   %ecx,%ecx
js     115c <readport+0x1c>
pop    %rbx
ret
mov    0x2e7d(%rip),%rax
mov    (%rax),%rbx
mov    0x2e63(%rip),%rax
cmp    (%rax),%rbx
jne    1180 <readport+0x40>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
jmp    119c <readport+0x5c>
mov    %rbx,%rdi
call   1060 <strerror@plt>
lea    0xe71(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [21 x i8] c"SERIAL EAGAIN ERROR\0A\00"
@.str.1 = [25 x i8] c"SERIAL read error %d %s\0A\00"
define i32 @readport(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = load i8*, i8** %5
%9 = call i32 @read(i32 %7, i8* %8, i32 254)
store i32 %9, i32* %6
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 0, i8* %14
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %2
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %24, i8* %26)
store i32 -1, i32* %3
br label %30
28: ; preds = %2
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %30
30: ; preds = %28, %23, %21
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [25 x i8] c"SERIAL read error %d %s\0A\00"
@str = [20 x i8] c"SERIAL EAGAIN ERROR\00"
define i32 @readport(i32 %0, i8* nocapture %1) {
%3 = tail call i32 @read(i32 %0, i8* %1, i32 254)
%4 = add nsw i32 %3, -1
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 0, i8* %6
%7 = icmp slt i32 %3, 0
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAGAIN
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %17
14: ; preds = %8
%15 = tail call i8* @strerror(i64 %9)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %9, i8* %15)
br label %17
17: ; preds = %2, %14, %12
%18 = phi i32 [ -1, %12 ], [ -1, %14 ], [ %3, %2 ]
ret i32 %18
}