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


x86 assembly (clang, -O0):
<uarthandler_read>:
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    11d0 <uarthandler_read+0xa0>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <uarthandler_read+0x64>
movl   $0x1,-0x4(%rbp)
jmp    11d7 <uarthandler_read+0xa7>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d7 <uarthandler_read+0xa7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uarthandler_read>:
push   %rbx
mov    %rsi,%rbx
mov    $0xfe,%edx
call   1040 <read@plt>
movslq %eax,%rcx
movb   $0x0,-0x1(%rcx,%rbx,1)
mov    $0x1,%eax
test   %ecx,%ecx
js     1151 <uarthandler_read+0x21>
pop    %rbx
ret
mov    0x2e88(%rip),%rcx
mov    (%rcx),%rbx
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rbx
je     114f <uarthandler_read+0x1f>
mov    %rbx,%rdi
call   1050 <strerror@plt>
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [25 x i8] c"SERIAL read error %d %s\0A\00"
define i32 @uarthandler_read(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 %27
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 %22
21: ; preds = %17
store i32 1, i32* %3
br label %28
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %23, i8* %25)
store i32 0, i32* %3
br label %28
27: ; preds = %2
store i32 1, i32* %3
br label %28
28: ; preds = %27, %22, %21
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [25 x i8] c"SERIAL read error %d %s\0A\00"
define i32 @uarthandler_read(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 %15
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAGAIN
%11 = icmp eq i64 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = tail call i8* @strerror(i64 %9)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %9, i8* %13)
br label %15
15: ; preds = %2, %8, %12
%16 = phi i32 [ 0, %12 ], [ 1, %8 ], [ 1, %2 ]
ret i32 %16
}