#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 size_t uint32_t;
typedef int ssize_t;

/* Variables and functions */
int read_msg(size_t, void *);
int read_uint32(size_t *);
ssize_t read_data(ssize_t size, void *data) {
  ssize_t r;
  uint32_t data_length;
  if ((r = read_uint32(&data_length)) == -1)
    return r;
  if (data_length > (size_t)size)
    return -1;
  if ((r = read_msg(data_length, data)) == -1)
    return r;
  return r;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <read_uint32@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    114f <read_data+0x2f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <read_data+0x76>
mov    -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jbe    116c <read_data+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <read_data+0x76>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <read_msg@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1190 <read_data+0x70>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <read_data+0x76>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rdi
call   1030 <read_uint32@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     1150 <read_data+0x30>
mov    (%rsp),%rdi
movslq %ebp,%rcx
cmp    %rcx,%rdi
ja     1150 <read_data+0x30>
mov    %rbx,%rsi
call   1040 <read_msg@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_data(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = call i32 @read_uint32(i64* %7)
store i32 %8, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* %6
store i32 %11, i32* %3
br label %27
12: ; preds = %2
%13 = load i64, i64* %7
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = icmp ugt i64 %13, %15
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %3
br label %27
18: ; preds = %12
%19 = load i64, i64* %7
%20 = load i8*, i8** %5
%21 = call i32 @read_msg(i64 %19, i8* %20)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %27
25: ; preds = %18
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %25, %23, %17, %10
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
define i32 @read_data(i32 %0, i8* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = call i32 @read_uint32(i64* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = load i64, i64* %3
%9 = sext i32 %0 to i64
%10 = icmp ugt i64 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = call i32 @read_msg(i64 %8, i8* %1)
br label %13
13: ; preds = %11, %7, %2
%14 = phi i32 [ -1, %2 ], [ -1, %7 ], [ %12, %11 ]
ret i32 %14
}