#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
scalar_t__ realloc(char *, int);
int read_message(int client, char **message) {
  int message_len = 0, message_idx = 0;
  int colon_count = 0;
  message_len = 1;
  message_idx = 0;
  *message = (char *)calloc(message_len, sizeof(char));
  do {
    int just_read = 0;
    if ((just_read = read(client, *message + message_idx, 1)) <= 0) {
      return -1;
    }
    *message = (char *)realloc(*message, sizeof(char) * (message_len + 1));
    memset(*message + message_len, 0, sizeof(char));
    if (*(*message + message_idx) == '\n')
      break;
    if (*(*message + message_idx) == ':')
      colon_count++;
    message_len += 1;
    message_idx += 1;
    ("message: %s\n", *message);
  } while (1);
  return colon_count;
}


x86 assembly (clang, -O0):
<read_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     11c3 <read_message+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <read_message+0x126>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    121e <read_message+0xde>
jmp    1260 <read_message+0x120>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3a,%eax
jne    123f <read_message+0xff>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    1189 <read_message+0x49>
jmp    1260 <read_message+0x120>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_message>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,(%rbx)
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11cc <read_message+0x8c>
xor    %r15d,%r15d
mov    $0x1,%ebp
jmp    119c <read_message+0x5c>
nopl   (%rax)
add    $0x1,%r15d
add    %rbp,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
add    $0x1,%rbp
test   %eax,%eax
jle    11cc <read_message+0x8c>
mov    (%rbx),%rdi
lea    0x1(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
lea    (%rax,%rbp,1),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <memset@plt>
mov    (%rbx),%rsi
movzbl -0x1(%rsi,%rbp,1),%eax
cmp    $0x3a,%al
je     1180 <read_message+0x40>
cmp    $0xa,%al
jne    1184 <read_message+0x44>
jmp    11d2 <read_message+0x92>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"message: %s\0A\00"
define i32 @read_message(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 1, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %6
%11 = call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = load i8**, i8*** %5
store i8* %12, i8** %13
br label %14
14: ; preds = %69, %2
store i32 0, i32* %9
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @read(i32 %15, i8* %20, i32 1)
store i32 %21, i32* %9
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
store i32 -1, i32* %3
br label %72
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = load i8*, i8** %25
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = mul i64 1, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @realloc(i8* %26, i32 %31)
%33 = inttoptr i64 %32 to i8*
%34 = load i8**, i8*** %5
store i8* %33, i8** %34
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = call i32 @memset(i8* %39, i32 0, i32 1)
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %50
49: ; preds = %24
br label %70
50: ; preds = %24
%51 = load i8**, i8*** %5
%52 = load i8*, i8** %51
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 58
br i1 %58, label %59, label %62
59: ; preds = %50
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %62
62: ; preds = %59, %50
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 1
store i32 %64, i32* %6
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 1
store i32 %66, i32* %7
%67 = load i8**, i8*** %5
%68 = load i8*, i8** %67
br label %69
69: ; preds = %62
br i1 true, label %14, label %70
70: ; preds = %69, %49
%71 = load i32, i32* %8
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %23
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
define i32 @read_message(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @calloc(i32 1, i32 1)
%4 = inttoptr i64 %3 to i8*
store i8* %4, i8** %1
%5 = tail call i32 @read(i32 %0, i8* %4, i32 1)
%6 = icmp slt i32 %5, 1
br i1 %6, label %29, label %7
7: ; preds = %2, %23
%8 = phi i64 [ %12, %23 ], [ 1, %2 ]
%9 = phi i64 [ %25, %23 ], [ 0, %2 ]
%10 = phi i32 [ %24, %23 ], [ 0, %2 ]
%11 = load i8*, i8** %1
%12 = add nuw i64 %8, 1
%13 = trunc i64 %12 to i32
%14 = tail call i64 @realloc(i8* %11, i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %1
%16 = getelementptr inbounds i8, i8* %15, i64 %8
%17 = tail call i32 @memset(i8* %16, i32 0, i32 1)
%18 = load i8*, i8** %1
%19 = getelementptr inbounds i8, i8* %18, i64 %9
%20 = load i8, i8* %19
switch i8 %20, label %23 [
i8 10, label %29
i8 58, label %21
]
21: ; preds = %7
%22 = add nsw i32 %10, 1
br label %23
23: ; preds = %21, %7
%24 = phi i32 [ %22, %21 ], [ %10, %7 ]
%25 = add nuw nsw i64 %9, 1
%26 = getelementptr inbounds i8, i8* %18, i64 %25
%27 = tail call i32 @read(i32 %0, i8* %26, i32 1)
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %7
29: ; preds = %7, %23, %2
%30 = phi i32 [ -1, %2 ], [ -1, %23 ], [ %10, %7 ]
ret i32 %30
}