#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 tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int serialport_read_until(int fd, char *buf, char until, int buf_max,
                          int timeout) {
  static struct timeval t1, t2;
  double timeLeft;
  char b[1];
  int i = 0;
  timeLeft = timeout;
  do {
    int n = read(fd, b, 1);
    if (n == -1)
      return -1;
    if (n == 0) {
      if (i == 0)
        return 0;
      gettimeofday(&t1, NULL);
      usleep(1000);
      gettimeofday(&t2, NULL);
      timeLeft -= (((t2.tv_sec - t1.tv_sec) * 1000) +
                   ((t2.tv_usec - t1.tv_usec) / 1000));
      continue;
    }
    buf[i++] = b[0];
  } while (b[0] != until && i < buf_max && timeLeft > 0);
  return i;
}


x86 assembly (clang, -O0):
<serialport_read_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x30(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1186 <serialport_read_until+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <serialport_read_until+0x148>
cmpl   $0x0,-0x34(%rbp)
jne    1217 <serialport_read_until+0xe7>
cmpl   $0x0,-0x30(%rbp)
jne    11a6 <serialport_read_until+0x76>
movl   $0x0,-0x4(%rbp)
jmp    1278 <serialport_read_until+0x148>
lea    0x2e8f(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
lea    0x2e7d(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    0x2e6e(%rip),%eax
sub    0x2e60(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x38(%rbp)
mov    0x2e5d(%rip),%eax
sub    0x2e4f(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
cvtsi2sd %eax,%xmm1
movsd  -0x28(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    122f <serialport_read_until+0xff>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x30(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x29(%rbp),%ecx
movsbl -0x11(%rbp),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x39(%rbp)
je     1267 <serialport_read_until+0x137>
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x39(%rbp)
jge    1267 <serialport_read_until+0x137>
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    115c <serialport_read_until+0x2c>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_read_until>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edi,%ebp
cvtsi2sd %r8d,%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %ebx,%ebx
lea    0xf(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11a0 <serialport_read_until+0x70>
cmp    $0xffffffff,%eax
je     1241 <serialport_read_until+0x111>
movzbl 0xf(%rsp),%eax
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %al,(%r15,%rcx,1)
cmp    %r12b,%al
jne    1226 <serialport_read_until+0xf6>
jmp    1247 <serialport_read_until+0x117>
nopl   0x0(%rax)
test   %ebx,%ebx
je     1245 <serialport_read_until+0x115>
lea    0x2e8d(%rip),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
lea    0x2e7d(%rip),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2e70(%rip),%eax
sub    0x2e62(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    0x2e62(%rip),%ecx
sub    0x2e54(%rip),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
movsd  0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
movzbl 0xf(%rsp),%eax
cmp    %r12b,%al
je     1247 <serialport_read_until+0x117>
cmp    %r14d,%ebx
jge    1247 <serialport_read_until+0x117>
movsd  0x10(%rsp),%xmm0
ucomisd 0xdc7(%rip),%xmm0
ja     1160 <serialport_read_until+0x30>
jmp    1247 <serialport_read_until+0x117>
mov    %eax,%ebx
jmp    1247 <serialport_read_until+0x117>
xor    %ebx,%ebx
mov    %ebx,%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 }
@serialport_read_until.t1 = internal global %struct.timeval zeroinitializer
@serialport_read_until.t2 = internal global %struct.timeval zeroinitializer
define i32 @serialport_read_until(i32 %0, i8* %1, i8 signext %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca [1 x i8]
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8 %2, i8* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %14
%16 = load i32, i32* %11
%17 = sitofp i32 %16 to double
store double %17, double* %12
br label %18
18: ; preds = %70, %5
%19 = load i32, i32* %7
%20 = getelementptr inbounds [1 x i8], [1 x i8]* %13, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1)
store i32 %21, i32* %15
%22 = load i32, i32* %15
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -1, i32* %6
br label %74
25: ; preds = %18
%26 = load i32, i32* %15
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = load i32, i32* %14
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 0, i32* %6
br label %74
32: ; preds = %28
%33 = call i32 @gettimeofday(%struct.timeval* @serialport_read_until.t1, i32* null)
%34 = call i32 @usleep(i32 1000)
%35 = call i32 @gettimeofday(%struct.timeval* @serialport_read_until.t2, i32* null)
%36 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t2, i32 0, i32 0)
%37 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t1, i32 0, i32 0)
%38 = sub nsw i32 %36, %37
%39 = mul nsw i32 %38, 1000
%40 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t2, i32 0, i32 1)
%41 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t1, i32 0, i32 1)
%42 = sub nsw i32 %40, %41
%43 = sdiv i32 %42, 1000
%44 = add nsw i32 %39, %43
%45 = sitofp i32 %44 to double
%46 = load double, double* %12
%47 = fsub double %46, %45
store double %47, double* %12
br label %56
48: ; preds = %25
%49 = getelementptr inbounds [1 x i8], [1 x i8]* %13, i64 0, i64 0
%50 = load i8, i8* %49
%51 = load i8*, i8** %8
%52 = load i32, i32* %14
%53 = add nsw i32 %52, 1
store i32 %53, i32* %14
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
store i8 %50, i8* %55
br label %56
56: ; preds = %48, %32
%57 = getelementptr inbounds [1 x i8], [1 x i8]* %13, i64 0, i64 0
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = load i8, i8* %9
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %59, %61
br i1 %62, label %63, label %70
63: ; preds = %56
%64 = load i32, i32* %14
%65 = load i32, i32* %10
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load double, double* %12
%69 = fcmp ogt double %68, 0.000000e+00
br label %70
70: ; preds = %67, %63, %56
%71 = phi i1 [ false, %63 ], [ false, %56 ], [ %69, %67 ]
br i1 %71, label %18, label %72
72: ; preds = %70
%73 = load i32, i32* %14
store i32 %73, i32* %6
br label %74
74: ; preds = %72, %31, %24
%75 = load i32, i32* %6
ret i32 %75
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@serialport_read_until.t1 = internal global %struct.timeval zeroinitializer
@serialport_read_until.t2 = internal global %struct.timeval zeroinitializer
define i32 @serialport_read_until(i32 %0, i8* nocapture writeonly %1, i8 signext %2, i32 %3, i32 %4) {
%6 = alloca [1 x i8]
%7 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%8 = sitofp i32 %4 to double
br label %9
9: ; preds = %41, %5
%10 = phi double [ %8, %5 ], [ %38, %41 ]
%11 = phi i32 [ 0, %5 ], [ %39, %41 ]
%12 = call i32 @read(i32 %0, i8* %7, i32 1)
switch i32 %12, label %31 [
i32 -1, label %45
i32 0, label %13
]
13: ; preds = %9
%14 = icmp eq i32 %11, 0
br i1 %14, label %45, label %15
15: ; preds = %13
%16 = tail call i32 @gettimeofday(%struct.timeval* @serialport_read_until.t1, i32* null)
%17 = tail call i32 @usleep(i32 1000)
%18 = tail call i32 @gettimeofday(%struct.timeval* @serialport_read_until.t2, i32* null)
%19 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t2, i64 0, i32 0)
%20 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t1, i64 0, i32 0)
%21 = sub nsw i32 %19, %20
%22 = mul nsw i32 %21, 1000
%23 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t2, i64 0, i32 1)
%24 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @serialport_read_until.t1, i64 0, i32 1)
%25 = sub nsw i32 %23, %24
%26 = sdiv i32 %25, 1000
%27 = add nsw i32 %26, %22
%28 = sitofp i32 %27 to double
%29 = fsub double %10, %28
%30 = load i8, i8* %7
br label %36
31: ; preds = %9
%32 = load i8, i8* %7
%33 = add nsw i32 %11, 1
%34 = sext i32 %11 to i64
%35 = getelementptr inbounds i8, i8* %1, i64 %34
store i8 %32, i8* %35
br label %36
36: ; preds = %15, %31
%37 = phi i8 [ %30, %15 ], [ %32, %31 ]
%38 = phi double [ %29, %15 ], [ %10, %31 ]
%39 = phi i32 [ %11, %15 ], [ %33, %31 ]
%40 = icmp eq i8 %37, %2
br i1 %40, label %45, label %41
41: ; preds = %36
%42 = icmp slt i32 %39, %3
%43 = fcmp ogt double %38, 0.000000e+00
%44 = select i1 %42, i1 %43, i1 false
br i1 %44, label %9, label %45
45: ; preds = %13, %9, %41, %36
%46 = phi i32 [ %39, %36 ], [ %39, %41 ], [ 0, %13 ], [ %12, %9 ]
ret i32 %46
}