#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 read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int serialport_read_until(int fd, char *buf, char until, int buf_max,
                          int timeout) {
  char b[1];
  int i = 0;
  do {
    int n = read(fd, b, 1);
    if (n == -1)
      return -1;
    if (n == 0) {
      usleep(1 * 1000);
      timeout--;
      if (timeout == 0)
        return -2;
      continue;
    }
    buf[i] = b[0];
    i++;
  } while (b[0] != until && i < buf_max && timeout > 0);
  buf[i] = 0;
  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)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    116c <serialport_read_until+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <serialport_read_until+0xe9>
cmpl   $0x0,-0x28(%rbp)
jne    11a4 <serialport_read_until+0x84>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    119f <serialport_read_until+0x7f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1209 <serialport_read_until+0xe9>
jmp    11bb <serialport_read_until+0x9b>
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     11eb <serialport_read_until+0xcb>
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    11eb <serialport_read_until+0xcb>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1142 <serialport_read_until+0x22>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
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,%ebx
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %r13d,%r13d
nopl   0x0(%rax)
mov    %ebp,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     1180 <serialport_read_until+0x60>
cmp    $0xffffffff,%eax
je     11b3 <serialport_read_until+0x93>
movzbl 0x7(%rsp),%eax
movslq %r13d,%r13
mov    %al,(%r14,%r13,1)
add    $0x1,%r13d
cmp    %r12b,%al
jne    1199 <serialport_read_until+0x79>
jmp    11a2 <serialport_read_until+0x82>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1040 <usleep@plt>
add    $0xffffffff,%ebx
je     11ae <serialport_read_until+0x8e>
movzbl 0x7(%rsp),%eax
cmp    %r12b,%al
je     11a2 <serialport_read_until+0x82>
cmp    %r15d,%r13d
jge    11a2 <serialport_read_until+0x82>
test   %ebx,%ebx
jg     1140 <serialport_read_until+0x20>
movslq %r13d,%rax
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
jmp    11b3 <serialport_read_until+0x93>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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