#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 read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int serialport_read_until(int fd, char *buf, char until, int size) {
  char b[1];
  int i = 0;
  int flag_eq = 0;
  do {
    int n = read(fd, b, 1);
    if (n == -1) {
      printf("ERROR WHILE READING\n");
      return -1;
    }
    if (n == 0) {
      usleep(10 * 1000);
      printf("Waiting...\n");
      continue;
    }
    buf[i] = b[0];
    if (!size) {
      flag_eq = (buf[0] == until) ? 1 : 0;
    } else {
      flag_eq = (size == ++i) ? 1 : 0;
    }
  } while (!flag_eq);
  return i;
}


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)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    118b <serialport_read_until+0x5b>
lea    0xe86(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <serialport_read_until+0xee>
cmpl   $0x0,-0x28(%rbp)
jne    11b0 <serialport_read_until+0x80>
mov    $0x2710,%edi
call   1050 <usleep@plt>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
jmp    1207 <serialport_read_until+0xd7>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0x0,-0x18(%rbp)
jne    11e7 <serialport_read_until+0xb7>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
movsbl -0x11(%rbp),%esi
xor    %eax,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1202 <serialport_read_until+0xd2>
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
xor    %eax,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1207 <serialport_read_until+0xd7>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1155 <serialport_read_until+0x25>
mov    -0x20(%rbp),%eax
mov    %eax,-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    %ecx,%r12d
mov    %edx,0x4(%rsp)
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0x3(%rsp),%r13
lea    0xeab(%rip),%r14
jmp    1178 <serialport_read_until+0x48>
nopw   0x0(%rax,%rax,1)
mov    $0x2710,%edi
call   1050 <usleep@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
test   %eax,%eax
jne    11cd <serialport_read_until+0x9d>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1160 <serialport_read_until+0x30>
cmp    $0xffffffff,%eax
je     11bc <serialport_read_until+0x8c>
movzbl 0x3(%rsp),%eax
movslq %ebx,%rcx
mov    %al,(%r15,%rcx,1)
test   %r12d,%r12d
je     11ae <serialport_read_until+0x7e>
add    $0x1,%ebx
xor    %eax,%eax
cmp    %r12d,%ebx
sete   %al
jmp    1174 <serialport_read_until+0x44>
xor    %eax,%eax
mov    0x4(%rsp),%ecx
cmp    %cl,(%r15)
sete   %al
jmp    1174 <serialport_read_until+0x44>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"ERROR WHILE READING\0A\00"
@.str.1 = [12 x i8] c"Waiting...\0A\00"
define i32 @serialport_read_until(i32 %0, i8* %1, i8 signext %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
%10 = alloca [1 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8 %2, i8* %8
store i32 %3, i32* %9
store i32 0, i32* %11
store i32 0, i32* %12
br label %14
14: ; preds = %55, %4
%15 = load i32, i32* %6
%16 = getelementptr inbounds [1 x i8], [1 x i8]* %10, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 1)
store i32 %17, i32* %13
%18 = load i32, i32* %13
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %61
22: ; preds = %14
%23 = load i32, i32* %13
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @usleep(i32 10000)
%27 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %55
28: ; preds = %22
%29 = getelementptr inbounds [1 x i8], [1 x i8]* %10, i64 0, i64 0
%30 = load i8, i8* %29
%31 = load i8*, i8** %7
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 %30, i8* %34
%35 = load i32, i32* %9
%36 = icmp ne i32 %35, 0
br i1 %36, label %47, label %37
37: ; preds = %28
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = load i8, i8* %8
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %41, %43
%45 = zext i1 %44 to i64
%46 = select i1 %44, i32 1, i32 0
store i32 %46, i32* %12
br label %54
47: ; preds = %28
%48 = load i32, i32* %9
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
%51 = icmp eq i32 %48, %50
%52 = zext i1 %51 to i64
%53 = select i1 %51, i32 1, i32 0
store i32 %53, i32* %12
br label %54
54: ; preds = %47, %37
br label %55
55: ; preds = %54, %25
%56 = load i32, i32* %12
%57 = icmp ne i32 %56, 0
%58 = xor i1 %57, true
br i1 %58, label %14, label %59
59: ; preds = %55
%60 = load i32, i32* %11
store i32 %60, i32* %5
br label %61
61: ; preds = %59, %20
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
@str = [11 x i8] c"Waiting...\00"
@str.2 = [20 x i8] c"ERROR WHILE READING\00"
define i32 @serialport_read_until(i32 %0, i8* nocapture %1, i8 signext %2, i32 %3) {
%5 = alloca [1 x i8]
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%7 = icmp eq i32 %3, 0
br label %8
8: ; preds = %30, %4
%9 = phi i32 [ 0, %4 ], [ %31, %30 ]
%10 = call i32 @read(i32 %0, i8* %6, i32 1)
switch i32 %10, label %14 [
i32 -1, label %28
i32 0, label %11
]
11: ; preds = %8
%12 = tail call i32 @usleep(i32 10000)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %30
14: ; preds = %8
%15 = load i8, i8* %6
%16 = sext i32 %9 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %15, i8* %17
br i1 %7, label %18, label %21
18: ; preds = %14
%19 = load i8, i8* %1
%20 = icmp eq i8 %19, %2
br label %24
21: ; preds = %14
%22 = add nsw i32 %9, 1
%23 = icmp eq i32 %22, %3
br label %24
24: ; preds = %21, %18
%25 = phi i32 [ %22, %21 ], [ %9, %18 ]
%26 = phi i1 [ %23, %21 ], [ %20, %18 ]
%27 = zext i1 %26 to i32
br label %30
28: ; preds = %8
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.2, i64 0, i64 0))
br label %34
30: ; preds = %11, %24
%31 = phi i32 [ %25, %24 ], [ %9, %11 ]
%32 = phi i32 [ %27, %24 ], [ 0, %11 ]
%33 = icmp eq i32 %32, 0
br i1 %33, label %8, label %34
34: ; preds = %30, %28
%35 = phi i32 [ -1, %28 ], [ %31, %30 ]
ret i32 %35
}