#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 member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int lowlevel_read(int fd, char *data) {
  static char rx_buf[2048];
  int rx_ptr = 0;
  const struct timeval tv = {0, 100000};
  fd_set rfds;
  int n;
  int r;
  for (;;) {
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    r = select(fd + 1, &rfds, NULL, NULL, (struct timeval *)&tv);
    if (r < 0) {
      fprintf(stderr,
              "ERROR: "
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/"
              "serial/serial.c"
              " line %d: "
              "select() error"
              "\n",
              229);
      return -1;
    }
    if (r == 0) {
      fprintf(stderr,
              "ERROR: "
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/"
              "serial/serial.c"
              " line %d: "
              "Read timeout"
              "\n",
              234);
      return -2;
    }
    n = read(fd, &rx_buf[rx_ptr], 2048 - rx_ptr);
    if (n < 0)
      return -3;
    for (int i = 0; i < n; i++) {
      if (rx_buf[rx_ptr + i] == 0) {
        memcpy(data, rx_buf, rx_ptr + i);
        return rx_ptr + i;
      }
    }
    rx_ptr += n;
    if (rx_ptr == 2048) {
      fprintf(stderr,
              "ERROR: "
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/"
              "serial/serial.c"
              " line %d: "
              "Buffer full"
              "\n",
              252);
      return -4;
    }
  }
}


x86 assembly (clang, -O0):
<lowlevel_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11e3 <lowlevel_read+0x83>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0xe5,%edx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <lowlevel_read+0x192>
cmpl   $0x0,-0x2c(%rbp)
jne    1213 <lowlevel_read+0xb3>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xeb7(%rip),%rsi
mov    $0xea,%edx
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12f2 <lowlevel_read+0x192>
mov    -0x8(%rbp),%edi
movslq -0x14(%rbp),%rax
lea    0x2e3f(%rip),%rsi
add    %rax,%rsi
mov    $0x800,%edx
sub    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    124a <lowlevel_read+0xea>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12f2 <lowlevel_read+0x192>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12b1 <lowlevel_read+0x151>
mov    -0x14(%rbp),%eax
add    -0x30(%rbp),%eax
movslq %eax,%rcx
lea    0x2df3(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    129e <lowlevel_read+0x13e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    -0x30(%rbp),%edx
lea    0x2dd5(%rip),%rsi
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f2 <lowlevel_read+0x192>
jmp    12a3 <lowlevel_read+0x143>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1251 <lowlevel_read+0xf1>
mov    -0x28(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x800,-0x14(%rbp)
jne    12ed <lowlevel_read+0x18d>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
mov    $0xfc,%edx
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12f2 <lowlevel_read+0x192>
jmp    1181 <lowlevel_read+0x21>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lowlevel_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x8(%rsp)
mov    %edi,%r15d
movabs $0x186a000000000,%rax
mov    %rax,0x10(%rsp)
lea    0x1(%r15),%r13d
xor    %r12d,%r12d
lea    0x4(%rsp),%rbx
lea    0x10(%rsp),%rbp
lea    0x2ec3(%rip),%r14
jmp    11b8 <lowlevel_read+0x58>
nop
add    %eax,%r12d
cmp    $0x800,%r12d
mov    %ebx,%r13d
lea    0x4(%rsp),%rbx
je     129e <lowlevel_read+0x13e>
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1070 <select@plt>
test   %eax,%eax
js     1252 <lowlevel_read+0xf2>
je     1274 <lowlevel_read+0x114>
mov    %r15,%rdi
mov    %r13d,%ebx
movslq %r12d,%r13
lea    (%r14,%r13,1),%rsi
mov    $0x800,%edx
sub    %r13d,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1296 <lowlevel_read+0x136>
je     11a0 <lowlevel_read+0x40>
mov    %eax,%edx
xor    %ecx,%ecx
nop
lea    (%rcx,%r13,1),%rsi
cmpb   $0x0,(%r14,%rsi,1)
je     1229 <lowlevel_read+0xc9>
add    $0x1,%rcx
cmp    %rcx,%rdx
jne    1210 <lowlevel_read+0xb0>
jmp    11a0 <lowlevel_read+0x40>
add    %ecx,%r12d
lea    0x2e2d(%rip),%rsi
mov    0x8(%rsp),%rdi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
mov    $0xe5,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r12d
jmp    1240 <lowlevel_read+0xe0>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    $0xea,%edx
call   1040 <fprintf@plt>
mov    $0xfffffffe,%r12d
jmp    1240 <lowlevel_read+0xe0>
mov    $0xfffffffd,%r12d
jmp    1240 <lowlevel_read+0xe0>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xea8(%rip),%rsi
mov    $0xfc,%edx
call   1040 <fprintf@plt>
mov    $0xfffffffc,%r12d
jmp    1240 <lowlevel_read+0xe0>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@lowlevel_read.rx_buf = internal global [2048 x i8] zeroinitializer
@__const.lowlevel_read.tv = %struct.timeval { i32 0, i32 100000 }
@stderr = global i32 0
@.str = [172 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: select() error\0A\00"
@.str.1 = [170 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Read timeout\0A\00"
@.str.2 = [169 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Buffer full\0A\00"
define i32 @lowlevel_read(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%12 = bitcast %struct.timeval* %7 to i8*
br label %13
13: ; preds = %78, %2
%14 = call i32 @FD_ZERO(i32* %8)
%15 = load i32, i32* %4
%16 = call i32 @FD_SET(i32 %15, i32* %8)
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
%19 = call i32 @select(i32 %18, i32* %8, i32* null, i32* null, %struct.timeval* %7)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([172 x i8], [172 x i8]* @.str, i64 0, i64 0), i32 229)
store i32 -1, i32* %3
br label %79
25: ; preds = %13
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([170 x i8], [170 x i8]* @.str.1, i64 0, i64 0), i32 234)
store i32 -2, i32* %3
br label %79
31: ; preds = %25
%32 = load i32, i32* %4
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_read.rx_buf, i64 0, i64 %34
%36 = load i32, i32* %6
%37 = sub nsw i32 2048, %36
%38 = call i32 @read(i32 %32, i8* %35, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %31
store i32 -3, i32* %3
br label %79
42: ; preds = %31
store i32 0, i32* %11
br label %43
43: ; preds = %66, %42
%44 = load i32, i32* %11
%45 = load i32, i32* %9
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %69
47: ; preds = %43
%48 = load i32, i32* %6
%49 = load i32, i32* %11
%50 = add nsw i32 %48, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_read.rx_buf, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %65
56: ; preds = %47
%57 = load i8*, i8** %5
%58 = load i32, i32* %6
%59 = load i32, i32* %11
%60 = add nsw i32 %58, %59
%61 = call i32 @memcpy(i8* %57, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @lowlevel_read.rx_buf, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %6
%63 = load i32, i32* %11
%64 = add nsw i32 %62, %63
store i32 %64, i32* %3
br label %79
65: ; preds = %47
br label %66
66: ; preds = %65
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 1
store i32 %68, i32* %11
br label %43
69: ; preds = %43
%70 = load i32, i32* %9
%71 = load i32, i32* %6
%72 = add nsw i32 %71, %70
store i32 %72, i32* %6
%73 = load i32, i32* %6
%74 = icmp eq i32 %73, 2048
br i1 %74, label %75, label %78
75: ; preds = %69
%76 = load i32, i32* @stderr
%77 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([169 x i8], [169 x i8]* @.str.2, i64 0, i64 0), i32 252)
store i32 -4, i32* %3
br label %79
78: ; preds = %69
br label %13
79: ; preds = %75, %56, %41, %28, %22
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@lowlevel_read.rx_buf = internal global [2048 x i8] zeroinitializer
@stderr = global i32 0
@.str = [172 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: select() error\0A\00"
@.str.1 = [170 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Read timeout\0A\00"
@.str.2 = [169 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Buffer full\0A\00"
define i32 @lowlevel_read(i32 %0, i8* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timeval*
%5 = alloca i32
%6 = bitcast i64* %3 to i8*
store i64 429496729600000, i64* %3
%7 = bitcast i32* %5 to i8*
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %45, %2
%10 = phi i32 [ 0, %2 ], [ %46, %45 ]
%11 = call i32 @FD_ZERO(i32* %5)
%12 = call i32 @FD_SET(i32 %0, i32* %5)
%13 = call i32 @select(i32 %8, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([172 x i8], [172 x i8]* @.str, i64 0, i64 0), i32 229)
br label %51
18: ; preds = %9
%19 = icmp eq i32 %13, 0
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([170 x i8], [170 x i8]* @.str.1, i64 0, i64 0), i32 234)
br label %51
23: ; preds = %18
%24 = sext i32 %10 to i64
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_read.rx_buf, i64 0, i64 %24
%26 = sub nsw i32 2048, %10
%27 = call i32 @read(i32 %0, i8* %25, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %51, label %29
29: ; preds = %23
%30 = icmp eq i32 %27, 0
br i1 %30, label %45, label %31
31: ; preds = %29
%32 = zext i32 %27 to i64
br label %35
33: ; preds = %35
%34 = icmp eq i64 %41, %32
br i1 %34, label %45, label %35
35: ; preds = %31, %33
%36 = phi i64 [ 0, %31 ], [ %41, %33 ]
%37 = add nsw i64 %36, %24
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_read.rx_buf, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 0
%41 = add nuw nsw i64 %36, 1
br i1 %40, label %42, label %33
42: ; preds = %35
%43 = trunc i64 %37 to i32
%44 = call i32 @memcpy(i8* %1, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @lowlevel_read.rx_buf, i64 0, i64 0), i32 %43)
br label %51
45: ; preds = %33, %29
%46 = add nsw i32 %27, %10
%47 = icmp eq i32 %46, 2048
br i1 %47, label %48, label %9
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([169 x i8], [169 x i8]* @.str.2, i64 0, i64 0), i32 252)
br label %51
51: ; preds = %23, %42, %48, %20, %15
%52 = phi i32 [ -1, %15 ], [ -2, %20 ], [ -4, %48 ], [ %43, %42 ], [ -3, %23 ]
ret i32 %52
}