#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 uint32_t;
typedef int ssize_t;
typedef int /*<<< orphan*/ length;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int read(int /*<<< orphan*/, void *, int);
int read_uint32(int *);
ssize_t read_msg(ssize_t size, void *data) {
  static uint32_t length;
  ssize_t len;
  if (size == -1) {
    length = sizeof(length);
    return read_uint32(&length);
  } else {
    len = read(STDIN_FILENO, data, size);
    if (len != -1)
      length -= len;
    if (len == -1 || len == 0 || len < size)
      return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<read_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1157 <read_msg+0x37>
movl   $0x4,0x2ef1(%rip)
lea    0x2eea(%rip),%rdi
call   1030 <read_uint32@plt>
mov    %eax,-0x4(%rbp)
jmp    11c1 <read_msg+0xa1>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     118a <read_msg+0x6a>
mov    -0x14(%rbp),%ecx
mov    0x2eb2(%rip),%eax
sub    %ecx,%eax
mov    %eax,0x2eaa(%rip)
cmpl   $0xffffffff,-0x14(%rbp)
je     11aa <read_msg+0x8a>
cmpl   $0x0,-0x14(%rbp)
je     11aa <read_msg+0x8a>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11b6 <read_msg+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <read_msg+0xa1>
jmp    11bb <read_msg+0x9b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_msg>:
push   %rbx
cmp    $0xffffffff,%edi
je     115c <read_msg+0x3c>
mov    %edi,%ebx
mov    0x2eb1(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1143 <read_msg+0x23>
sub    %eax,0x2ef1(%rip)
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
mov    $0xffffffff,%edx
mov    %eax,%ecx
cmovb  %edx,%ecx
cmp    %ebx,%eax
cmovl  %edx,%ecx
mov    %ecx,%eax
pop    %rbx
ret
movl   $0x4,0x2ece(%rip)
lea    0x2ec7(%rip),%rdi
pop    %rbx
jmp    1030 <read_uint32@plt>

LLVM IR (-O0):
@read_msg.length = internal global i32 0
@STDIN_FILENO = global i32 0
define i32 @read_msg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
store i32 4, i32* @read_msg.length
%10 = call i32 @read_uint32(i32* @read_msg.length)
store i32 %10, i32* %3
br label %36
11: ; preds = %2
%12 = load i32, i32* @STDIN_FILENO
%13 = load i8*, i8** %5
%14 = load i32, i32* %4
%15 = call i32 @read(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = load i32, i32* %6
%20 = load i32, i32* @read_msg.length
%21 = sub nsw i32 %20, %19
store i32 %21, i32* @read_msg.length
br label %22
22: ; preds = %18, %11
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %32, label %25
25: ; preds = %22
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = load i32, i32* %6
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28, %25, %22
store i32 -1, i32* %3
br label %36
33: ; preds = %28
br label %34
34: ; preds = %33
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %32, %9
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@read_msg.length = internal global i32 0
@STDIN_FILENO = global i32 0
define i32 @read_msg(i32 %0, i8* nocapture %1) {
%3 = icmp eq i32 %0, -1
br i1 %3, label %4, label %6
4: ; preds = %2
store i32 4, i32* @read_msg.length
%5 = tail call i32 @read_uint32(i32* @read_msg.length)
br label %19
6: ; preds = %2
%7 = load i32, i32* @STDIN_FILENO
%8 = tail call i32 @read(i32 %7, i8* %1, i32 %0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = load i32, i32* @read_msg.length
%12 = sub nsw i32 %11, %8
store i32 %12, i32* @read_msg.length
br label %13
13: ; preds = %10, %6
%14 = add i32 %8, 1
%15 = icmp ult i32 %14, 2
%16 = icmp slt i32 %8, %0
%17 = or i1 %16, %15
%18 = select i1 %17, i32 -1, i32 %8
br label %19
19: ; preds = %13, %4
%20 = phi i32 [ %5, %4 ], [ %18, %13 ]
ret i32 %20
}