#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 */
int espera_recepcion(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int lee(int s, unsigned char *buffer, int longitud, int segundos) {
  int leidos_total = 0;
  int leidos_actual = 0;
  int error = 0;
  while (leidos_total < longitud && !error) {
    if (espera_recepcion(s, segundos) == 1) {
      leidos_actual = read(s, buffer, 1);
      if (leidos_actual == 0)
        error = 1;
      else if (leidos_actual < 0) {
        perror("read");
        error = 1;
      } else
        leidos_total += leidos_actual;
    } else {
      fprintf(stderr, "%s\n", "Err 6");
      error = 1;
      exit(1);
    }
  }
  return (leidos_total == longitud);
}


x86 assembly (clang, -O0):
<lee>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x25(%rbp)
jge    1197 <lee+0x47>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11a7 <lee+0x57>
jmp    124c <lee+0xfc>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <espera_recepcion@plt>
cmp    $0x1,%eax
jne    121a <lee+0xca>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11e5 <lee+0x95>
movl   $0x1,-0x24(%rbp)
jmp    1215 <lee+0xc5>
cmpl   $0x0,-0x20(%rbp)
jge    1207 <lee+0xb7>
lea    0xe0a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x24(%rbp)
jmp    1210 <lee+0xc0>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1215 <lee+0xc5>
jmp    1247 <lee+0xf7>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
lea    0xdd8(%rip),%rdx
call   1050 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    117a <lee+0x2a>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lee>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
test   %edx,%edx
jle    119d <lee+0x4d>
mov    %ecx,%r15d
mov    %rsi,%r12
mov    %edi,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <espera_recepcion@plt>
cmp    $0x1,%eax
jne    11be <lee+0x6e>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     119f <lee+0x4f>
js     11b0 <lee+0x60>
add    %eax,%ebx
cmp    %r14d,%ebx
jl     1170 <lee+0x20>
jmp    119f <lee+0x4f>
xor    %ebx,%ebx
xor    %eax,%eax
cmp    %r14d,%ebx
sete   %al
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe49(%rip),%rdi
call   1060 <perror@plt>
jmp    119f <lee+0x4f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
lea    0xe34(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [6 x i8] c"Err 6\00"
define i32 @lee(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
br label %12
12: ; preds = %49, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* %11
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ false, %12 ], [ %19, %16 ]
br i1 %21, label %22, label %50
22: ; preds = %20
%23 = load i32, i32* %5
%24 = load i32, i32* %8
%25 = call i32 @espera_recepcion(i32 %23, i32 %24)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %45
27: ; preds = %22
%28 = load i32, i32* %5
%29 = load i8*, i8** %6
%30 = call i32 @read(i32 %28, i8* %29, i32 1)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 1, i32* %11
br label %44
34: ; preds = %27
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %11
br label %43
39: ; preds = %34
%40 = load i32, i32* %10
%41 = load i32, i32* %9
%42 = add nsw i32 %41, %40
store i32 %42, i32* %9
br label %43
43: ; preds = %39, %37
br label %44
44: ; preds = %43, %33
br label %49
45: ; preds = %22
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %11
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %44
br label %12
50: ; preds = %20
%51 = load i32, i32* %9
%52 = load i32, i32* %7
%53 = icmp eq i32 %51, %52
%54 = zext i1 %53 to i32
ret i32 %54
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [6 x i8] c"Err 6\00"
define i32 @lee(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %24
6: ; preds = %4, %21
%7 = phi i32 [ %22, %21 ], [ 0, %4 ]
%8 = tail call i32 @espera_recepcion(i32 %0, i32 %3)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = icmp slt i32 %11, 0
br i1 %14, label %15, label %21
15: ; preds = %13
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
17: ; preds = %6
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = add nsw i32 %11, %7
%23 = icmp slt i32 %22, %2
br i1 %23, label %6, label %24
24: ; preds = %10, %21, %15, %4
%25 = phi i32 [ 0, %4 ], [ %7, %15 ], [ %7, %10 ], [ %22, %21 ]
%26 = icmp eq i32 %25, %2
%27 = zext i1 %26 to i32
ret i32 %27
}