#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 (c == ':' && channel[character] == '\0')
      break;
    if (toupper(c) == toupper(channel[character]))
      character++;
    else
      character = 0;
  };
  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    11d2 <find_channel+0xb2>
movsbl -0x15(%rbp),%eax
cmp    $0x3a,%eax
jne    1183 <find_channel+0x63>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1183 <find_channel+0x63>
jmp    11cb <find_channel+0xab>
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    11bf <find_channel+0x9f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c6 <find_channel+0xa6>
movl   $0x0,-0x14(%rbp)
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,%r12
mov    %edi,%r15d
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    119a <find_channel+0x7a>
xor    %r13d,%r13d
lea    0x7(%rsp),%r14
jmp    1167 <find_channel+0x47>
xchg   %ax,%ax
xor    %r13d,%r13d
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    119a <find_channel+0x7a>
movzbl 0x7(%rsp),%eax
movslq %r13d,%rbx
cmp    $0x3a,%al
jne    117a <find_channel+0x5a>
cmpb   $0x0,(%r12,%rbx,1)
je     11a1 <find_channel+0x81>
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %rax,%rbp
movsbl (%r12,%rbx,1),%edi
call   1030 <toupper@plt>
cmp    %rax,%rbp
jne    1150 <find_channel+0x30>
add    $0x1,%r13d
jmp    1153 <find_channel+0x33>
mov    $0xffffffff,%eax
jmp    11a3 <find_channel+0x83>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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, %40
%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 %42
13: ; preds = %8
%14 = load i8, i8* %7
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 58
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
br label %41
26: ; preds = %17, %13
%27 = load i8, i8* %7
%28 = call i64 @toupper(i8 signext %27)
%29 = load i8*, i8** %5
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = call i64 @toupper(i8 signext %33)
%35 = icmp eq i64 %28, %34
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
br label %40
39: ; preds = %26
store i32 0, i32* %6
br label %40
40: ; preds = %39, %36
br label %8
41: ; preds = %25
store i32 0, i32* %3
br label %42
42: ; preds = %41, %12
%43 = load i32, i32* %3
ret i32 %43
}

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 = %16, %2
br label %27
7: ; preds = %2, %16
%8 = phi i32 [ %23, %16 ], [ 0, %2 ]
%9 = load i8, i8* %3
%10 = icmp eq i8 %9, 58
%11 = sext i32 %8 to i64
br i1 %10, label %12, label %16
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %1, i64 %11
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %7, %12
%17 = tail call i64 @toupper(i8 signext %9)
%18 = getelementptr inbounds i8, i8* %1, i64 %11
%19 = load i8, i8* %18
%20 = tail call i64 @toupper(i8 signext %19)
%21 = icmp eq i64 %17, %20
%22 = add nsw i32 %8, 1
%23 = select i1 %21, i32 %22, i32 0
%24 = call i32 @read(i32 %0, i8* %3, i32 1)
%25 = icmp slt i32 %24, 1
br i1 %25, label %6, label %7
26: ; preds = %12
br label %27
27: ; preds = %6, %26
%28 = phi i32 [ 0, %26 ], [ -1, %6 ]
ret i32 %28
}