#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int fd_compare(char *name, int fd, char *state) {
  while (1) {
    char c = 0;
    ssize_t result = read(fd, &c, 1);
    if (result == 1) {
      if (state[0] == '\0' || state[0] != c) {
        return 0;
      }
      state++;
    }
    if (result == 0) {
      if (state[0] == '\0') {
        return 1;
      }
      return 0;
    }
    if (result == -1 && errno != EINTR) {
      fprintf(stderr, "%s: read failed: %s\n", name, strerror(errno));
      return 2;
    }
  }
}


x86 assembly (clang, -O0):
<fd_compare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jne    11a0 <fd_compare+0x70>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1188 <fd_compare+0x58>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
je     1194 <fd_compare+0x64>
movl   $0x0,-0x4(%rbp)
jmp    1240 <fd_compare+0x110>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11d2 <fd_compare+0xa2>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11c6 <fd_compare+0x96>
movl   $0x1,-0x4(%rbp)
jmp    1240 <fd_compare+0x110>
movl   $0x0,-0x4(%rbp)
jmp    1240 <fd_compare+0x110>
cmpl   $0xffffffff,-0x28(%rbp)
jne    123b <fd_compare+0x10b>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
je     123b <fd_compare+0x10b>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd6(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1240 <fd_compare+0x110>
jmp    1143 <fd_compare+0x13>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_compare>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x7(%rsp),%r15
mov    0x2e89(%rip),%r12
mov    0x2e7a(%rip),%r13
jmp    116a <fd_compare+0x3a>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
jne    11af <fd_compare+0x7f>
movb   $0x0,0x7(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1160 <fd_compare+0x30>
cmp    $0x1,%eax
je     1190 <fd_compare+0x60>
test   %eax,%eax
jne    116a <fd_compare+0x3a>
jmp    11a5 <fd_compare+0x75>
xchg   %ax,%ax
movzbl (%rbx),%ecx
xor    %eax,%eax
test   %cl,%cl
je     11d6 <fd_compare+0xa6>
cmp    0x7(%rsp),%cl
jne    11d6 <fd_compare+0xa6>
add    $0x1,%rbx
jmp    116a <fd_compare+0x3a>
xor    %eax,%eax
cmpb   $0x0,(%rbx)
sete   %al
jmp    11d6 <fd_compare+0xa6>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe3c(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: read failed: %s\0A\00"
define i32 @fd_compare(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
br label %10
10: ; preds = %3, %57
store i8 0, i8* %8
%11 = load i32, i32* %6
%12 = call i32 @read(i32 %11, i8* %8, i32 1)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %33
15: ; preds = %10
%16 = load i8*, i8** %7
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %15
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = load i8, i8* %8
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %21, %15
store i32 0, i32* %4
br label %58
30: ; preds = %21
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %7
br label %33
33: ; preds = %30, %10
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 1, i32* %4
br label %58
43: ; preds = %36
store i32 0, i32* %4
br label %58
44: ; preds = %33
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINTR
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %5
%54 = load i64, i64* @errno
%55 = call i8* @strerror(i64 %54)
%56 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %53, i8* %55)
store i32 2, i32* %4
br label %58
57: ; preds = %47, %44
br label %10
58: ; preds = %51, %43, %42, %29
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: read failed: %s\0A\00"
define i32 @fd_compare(i8* %0, i32 %1, i8* nocapture %2) {
%4 = alloca i8
br label %5
5: ; preds = %28, %3
%6 = phi i8* [ %2, %3 ], [ %29, %28 ]
store i8 0, i8* %4
%7 = call i32 @read(i32 %1, i8* %4, i32 1)
switch i32 %7, label %28 [
i32 1, label %8
i32 0, label %16
i32 -1, label %20
]
8: ; preds = %5
%9 = load i8, i8* %6
%10 = icmp ne i8 %9, 0
%11 = load i8, i8* %4
%12 = icmp eq i8 %9, %11
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %30
14: ; preds = %8
%15 = getelementptr inbounds i8, i8* %6, i64 1
br label %28
16: ; preds = %5
%17 = load i8, i8* %6
%18 = icmp eq i8 %17, 0
%19 = zext i1 %18 to i32
br label %30
20: ; preds = %5
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i8* @strerror(i64 %21)
%27 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %26)
br label %30
28: ; preds = %5, %14, %20
%29 = phi i8* [ %6, %20 ], [ %15, %14 ], [ %6, %5 ]
br label %5
30: ; preds = %8, %24, %16
%31 = phi i32 [ %19, %16 ], [ 2, %24 ], [ 0, %8 ]
ret i32 %31
}