#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*/ TCIOFLUSH;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int write(int, char *, int);
int lowlevel_write(int fd, char *data, int n) {
  static char tx_buf[2048];
  int tx_ptr = 0;
  int n_written;
  int r;
  const struct timeval tv = {0, 100000};
  fd_set wfds;
  FD_ZERO(&wfds);
  FD_SET(fd, &wfds);
  memcpy(tx_buf, data, n);
  tx_buf[n] = 0;
  for (;;) {
    FD_ZERO(&wfds);
    FD_SET(fd, &wfds);
    r = select(fd + 1, NULL, &wfds, 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",
              279);
      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: "
              "Write timeout"
              "\n",
              284);
      return -2;
    }
    n_written = write(fd, &tx_buf[tx_ptr], n + 1 - tx_ptr);
    if (n_written < 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 error"
              "\n",
              292);
      return -3;
    }
    tx_ptr += n_written;
    if (tx_ptr == n + 1)
      return 0;
  }
  tcflush(fd, TCIOFLUSH);
}


x86 assembly (clang, -O0):
<lowlevel_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0xe80(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0x2eb9(%rip),%rdi
call   1050 <memcpy@plt>
movslq -0x14(%rbp),%rcx
lea    0x2ea9(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    -0x2c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x2c(%rbp),%rdx
lea    -0x28(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    121d <lowlevel_write+0xbd>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
mov    $0x117,%edx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <lowlevel_write+0x16a>
cmpl   $0x0,-0x20(%rbp)
jne    124d <lowlevel_write+0xed>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
mov    $0x11c,%edx
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12ca <lowlevel_write+0x16a>
mov    -0x8(%rbp),%edi
movslq -0x18(%rbp),%rax
lea    0x2e05(%rip),%rsi
add    %rax,%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    129f <lowlevel_write+0x13f>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xed6(%rip),%rsi
mov    $0x124,%edx
call   1040 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12ca <lowlevel_write+0x16a>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    12c5 <lowlevel_write+0x165>
movl   $0x0,-0x4(%rbp)
jmp    12ca <lowlevel_write+0x16a>
jmp    11bb <lowlevel_write+0x5b>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lowlevel_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r12
mov    %edi,%ebx
movabs $0x186a000000000,%rax
mov    %rax,0x8(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
lea    0x2ebe(%rip),%r15
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rbp
movb   $0x0,0x0(%rbp,%r15,1)
lea    0x1(%rbx),%r13d
add    $0x1,%ebp
xor    %r15d,%r15d
lea    0x8(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
mov    %r13d,%edi
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    %r12,%r8
call   1070 <select@plt>
test   %eax,%eax
js     1223 <lowlevel_write+0xc3>
je     1244 <lowlevel_write+0xe4>
movslq %r15d,%rax
lea    0x2e5c(%rip),%rcx
lea    (%rcx,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %ebx,%edi
call   1030 <write@plt>
test   %eax,%eax
js     1265 <lowlevel_write+0x105>
add    %eax,%r15d
cmp    %ebp,%r15d
jne    11d0 <lowlevel_write+0x70>
xor    %eax,%eax
jmp    1284 <lowlevel_write+0x124>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    $0x117,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1284 <lowlevel_write+0x124>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
mov    $0x11c,%edx
call   1040 <fprintf@plt>
mov    $0xfffffffe,%eax
jmp    1284 <lowlevel_write+0x124>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xee2(%rip),%rsi
mov    $0x124,%edx
call   1040 <fprintf@plt>
mov    $0xfffffffd,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@lowlevel_write.tx_buf = internal global [2048 x i8] zeroinitializer
@__const.lowlevel_write.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 = [171 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: Write timeout\0A\00"
@.str.2 = [168 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 error\0A\00"
@TCIOFLUSH = global i32 0
define i32 @lowlevel_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%13 = bitcast %struct.timeval* %11 to i8*
%14 = call i32 @FD_ZERO(i32* %12)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %12)
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = call i32 @memcpy(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @lowlevel_write.tx_buf, i64 0, i64 0), i8* %17, i32 %18)
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_write.tx_buf, i64 0, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %65, %3
%24 = call i32 @FD_ZERO(i32* %12)
%25 = load i32, i32* %5
%26 = call i32 @FD_SET(i32 %25, i32* %12)
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* null, i32* %12, i32* null, %struct.timeval* %11)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([172 x i8], [172 x i8]* @.str, i64 0, i64 0), i32 279)
store i32 -1, i32* %4
br label %66
35: ; preds = %23
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 284)
store i32 -2, i32* %4
br label %66
41: ; preds = %35
%42 = load i32, i32* %5
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_write.tx_buf, i64 0, i64 %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
%48 = load i32, i32* %8
%49 = sub nsw i32 %47, %48
%50 = call i32 @write(i32 %42, i8* %45, i32 %49)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %41
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([168 x i8], [168 x i8]* @.str.2, i64 0, i64 0), i32 292)
store i32 -3, i32* %4
br label %66
56: ; preds = %41
%57 = load i32, i32* %9
%58 = load i32, i32* %8
%59 = add nsw i32 %58, %57
store i32 %59, i32* %8
%60 = load i32, i32* %8
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
%63 = icmp eq i32 %60, %62
br i1 %63, label %64, label %65
64: ; preds = %56
store i32 0, i32* %4
br label %66
65: ; preds = %56
br label %23
66: ; preds = %64, %53, %38, %32
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@lowlevel_write.tx_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 = [171 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: Write timeout\0A\00"
@.str.2 = [168 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 error\0A\00"
@TCIOFLUSH = global i32 0
define i32 @lowlevel_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = alloca i32
%7 = bitcast i64* %4 to i8*
store i64 429496729600000, i64* %4
%8 = bitcast i32* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %6)
%10 = call i32 @FD_SET(i32 %0, i32* %6)
%11 = call i32 @memcpy(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @lowlevel_write.tx_buf, i64 0, i64 0), i8* %1, i32 %2)
%12 = sext i32 %2 to i64
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_write.tx_buf, i64 0, i64 %12
store i8 0, i8* %13
%14 = add nsw i32 %0, 1
%15 = add nsw i32 %2, 1
br label %16
16: ; preds = %39, %3
%17 = phi i32 [ 0, %3 ], [ %40, %39 ]
%18 = call i32 @FD_ZERO(i32* %6)
%19 = call i32 @FD_SET(i32 %0, i32* %6)
%20 = call i32 @select(i32 %14, i32* null, i32* %6, i32* null, %struct.timeval* %5)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%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 279)
br label %42
25: ; preds = %16
%26 = icmp eq i32 %20, 0
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 284)
br label %42
30: ; preds = %25
%31 = sext i32 %17 to i64
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* @lowlevel_write.tx_buf, i64 0, i64 %31
%33 = sub nsw i32 %15, %17
%34 = call i32 @write(i32 %0, i8* %32, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([168 x i8], [168 x i8]* @.str.2, i64 0, i64 0), i32 292)
br label %42
39: ; preds = %30
%40 = add nsw i32 %34, %17
%41 = icmp eq i32 %40, %15
br i1 %41, label %42, label %16
42: ; preds = %39, %36, %27, %22
%43 = phi i32 [ -1, %22 ], [ -2, %27 ], [ -3, %36 ], [ 0, %39 ]
ret i32 %43
}