#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, void *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int /*<<< orphan*/ write(int, char *, int);
int expect_char(int pty, char *expected, int seconds) {
  fd_set rfds;
  struct timeval tv;
  int rc;
  char c;
  do {
    if (seconds != 0) {
      FD_ZERO(&rfds);
      FD_SET(pty, &rfds);
      tv.tv_sec = seconds;
      tv.tv_usec = 0;
      rc = select(pty + 1, &rfds, 0, 0, &tv);
      if (rc <= 0) {
        return -1;
      }
    }
    rc = read(pty, (void *)&c, sizeof(char));
    if (rc > 0) {
      if (expected == 0) {
        write(1, &c, sizeof(char));
      }
      if (expected != 0 && c == *expected) {
        return 0;
      }
    }
  } while (rc > 0);
  return -1;
}


x86 assembly (clang, -O0):
<expect_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c4 <expect_char+0x74>
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
mov    %ecx,%edx
call   1060 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     11bf <expect_char+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <expect_char+0xf2>
jmp    11c4 <expect_char+0x74>
mov    -0x8(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    122c <expect_char+0xdc>
cmpq   $0x0,-0x10(%rbp)
jne    11fd <expect_char+0xad>
mov    $0x1,%edx
lea    -0x2d(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
cmpq   $0x0,-0x10(%rbp)
je     1227 <expect_char+0xd7>
movsbl -0x2d(%rbp),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1227 <expect_char+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1242 <expect_char+0xf2>
jmp    122c <expect_char+0xdc>
jmp    1231 <expect_char+0xe1>
cmpl   $0x0,-0x2c(%rbp)
jg     1162 <expect_char+0x12>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expect_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x1(%rbx),%r15d
lea    0x3(%rsp),%r12
lea    0x4(%rsp),%r13
jmp    1192 <expect_char+0x42>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %ebp,%ebp
je     11cd <expect_char+0x7d>
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
mov    %ebp,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r15d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
jle    11f3 <expect_char+0xa3>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11f3 <expect_char+0xa3>
test   %r14,%r14
je     1180 <expect_char+0x30>
movzbl 0x3(%rsp),%eax
cmp    (%r14),%al
jne    1192 <expect_char+0x42>
xor    %eax,%eax
jmp    11f8 <expect_char+0xa8>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @expect_char(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %12
12: ; preds = %52, %3
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = call i32 @FD_ZERO(i32* %8)
%17 = load i32, i32* %5
%18 = call i32 @FD_SET(i32 %17, i32* %8)
%19 = load i32, i32* %7
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %21
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
%24 = call i32 @select(i32 %23, i32* %8, i32 0, i32 0, %struct.timeval* %9)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %15
store i32 -1, i32* %4
br label %56
28: ; preds = %15
br label %29
29: ; preds = %28, %12
%30 = load i32, i32* %5
%31 = call i32 @read(i32 %30, i8* %11, i32 1)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %51
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @write(i32 1, i8* %11, i32 1)
br label %39
39: ; preds = %37, %34
%40 = load i8*, i8** %6
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i8, i8* %11
%44 = sext i8 %43 to i32
%45 = load i8*, i8** %6
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %44, %47
br i1 %48, label %49, label %50
49: ; preds = %42
store i32 0, i32* %4
br label %56
50: ; preds = %42, %39
br label %51
51: ; preds = %50, %29
br label %52
52: ; preds = %51
%53 = load i32, i32* %10
%54 = icmp sgt i32 %53, 0
br i1 %54, label %12, label %55
55: ; preds = %52
store i32 -1, i32* %4
br label %56
56: ; preds = %55, %49, %27
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @expect_char(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i8
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = icmp eq i32 %2, 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%12 = add nsw i32 %0, 1
%13 = icmp eq i8* %1, null
br label %14
14: ; preds = %30, %3
br i1 %9, label %20, label %15
15: ; preds = %14
%16 = call i32 @FD_ZERO(i32* %4)
%17 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 %2, i32* %10
store i64 0, i64* %11
%18 = call i32 @select(i32 %12, i32* %4, i32 0, i32 0, %struct.timeval* %5)
%19 = icmp slt i32 %18, 1
br i1 %19, label %31, label %20
20: ; preds = %15, %14
%21 = call i32 @read(i32 %0, i8* %6, i32 1)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
br i1 %13, label %24, label %26
24: ; preds = %23
%25 = call i32 @write(i32 1, i8* %6, i32 1)
br label %30
26: ; preds = %23
%27 = load i8, i8* %6
%28 = load i8, i8* %1
%29 = icmp eq i8 %27, %28
br i1 %29, label %31, label %30
30: ; preds = %26, %24
br label %14
31: ; preds = %20, %26, %15
%32 = phi i32 [ -1, %15 ], [ 0, %26 ], [ -1, %20 ]
ret i32 %32
}