#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*/ extended_show_mem();
int /*<<< orphan*/ print_error(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ simple_show_mem();
void message_loop(int erlin_fd) {
  char cmdLen, cmd;
  int res;
  while (1) {
    if ((res = read(erlin_fd, &cmdLen, 1)) < 0) {
      print_error("Error reading from Erlang");
      return;
    }
    if (res == 1) {
      if (cmdLen == 1) {
        switch (read(erlin_fd, &cmd, 1)) {
        case 1:
          switch (cmd) {
          case 1:
            simple_show_mem();
            break;
          case 2:
            extended_show_mem();
            break;
          default:
            break;
          }
          break;
        case 0:
          print_error("Erlang has closed");
          return;
        default:
          print_error("Error reading from Erlang");
          return;
        }
      } else {
        print_error("Invalid command length (%d) received", cmdLen);
        return;
      }
    } else {
      print_error("Erlang has closed");
      return;
    }
  }
}


x86 assembly (clang, -O0):
<message_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    117b <message_loop+0x3b>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <print_error@plt>
jmp    126d <message_loop+0x12d>
cmpl   $0x1,-0xc(%rbp)
jne    1255 <message_loop+0x115>
movsbl -0x5(%rbp),%eax
cmp    $0x1,%eax
jne    1239 <message_loop+0xf9>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
test   %eax,%eax
je     120e <message_loop+0xce>
jmp    11b5 <message_loop+0x75>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
jne    1221 <message_loop+0xe1>
jmp    11c6 <message_loop+0x86>
movsbl -0x6(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x1,%eax
je     11ec <message_loop+0xac>
jmp    11db <message_loop+0x9b>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
je     11f8 <message_loop+0xb8>
jmp    1204 <message_loop+0xc4>
mov    $0x0,%al
call   1040 <simple_show_mem@plt>
jmp    1209 <message_loop+0xc9>
mov    $0x0,%al
call   1060 <extended_show_mem@plt>
jmp    1209 <message_loop+0xc9>
jmp    1209 <message_loop+0xc9>
jmp    1234 <message_loop+0xf4>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <print_error@plt>
jmp    126d <message_loop+0x12d>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1030 <print_error@plt>
jmp    126d <message_loop+0x12d>
jmp    1250 <message_loop+0x110>
movsbl -0x5(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <print_error@plt>
jmp    126d <message_loop+0x12d>
jmp    1268 <message_loop+0x128>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <print_error@plt>
jmp    126d <message_loop+0x12d>
jmp    114b <message_loop+0xb>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<message_loop>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11d2 <message_loop+0x92>
lea    0xf(%rsp),%r14
lea    0xe(%rsp),%r15
jmp    118a <message_loop+0x4a>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <extended_show_mem@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11d2 <message_loop+0x92>
cmp    $0x1,%eax
jne    11c9 <message_loop+0x89>
movsbl 0xe(%rsp),%esi
cmp    $0x1,%esi
jne    11e9 <message_loop+0xa9>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11c5 <message_loop+0x85>
movsbl 0xf(%rsp),%eax
cmp    $0x2,%eax
je     1170 <message_loop+0x30>
cmp    $0x1,%eax
jne    1177 <message_loop+0x37>
xor    %eax,%eax
call   1040 <simple_show_mem@plt>
jmp    1177 <message_loop+0x37>
test   %eax,%eax
jne    11d2 <message_loop+0x92>
lea    0xe4a(%rip),%rdi
jmp    11d9 <message_loop+0x99>
lea    0xe27(%rip),%rdi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <print_error@plt>
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <print_error@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Error reading from Erlang\00"
@.str.1 = [18 x i8] c"Erlang has closed\00"
@.str.2 = [37 x i8] c"Invalid command length (%d) received\00"
define void @message_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %43
%7 = load i32, i32* %2
%8 = call i32 @read(i32 %7, i8* %3, i32 1)
store i32 %8, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %44
12: ; preds = %6
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %41
15: ; preds = %12
%16 = load i8, i8* %3
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %36
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i32 @read(i32 %20, i8* %4, i32 1)
switch i32 %21, label %33 [
i32 1, label %22
i32 0, label %31
]
22: ; preds = %19
%23 = load i8, i8* %4
%24 = sext i8 %23 to i32
switch i32 %24, label %29 [
i32 1, label %25
i32 2, label %27
]
25: ; preds = %22
%26 = call i32 (...) @simple_show_mem()
br label %30
27: ; preds = %22
%28 = call i32 (...) @extended_show_mem()
br label %30
29: ; preds = %22
br label %30
30: ; preds = %29, %27, %25
br label %35
31: ; preds = %19
%32 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %44
33: ; preds = %19
%34 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %44
35: ; preds = %30
br label %40
36: ; preds = %15
%37 = load i8, i8* %3
%38 = sext i8 %37 to i32
%39 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %38)
br label %44
40: ; preds = %35
br label %43
41: ; preds = %12
%42 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %44
43: ; preds = %40
br label %6
44: ; preds = %41, %36, %33, %31, %10
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Error reading from Erlang\00"
@.str.1 = [18 x i8] c"Erlang has closed\00"
@.str.2 = [37 x i8] c"Invalid command length (%d) received\00"
define void @message_loop(i32 %0) {
%2 = alloca i8
%3 = alloca i8
%4 = call i32 @read(i32 %0, i8* %2, i32 1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %32, %1
%7 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %35
8: ; preds = %1, %32
%9 = phi i32 [ %33, %32 ], [ %4, %1 ]
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %30
11: ; preds = %8
%12 = load i8, i8* %2
%13 = icmp eq i8 %12, 1
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = call i32 @read(i32 %0, i8* %3, i32 1)
switch i32 %15, label %25 [
i32 1, label %16
i32 0, label %23
]
16: ; preds = %14
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
switch i32 %18, label %32 [
i32 1, label %19
i32 2, label %21
]
19: ; preds = %16
%20 = tail call i32 (...) @simple_show_mem()
br label %32
21: ; preds = %16
%22 = tail call i32 (...) @extended_show_mem()
br label %32
23: ; preds = %14
%24 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %35
25: ; preds = %14
%26 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %35
27: ; preds = %11
%28 = sext i8 %12 to i32
%29 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %35
30: ; preds = %8
%31 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %35
32: ; preds = %19, %21, %16
%33 = call i32 @read(i32 %0, i8* %2, i32 1)
%34 = icmp slt i32 %33, 0
br i1 %34, label %6, label %8
35: ; preds = %30, %27, %25, %23, %6
ret void
}