#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);
scalar_t__ toupper(char const);
int find_channel(int fd, const char *channel) {
  int character = 0;
  while (1) {
    char c;
    if (read(fd, &c, 1) < 1)
      return -1;
    if ('\n' == c)
      character = 0;
    else if (character >= 0) {
      if (c == ':' && channel[character] == '\0')
        break;
      if (toupper(c) == toupper(channel[character]))
        character++;
      else
        character = -1;
    }
  };
  return 0;
}


x86 assembly (clang, -O0):
<find_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jge    115c <find_channel+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <find_channel+0xe3>
movsbl -0x15(%rbp),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1179 <find_channel+0x59>
movl   $0x0,-0x14(%rbp)
jmp    11f7 <find_channel+0xd7>
cmpl   $0x0,-0x14(%rbp)
jl     11f2 <find_channel+0xd2>
movsbl -0x15(%rbp),%eax
cmp    $0x3a,%eax
jne    11aa <find_channel+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11aa <find_channel+0x8a>
jmp    11fc <find_channel+0xdc>
movsbl -0x15(%rbp),%edi
call   1030 <toupper@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jne    11e6 <find_channel+0xc6>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ed <find_channel+0xcd>
movl   $0xffffffff,-0x14(%rbp)
jmp    11f2 <find_channel+0xd2>
jmp    11f7 <find_channel+0xd7>
jmp    1136 <find_channel+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_channel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r12d
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11a9 <find_channel+0x89>
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
jmp    1168 <find_channel+0x48>
add    $0x1,%ebp
mov    %ebp,%ebx
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %ebx,%ebp
test   %eax,%eax
jle    11a9 <find_channel+0x89>
movzbl 0x7(%rsp),%eax
mov    $0x0,%ebx
cmp    $0xa,%al
je     1152 <find_channel+0x32>
test   %ebp,%ebp
js     1150 <find_channel+0x30>
mov    %ebp,%r13d
cmp    $0x3a,%al
jne    1188 <find_channel+0x68>
cmpb   $0x0,(%r14,%r13,1)
je     11bd <find_channel+0x9d>
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %rax,%rbx
movsbl (%r14,%r13,1),%edi
call   1030 <toupper@plt>
cmp    %rax,%rbx
je     114d <find_channel+0x2d>
mov    $0xffffffff,%ebx
jmp    1152 <find_channel+0x32>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    11ae <find_channel+0x8e>

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

LLVM IR (-O2):
define i32 @find_channel(i32 %0, i8* nocapture %1) {
%3 = alloca i8
%4 = call i32 @read(i32 %0, i8* %3, i32 1)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %7
6: ; preds = %28, %2
br label %33
7: ; preds = %2, %28
%8 = phi i32 [ %29, %28 ], [ 0, %2 ]
%9 = load i8, i8* %3
%10 = icmp eq i8 %9, 10
br i1 %10, label %28, label %11
11: ; preds = %7
%12 = icmp sgt i32 %8, -1
br i1 %12, label %13, label %28
13: ; preds = %11
%14 = icmp eq i8 %9, 58
%15 = zext i32 %8 to i64
br i1 %14, label %16, label %20
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %1, i64 %15
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %13, %16
%21 = tail call i64 @toupper(i8 signext %9)
%22 = getelementptr inbounds i8, i8* %1, i64 %15
%23 = load i8, i8* %22
%24 = tail call i64 @toupper(i8 signext %23)
%25 = icmp eq i64 %21, %24
%26 = add nuw nsw i32 %8, 1
%27 = select i1 %25, i32 %26, i32 -1
br label %28
28: ; preds = %20, %11, %7
%29 = phi i32 [ %8, %11 ], [ 0, %7 ], [ %27, %20 ]
%30 = call i32 @read(i32 %0, i8* %3, i32 1)
%31 = icmp slt i32 %30, 1
br i1 %31, label %6, label %7
32: ; preds = %16
br label %33
33: ; preds = %6, %32
%34 = phi i32 [ 0, %32 ], [ -1, %6 ]
ret i32 %34
}