#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 /*<<< orphan*/ abort();
int /*<<< orphan*/ disconnect(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
char *mystrdup(char *);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
char *next_message(int socket) {
  char *complete_msg;
  int offset = 0, end = 0, i, msg_lenght;
  char buffer[8192];
  memset(buffer, '\0', 8192);
  do {
    msg_lenght = read(socket, (void *)(buffer + offset), (8192 - offset));
    if (msg_lenght == -1) {
      fprintf(stderr, "Socket read error.\n");
      disconnect(socket);
      abort();
    }
    offset += msg_lenght;
    for (i = 0; i < 8192 - 1; i++) {
      if (buffer[i] == '\r' && buffer[i + 1] == '\n') {
        end = 1;
        break;
      }
    }
  } while (offset != 8192 && end == 0);
  if (end == 0) {
    fputs("Incoming message is badly formated.\n", stderr);
    disconnect(socket);
    abort();
  }
  buffer[i] = '\0';
  complete_msg = mystrdup(buffer);
  fprintf(stderr, "Incoming msg: '%s'\n", complete_msg);
  return complete_msg;
}


x86 assembly (clang, -O0):
<next_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x2020(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x2020(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x2000,%edx
sub    -0x14(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f0 <next_message+0x80>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1090 <disconnect@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1fff,-0x1c(%rbp)
jge    125a <next_message+0xea>
movslq -0x1c(%rbp),%rax
movsbl -0x2020(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
jne    1247 <next_message+0xd7>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
movsbl -0x2020(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1247 <next_message+0xd7>
movl   $0x1,-0x18(%rbp)
jmp    125a <next_message+0xea>
jmp    124c <next_message+0xdc>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1200 <next_message+0x90>
jmp    125f <next_message+0xef>
xor    %eax,%eax
cmpl   $0x2000,-0x14(%rbp)
mov    %al,-0x2021(%rbp)
je     1281 <next_message+0x111>
cmpl   $0x0,-0x18(%rbp)
sete   %al
mov    %al,-0x2021(%rbp)
mov    -0x2021(%rbp),%al
test   $0x1,%al
jne    119f <next_message+0x2f>
cmpl   $0x0,-0x18(%rbp)
jne    12bd <next_message+0x14d>
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
lea    0xd6b(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x4(%rbp),%edi
call   1090 <disconnect@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
lea    -0x2020(%rbp),%rdi
call   1040 <mystrdup@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_message>:
push   %rbp
push   %rbx
sub    $0x2008,%rsp
mov    %edi,%ebp
mov    %rsp,%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x2000,%edx
call   1060 <memset@plt>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x2000,%edx
sub    %eax,%edx
mov    %ebp,%edi
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1243 <next_message+0xd3>
add    %eax,%ebx
xor    %eax,%eax
jmp    11b4 <next_message+0x44>
add    $0x2,%rax
cmpb   $0xd,(%rsp,%rax,1)
jne    11c1 <next_message+0x51>
cmpb   $0xa,0x1(%rsp,%rax,1)
je     120b <next_message+0x9b>
cmp    $0x1ffe,%rax
je     11e0 <next_message+0x70>
cmpb   $0xd,0x1(%rsp,%rax,1)
jne    11b0 <next_message+0x40>
cmpb   $0xa,0x2(%rsp,%rax,1)
jne    11b0 <next_message+0x40>
jmp    1207 <next_message+0x97>
nopl   0x0(%rax)
cmp    $0x2000,%ebx
je     11f0 <next_message+0x80>
cmp    $0x1ffd,%rax
ja     118c <next_message+0x1c>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
call   1050 <fputs@plt>
jmp    125a <next_message+0xea>
add    $0x1,%rax
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1040 <mystrdup@plt>
mov    %rax,%rbx
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rax
add    $0x2008,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1090 <disconnect@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Socket read error.\0A\00"
@.str.1 = [37 x i8] c"Incoming message is badly formated.\0A\00"
@.str.2 = [20 x i8] c"Incoming msg: '%s'\0A\00"
define i8* @next_message(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8192 x i8]
store i32 %0, i32* %2
store i32 0, i32* %4
store i32 0, i32* %5
%9 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 8192)
br label %11
11: ; preds = %62, %1
%12 = load i32, i32* %2
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i32, i32* %4
%18 = sub nsw i32 8192, %17
%19 = call i32 @read(i32 %12, i8* %16, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @disconnect(i32 %25)
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %11
%29 = load i32, i32* %7
%30 = load i32, i32* %4
%31 = add nsw i32 %30, %29
store i32 %31, i32* %4
store i32 0, i32* %6
br label %32
32: ; preds = %52, %28
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 8191
br i1 %34, label %35, label %55
35: ; preds = %32
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 13
br i1 %41, label %42, label %51
42: ; preds = %35
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 10
br i1 %49, label %50, label %51
50: ; preds = %42
store i32 1, i32* %5
br label %55
51: ; preds = %42, %35
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %32
55: ; preds = %50, %32
br label %56
56: ; preds = %55
%57 = load i32, i32* %4
%58 = icmp ne i32 %57, 8192
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* %5
%61 = icmp eq i32 %60, 0
br label %62
62: ; preds = %59, %56
%63 = phi i1 [ false, %56 ], [ %61, %59 ]
br i1 %63, label %11, label %64
64: ; preds = %62
%65 = load i32, i32* %5
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %64
%68 = load i32, i32* @stderr
%69 = call i32 @fputs(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %2
%71 = call i32 @disconnect(i32 %70)
%72 = call i32 (...) @abort()
unreachable
73: ; preds = %64
%74 = load i32, i32* %6
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 %75
store i8 0, i8* %76
%77 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%78 = call i8* @mystrdup(i8* %77)
store i8* %78, i8** %3
%79 = load i32, i32* @stderr
%80 = load i8*, i8** %3
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %80)
%82 = load i8*, i8** %3
ret i8* %82
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Socket read error.\0A\00"
@.str.1 = [37 x i8] c"Incoming message is badly formated.\0A\00"
@.str.2 = [20 x i8] c"Incoming msg: '%s'\0A\00"
define i8* @next_message(i32 %0) {
%2 = alloca [8192 x i8]
%3 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i8 signext 0, i32 8192)
br label %5
5: ; preds = %41, %1
%6 = phi i32 [ 0, %1 ], [ %18, %41 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %7
%9 = sub nsw i32 8192, %6
%10 = call i32 @read(i32 %0, i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @disconnect(i32 %0)
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %5
%18 = add nsw i32 %10, %6
br label %19
19: ; preds = %40, %17
%20 = phi i64 [ 0, %17 ], [ %35, %40 ]
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 13
%24 = or i64 %20, 1
br i1 %23, label %25, label %29
25: ; preds = %19
%26 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %24
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 10
br i1 %28, label %50, label %29
29: ; preds = %19, %25
%30 = icmp eq i64 %24, 8191
br i1 %30, label %41, label %31
31: ; preds = %29
%32 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %24
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 13
%35 = add nuw nsw i64 %20, 2
br i1 %34, label %36, label %40
36: ; preds = %31
%37 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %35
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 10
br i1 %39, label %50, label %40
40: ; preds = %36, %31
br label %19
41: ; preds = %29
%42 = icmp ugt i64 %20, 8189
%43 = icmp ne i32 %18, 8192
%44 = select i1 %43, i1 %42, i1 false
br i1 %44, label %5, label %45
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 @fputs(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = call i32 @disconnect(i32 %0)
%49 = call i32 (...) @abort()
unreachable
50: ; preds = %36, %25
%51 = phi i64 [ %20, %25 ], [ %24, %36 ]
%52 = and i64 %51, 4294967295
%53 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %52
store i8 0, i8* %53
%54 = call i8* @mystrdup(i8* %3)
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %54)
ret i8* %54
}