#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*/ printf(char *, int, int, char);
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;
    }
    printf("serialport_read_until: i=%d, n=%d b='%c'\n", i, n, b[0]);
    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   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    117c <serialport_read_until+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <serialport_read_until+0xe9>
cmpl   $0x0,-0x28(%rbp)
jne    119e <serialport_read_until+0x6e>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cb <serialport_read_until+0x9b>
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xe55(%rip),%rdi
movsbl -0x1d(%rbp),%ecx
call   1030 <printf@plt>
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     11fb <serialport_read_until+0xcb>
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    11fb <serialport_read_until+0xcb>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1152 <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
sub    $0x18,%rsp
mov    %r8d,%r12d
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,0x10(%rsp)
mov    %edi,%r13d
xor    %ebp,%ebp
mov    %r13d,%edi
lea    0xb(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11c0 <serialport_read_until+0x90>
cmp    $0xffffffff,%eax
je     11f4 <serialport_read_until+0xc4>
movzbl 0xb(%rsp),%r15d
mov    %r12d,0xc(%rsp)
mov    %r13d,%r12d
mov    %r14d,%r13d
movsbl %r15b,%r14d
lea    0xe75(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    %r14d,%ecx
call   1030 <printf@plt>
movslq %ebp,%rbp
mov    0x10(%rsp),%rax
mov    %r14b,(%rax,%rbp,1)
mov    %r13d,%r14d
mov    %r12d,%r13d
mov    0xc(%rsp),%r12d
add    $0x1,%ebp
cmp    %r14b,%r15b
jne    11d9 <serialport_read_until+0xa9>
jmp    11e6 <serialport_read_until+0xb6>
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1050 <usleep@plt>
add    $0xffffffff,%r12d
movzbl 0xb(%rsp),%r15d
cmp    %r14b,%r15b
je     11e6 <serialport_read_until+0xb6>
cmp    %ebx,%ebp
jge    11e6 <serialport_read_until+0xb6>
test   %r12d,%r12d
jg     1150 <serialport_read_until+0x20>
movslq %ebp,%rax
mov    0x10(%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"serialport_read_until: i=%d, n=%d b='%c'\0A\00"
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 = %57, %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 %64
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 %43
29: ; preds = %22
%30 = load i32, i32* %13
%31 = load i32, i32* %14
%32 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%33 = load i8, i8* %32
%34 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %31, i8 signext %33)
%35 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%36 = load i8, i8* %35
%37 = load i8*, i8** %8
%38 = load i32, i32* %13
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
%41 = load i32, i32* %13
%42 = add nsw i32 %41, 1
store i32 %42, i32* %13
br label %43
43: ; preds = %29, %25
%44 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = load i8, i8* %9
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %46, %48
br i1 %49, label %50, label %57
50: ; preds = %43
%51 = load i32, i32* %13
%52 = load i32, i32* %10
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* %11
%56 = icmp sgt i32 %55, 0
br label %57
57: ; preds = %54, %50, %43
%58 = phi i1 [ false, %50 ], [ false, %43 ], [ %56, %54 ]
br i1 %58, label %15, label %59
59: ; preds = %57
%60 = load i8*, i8** %8
%61 = load i32, i32* %13
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
store i32 0, i32* %6
br label %64
64: ; preds = %59, %21
%65 = load i32, i32* %6
ret i32 %65
}

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