#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 */
typedef size_t ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, size_t);
ssize_t readSerial(int fd, char *buffer, ssize_t size) {
  ssize_t nbytes = 0;
  do {
    nbytes = read(fd, buffer, size - 1);
  } while (nbytes < 0 && errno == EINTR);
  buffer[nbytes] = '\0';
  printf("%s", buffer);
  return nbytes;
}


x86 assembly (clang, -O0):
<readSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jae    117d <readSerial+0x5d>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    113b <readSerial+0x1b>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readSerial>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
add    $0xffffffffffffffff,%rdx
call   1040 <read@plt>
mov    %rax,%r14
movb   $0x0,(%rbx,%rax,1)
lea    0xec2(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [3 x i8] c"%s\00"
define i64 @readSerial(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %21, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = sub i64 %11, 1
%13 = call i64 @read(i32 %9, i8* %10, i64 %12)
store i64 %13, i64* %7
br label %14
14: ; preds = %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %14
%22 = phi i1 [ false, %14 ], [ %20, %17 ]
br i1 %22, label %8, label %23
23: ; preds = %21
%24 = load i8*, i8** %5
%25 = load i64, i64* %7
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %5
%28 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = load i64, i64* %7
ret i64 %29
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [3 x i8] c"%s\00"
define i64 @readSerial(i32 %0, i8* %1, i64 %2) {
%4 = add i64 %2, -1
%5 = tail call i64 @read(i32 %0, i8* %1, i64 %4)
%6 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 0, i8* %6
%7 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
ret i64 %5
}