#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--;
      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    11f3 <serialport_read_until+0xd3>
cmpl   $0x0,-0x28(%rbp)
jne    118e <serialport_read_until+0x6e>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a5 <serialport_read_until+0x85>
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     11d5 <serialport_read_until+0xb5>
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    11d5 <serialport_read_until+0xb5>
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     11aa <serialport_read_until+0x8a>
movzbl 0x7(%rsp),%eax
movslq %r13d,%r13
mov    %al,(%r14,%r13,1)
add    $0x1,%r13d
cmp    %r12b,%al
jne    1197 <serialport_read_until+0x77>
jmp    11a0 <serialport_read_until+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1040 <usleep@plt>
add    $0xffffffff,%ebx
movzbl 0x7(%rsp),%eax
cmp    %r12b,%al
je     11a0 <serialport_read_until+0x80>
cmp    %r15d,%r13d
jge    11a0 <serialport_read_until+0x80>
test   %ebx,%ebx
jg     1140 <serialport_read_until+0x20>
movslq %r13d,%rax
movb   $0x0,(%r14,%rax,1)
xor    %eax,%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 = %52, %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 %59
22: ; preds = %15
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
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
br label %38
29: ; preds = %22
%30 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%31 = load i8, i8* %30
%32 = load i8*, i8** %8
%33 = load i32, i32* %13
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
%36 = load i32, i32* %13
%37 = add nsw i32 %36, 1
store i32 %37, i32* %13
br label %38
38: ; preds = %29, %25
%39 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = load i8, i8* %9
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %41, %43
br i1 %44, label %45, label %52
45: ; preds = %38
%46 = load i32, i32* %13
%47 = load i32, i32* %10
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %11
%51 = icmp sgt i32 %50, 0
br label %52
52: ; preds = %49, %45, %38
%53 = phi i1 [ false, %45 ], [ false, %38 ], [ %51, %49 ]
br i1 %53, label %15, label %54
54: ; preds = %52
%55 = load i8*, i8** %8
%56 = load i32, i32* %13
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
store i32 0, i32* %6
br label %59
59: ; preds = %54, %21
%60 = load i32, i32* %6
ret i32 %60
}

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 = %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 %33
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
br label %33
33: ; preds = %8, %30
%34 = phi i32 [ 0, %30 ], [ %11, %8 ]
ret i32 %34
}