#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 *, ...);
int read(int, char *, int);
char *strerror(scalar_t__);
int portread2(int fd, char *data) {
  int n = 0, len = 1;
  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    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e9 <portread2+0xb9>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    11ab <portread2+0x7b>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <portread2+0xc0>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <portread2+0xc0>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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   %eax,%eax
jns    119e <portread2+0x5e>
mov    0x2e7f(%rip),%rax
mov    (%rax),%r14
mov    0x2e65(%rip),%rax
cmp    (%rax),%r14
jne    1180 <portread2+0x40>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    119e <portread2+0x5e>
mov    %r14,%rdi
call   1060 <strerror@plt>
lea    0xe71(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
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 i32 @portread2(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 1, i32* %7
store i32 0, i32* %8
%12 = load i8*, i8** %5
store i8 63, i8* %12
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i32, i32* %7
%16 = call i32 @read(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %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 i32 0, i32* %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 i32 0, i32* %3
br label %31
30: ; preds = %2
store i32 1, i32* %3
br label %31
31: ; preds = %30, %25, %23
%32 = load i32, i32* %3
ret i32 %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 i32 @portread2(i32 %0, i8* nocapture %1) {
store i8 63, i8* %1
%3 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%4 = icmp slt i32 %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 i32 [ 0, %9 ], [ 0, %11 ], [ 1, %2 ]
ret i32 %15
}