#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);
int /*<<< orphan*/ strncpy(char *, char *, int);
int IRCMsgRead(int sockfd, char *raw_msg) {
  char buf[512];
  int bytes, total = 0;
  for (;;) {
    if (total >= 512)
      return -1;
    if ((bytes = read(sockfd, &buf[total], 1)) <= 0)
      return -1;
    if (buf[total] == '\r') {
      if (++total < 512) {
        if (read(sockfd, &buf[total], 1) <= 0)
          return -1;
        if (buf[total] == '\n') {
          buf[total - 1] = '\0';
          strncpy(raw_msg, buf, total);
          return total - 1;
        } else
          return -1;
      } else {
        return -1;
      }
    } else {
      total += bytes;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<IRCMsgRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x218(%rbp)
cmpl   $0x200,-0x218(%rbp)
jl     1158 <IRCMsgRead+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <IRCMsgRead+0x157>
mov    -0x8(%rbp),%edi
movslq -0x218(%rbp),%rax
lea    -0x210(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
jg     1191 <IRCMsgRead+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <IRCMsgRead+0x157>
movslq -0x218(%rbp),%rax
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
jne    1260 <IRCMsgRead+0x140>
mov    -0x218(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x218(%rbp)
cmp    $0x200,%eax
jge    1254 <IRCMsgRead+0x134>
mov    -0x8(%rbp),%edi
movslq -0x218(%rbp),%rax
lea    -0x210(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%eax
jg     11f6 <IRCMsgRead+0xd6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <IRCMsgRead+0x157>
movslq -0x218(%rbp),%rax
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1248 <IRCMsgRead+0x128>
mov    -0x218(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x210(%rbp),%rsi
mov    -0x218(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x218(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <IRCMsgRead+0x157>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <IRCMsgRead+0x157>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <IRCMsgRead+0x157>
mov    -0x214(%rbp),%eax
add    -0x218(%rbp),%eax
mov    %eax,-0x218(%rbp)
jmp    113c <IRCMsgRead+0x1c>
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IRCMsgRead>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %r12d,%r12d
mov    $0xffffffff,%ebp
nop
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rbx
add    $0x10,%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11c0 <IRCMsgRead+0xa0>
cmpb   $0xd,(%rbx)
je     1172 <IRCMsgRead+0x52>
add    %eax,%r12d
cmp    $0x1ff,%r12d
jle    1140 <IRCMsgRead+0x20>
jmp    11c0 <IRCMsgRead+0xa0>
cmp    $0x1fe,%r12d
jg     11c0 <IRCMsgRead+0xa0>
lea    0x1(%r12),%eax
mov    %eax,0xc(%rsp)
cltq
lea    (%rsp,%rax,1),%r13
add    $0x10,%r13
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11c0 <IRCMsgRead+0xa0>
cmpb   $0xa,0x0(%r13)
jne    11c0 <IRCMsgRead+0xa0>
movb   $0x0,(%rbx)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    0xc(%rsp),%edx
call   1030 <strncpy@plt>
mov    %r12d,%ebp
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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