#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 */
struct timeval {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
long read(int, char *, long);
char *strerror(scalar_t__);
long int portread2(int fd, char *data) {
  long int n = 0, len = 1;
  long int numport = 0;
  char com1;
  fd_set fds;
  struct timeval timeout;
  *data = '?';
  n = read(fd, data, len);
  if (n < 0) {
    if (errno == EAGAIN) {
      printf("SERIAL EAGAIN ERROR\n");
      return 0;
    } else {
      printf("SERIAL read error %d %s\n", errno, strerror(errno));
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<portread2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11f1 <portread2+0xc1>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <portread2+0x82>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <portread2+0xc9>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <portread2+0xc9>
movq   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portread2>:
push   %r14
push   %rbx
push   %rax
movb   $0x3f,(%rsi)
mov    $0x1,%ebx
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jns    119f <portread2+0x5f>
mov    0x2e7e(%rip),%rax
mov    (%rax),%r14
mov    0x2e64(%rip),%rax
cmp    (%rax),%r14
jne    1181 <portread2+0x41>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    119f <portread2+0x5f>
mov    %r14,%rdi
call   1060 <strerror@plt>
lea    0xe70(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@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 i64 @portread2(i32 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 0, i64* %6
store i64 1, i64* %7
store i64 0, i64* %8
%12 = load i8*, i8** %5
store i8 63, i8* %12
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i64, i64* %7
%16 = call i64 @read(i32 %13, i8* %14, i64 %15)
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %2
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
br label %31
25: ; preds = %19
%26 = load i64, i64* @errno
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %26, i8* %28)
store i64 0, i64* %3
br label %31
30: ; preds = %2
store i64 1, i64* %3
br label %31
31: ; preds = %30, %25, %23
%32 = load i64, i64* %3
ret i64 %32
}

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 i64 @portread2(i32 %0, i8* nocapture %1) {
store i8 63, i8* %1
%3 = tail call i64 @read(i32 %0, i8* %1, i64 1)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @EAGAIN
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %14
11: ; preds = %5
%12 = tail call i8* @strerror(i64 %6)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %6, i8* %12)
br label %14
14: ; preds = %2, %11, %9
%15 = phi i64 [ 0, %9 ], [ 0, %11 ], [ 1, %2 ]
ret i64 %15
}