#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 */
char *strchr(char const *, char);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
ssize_t read_protocol(const char *line, char *protocol, size_t protocol_size) {
  const char *start, *end;
  ssize_t length;
  start = strrchr(line, ' ');
  if (start) {
    start++;
  } else {
    start = line;
  }
  end = strchr(start, ':');
  if (!end) {
    goto error;
  }
  length = end - start;
  if ((size_t)(length + 1) > protocol_size) {
    goto error;
  }
  strncpy(protocol, start, length);
  protocol[length] = '\0';
out:
  return length;
error:
  length = -1;
  goto out;
}


x86 assembly (clang, -O0):
<read_protocol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1172 <read_protocol+0x42>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    117a <read_protocol+0x4a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <read_protocol+0x6c>
jmp    11e6 <read_protocol+0xb6>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    11c1 <read_protocol+0x91>
jmp    11e6 <read_protocol+0xb6>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret
movl   $0xffffffff,-0x2c(%rbp)
jmp    11dd <read_protocol+0xad>

x86 assembly (clang, -O2):
<read_protocol>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x20,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r12,%rbx
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,%r12
mov    $0xffffffff,%eax
test   %r12,%r12
je     11ab <read_protocol+0x7b>
sub    %rbx,%r12
mov    %r12,%r13
shl    $0x20,%r13
movabs $0x100000000,%rcx
add    %r13,%rcx
sar    $0x20,%rcx
cmp    %r15,%rcx
ja     11ab <read_protocol+0x7b>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
sar    $0x20,%r13
movb   $0x0,(%r14,%r13,1)
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @read_protocol(i8* %0, i8* %1, i64 %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 32)
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 1
%7 = select i1 %5, i8* %0, i8* %6
%8 = tail call i8* @strchr(i8* dereferenceable(1) %7, i8 signext 58)
%9 = icmp eq i8* %8, null
br i1 %9, label %23, label %10
10: ; preds = %3
%11 = ptrtoint i8* %8 to i64
%12 = ptrtoint i8* %7 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = shl i64 %13, 32
%16 = add i64 %15, 4294967296
%17 = ashr exact i64 %16, 32
%18 = icmp ugt i64 %17, %2
br i1 %18, label %23, label %19
19: ; preds = %10
%20 = tail call i32 @strncpy(i8* %1, i8* %7, i32 %14)
%21 = ashr exact i64 %15, 32
%22 = getelementptr inbounds i8, i8* %1, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %3, %10, %19
%24 = phi i32 [ %14, %19 ], [ -1, %10 ], [ -1, %3 ]
ret i32 %24
}