#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 /*<<< orphan*/ TCIOFLUSH;
int read(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int serialport_read_until(int fd, char *buf, char until, int buf_max,
                          int timeout) {
  char b[1] = {0};
  int i = 0;
  do {
    int n = read(fd, b, 1);
    if (n == -1)
      return -1;
    if (n == 0) {
      usleep(1 * 1000);
      timeout--;
      continue;
    }
    buf[i] = b[0];
    i++;
  } while (b[0] != until && i < buf_max && timeout > 0);
  buf[i] = 0;
  tcflush(fd, TCIOFLUSH);
  return 0;
}


x86 assembly (clang, -O0):
<serialport_read_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
lea    -0x1d(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    119c <serialport_read_until+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <serialport_read_until+0xf4>
cmpl   $0x0,-0x28(%rbp)
jne    11be <serialport_read_until+0x7e>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d5 <serialport_read_until+0x95>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movsbl -0x1d(%rbp),%ecx
movsbl -0x11(%rbp),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x29(%rbp)
je     1205 <serialport_read_until+0xc5>
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    1205 <serialport_read_until+0xc5>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1172 <serialport_read_until+0x32>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_read_until>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebp
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%ebx
movb   $0x0,0x7(%rsp)
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11a0 <serialport_read_until+0x70>
cmp    $0xffffffff,%eax
je     11da <serialport_read_until+0xaa>
movzbl 0x7(%rsp),%eax
movslq %r13d,%r13
mov    %al,(%r14,%r13,1)
add    $0x1,%r13d
cmp    %r12b,%al
jne    11b7 <serialport_read_until+0x87>
jmp    11c0 <serialport_read_until+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1050 <usleep@plt>
add    $0xffffffff,%ebp
movzbl 0x7(%rsp),%eax
cmp    %r12b,%al
je     11c0 <serialport_read_until+0x90>
cmp    %r15d,%r13d
jge    11c0 <serialport_read_until+0x90>
test   %ebp,%ebp
jg     1160 <serialport_read_until+0x30>
movslq %r13d,%rax
movb   $0x0,(%r14,%rax,1)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@TCIOFLUSH = global i32 0
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 [1 x i8]
%13 = alloca i32
%14 = 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
%15 = bitcast [1 x i8]* %12 to i8*
store i32 0, i32* %13
br label %16
16: ; preds = %53, %5
%17 = load i32, i32* %7
%18 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 1)
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %6
br label %63
23: ; preds = %16
%24 = load i32, i32* %14
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i32 @usleep(i32 1000)
%28 = load i32, i32* %11
%29 = add nsw i32 %28, -1
store i32 %29, i32* %11
br label %39
30: ; preds = %23
%31 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%32 = load i8, i8* %31
%33 = load i8*, i8** %8
%34 = load i32, i32* %13
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 %32, i8* %36
%37 = load i32, i32* %13
%38 = add nsw i32 %37, 1
store i32 %38, i32* %13
br label %39
39: ; preds = %30, %26
%40 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = load i8, i8* %9
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %42, %44
br i1 %45, label %46, label %53
46: ; preds = %39
%47 = load i32, i32* %13
%48 = load i32, i32* %10
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* %11
%52 = icmp sgt i32 %51, 0
br label %53
53: ; preds = %50, %46, %39
%54 = phi i1 [ false, %46 ], [ false, %39 ], [ %52, %50 ]
br i1 %54, label %16, label %55
55: ; preds = %53
%56 = load i8*, i8** %8
%57 = load i32, i32* %13
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i32, i32* %7
%61 = load i32, i32* @TCIOFLUSH
%62 = call i32 @tcflush(i32 %60, i32 %61)
store i32 0, i32* %6
br label %63
63: ; preds = %55, %22
%64 = load i32, i32* %6
ret i32 %64
}

LLVM IR (-O2):
@TCIOFLUSH = global i32 0
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
store i8 0, i8* %7
br label %8
8: ; preds = %26, %5
%9 = phi i32 [ %4, %5 ], [ %23, %26 ]
%10 = phi i32 [ 0, %5 ], [ %24, %26 ]
%11 = call i32 @read(i32 %0, i8* %7, i32 1)
switch i32 %11, label %16 [
i32 -1, label %35
i32 0, label %12
]
12: ; preds = %8
%13 = tail call i32 @usleep(i32 1000)
%14 = add nsw i32 %9, -1
%15 = load i8, i8* %7
br label %21
16: ; preds = %8
%17 = load i8, i8* %7
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 %17, i8* %19
%20 = add nsw i32 %10, 1
br label %21
21: ; preds = %12, %16
%22 = phi i8 [ %17, %16 ], [ %15, %12 ]
%23 = phi i32 [ %9, %16 ], [ %14, %12 ]
%24 = phi i32 [ %20, %16 ], [ %10, %12 ]
%25 = icmp eq i8 %22, %2
br i1 %25, label %30, label %26
26: ; preds = %21
%27 = icmp slt i32 %24, %3
%28 = icmp sgt i32 %23, 0
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %8, label %30
30: ; preds = %21, %26
%31 = sext i32 %24 to i64
%32 = getelementptr inbounds i8, i8* %1, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* @TCIOFLUSH
%34 = tail call i32 @tcflush(i32 %0, i32 %33)
br label %35
35: ; preds = %8, %30
%36 = phi i32 [ 0, %30 ], [ %11, %8 ]
ret i32 %36
}