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


x86 assembly (clang, -O0):
<serialport_read_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1165 <serialport_read_until+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <serialport_read_until+0x98>
cmpl   $0x0,-0x1c(%rbp)
jne    117e <serialport_read_until+0x5e>
mov    $0x2710,%edi
call   1040 <usleep@plt>
jmp    1195 <serialport_read_until+0x75>
mov    -0x12(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movsbl -0x12(%rbp),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jne    113b <serialport_read_until+0x1b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_read_until>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%ebx
xor    %ebp,%ebp
lea    0xf(%rsp),%r15
jmp    1154 <serialport_read_until+0x34>
nopl   (%rax)
mov    $0x2710,%edi
call   1040 <usleep@plt>
movzbl 0xf(%rsp),%eax
cmp    %r12b,%al
je     1180 <serialport_read_until+0x60>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     1140 <serialport_read_until+0x20>
cmp    $0xffffffff,%eax
je     118a <serialport_read_until+0x6a>
movzbl 0xf(%rsp),%eax
movslq %ebp,%rbp
mov    %al,(%r14,%rbp,1)
add    $0x1,%ebp
cmp    %r12b,%al
jne    1154 <serialport_read_until+0x34>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serialport_read_until(i32 %0, i8* %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
%8 = alloca [1 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8 %2, i8* %7
store i32 0, i32* %9
br label %11
11: ; preds = %32, %3
%12 = load i32, i32* %5
%13 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 1)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 -1, i32* %4
br label %44
18: ; preds = %11
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @usleep(i32 10000)
br label %32
23: ; preds = %18
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%25 = load i8, i8* %24
%26 = load i8*, i8** %6
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %32
32: ; preds = %23, %21
%33 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = load i8, i8* %7
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %35, %37
br i1 %38, label %11, label %39
39: ; preds = %32
%40 = load i8*, i8** %6
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
store i32 0, i32* %4
br label %44
44: ; preds = %39, %17
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
define i32 @serialport_read_until(i32 %0, i8* nocapture writeonly %1, i8 signext %2) {
%4 = alloca [1 x i8]
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
br label %6
6: ; preds = %17, %3
%7 = phi i32 [ 0, %3 ], [ %19, %17 ]
%8 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %8, label %12 [
i32 -1, label %24
i32 0, label %9
]
9: ; preds = %6
%10 = tail call i32 @usleep(i32 10000)
%11 = load i8, i8* %5
br label %17
12: ; preds = %6
%13 = load i8, i8* %5
%14 = sext i32 %7 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %13, i8* %15
%16 = add nsw i32 %7, 1
br label %17
17: ; preds = %9, %12
%18 = phi i8 [ %13, %12 ], [ %11, %9 ]
%19 = phi i32 [ %16, %12 ], [ %7, %9 ]
%20 = icmp eq i8 %18, %2
br i1 %20, label %21, label %6
21: ; preds = %17
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %6, %21
%25 = phi i32 [ 0, %21 ], [ %8, %6 ]
ret i32 %25
}