#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 uint8_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int pmt_read_group(FILE *fh, uint8_t *buf, int buf_size) {
  uint16_t group_size;
  int bytes_read = fread(&group_size, 1, 2, fh);
  if (bytes_read != 2) {
    printf("ERROR: failed to read size for the group.\n");
    return -1;
  }
  if (group_size > buf_size) {
    printf("ERROR: group data (%d bytes) does not fit in buffer (%d bytes).\n",
           group_size, buf_size);
    return -1;
  }
  bytes_read = fread(buf, 1, group_size, fh);
  if (bytes_read != group_size) {
    printf("ERROR: failed to read %d group data bytes.\n", group_size);
    return -1;
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<pmt_read_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
je     1171 <pmt_read_group+0x51>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <pmt_read_group+0xc4>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    119d <pmt_read_group+0x7d>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <pmt_read_group+0xc4>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     11de <pmt_read_group+0xbe>
mov    -0x20(%rbp),%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <pmt_read_group+0xc4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pmt_read_group>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
cmp    $0x2,%eax
jne    117a <pmt_read_group+0x4a>
mov    0x4(%rsp),%ebx
cmp    %r14d,%ebx
jle    1188 <pmt_read_group+0x58>
lea    0xe94(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11ae <pmt_read_group+0x7e>
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
jmp    11ae <pmt_read_group+0x7e>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
cmp    %ebx,%eax
je     11b3 <pmt_read_group+0x83>
lea    0xe9c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"ERROR: failed to read size for the group.\0A\00"
@.str.1 = [65 x i8] c"ERROR: group data (%d bytes) does not fit in buffer (%d bytes).\0A\00"
@.str.2 = [44 x i8] c"ERROR: failed to read %d group data bytes.\0A\00"
define i32 @pmt_read_group(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i32*, i32** %5
%11 = call i32 @fread(i32* %8, i32 1, i32 2, i32* %10)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %37
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32, i32* %7
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22)
store i32 -1, i32* %4
br label %37
24: ; preds = %16
%25 = load i32*, i32** %6
%26 = load i32, i32* %8
%27 = load i32*, i32** %5
%28 = call i32 @fread(i32* %25, i32 1, i32 %26, i32* %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = load i32, i32* %8
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = load i32, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 -1, i32* %4
br label %37
35: ; preds = %24
%36 = load i32, i32* %9
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %32, %20, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str.1 = [65 x i8] c"ERROR: group data (%d bytes) does not fit in buffer (%d bytes).\0A\00"
@.str.2 = [44 x i8] c"ERROR: failed to read %d group data bytes.\0A\00"
@str = [42 x i8] c"ERROR: failed to read size for the group.\00"
define i32 @pmt_read_group(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @fread(i32* %4, i32 1, i32 2, i32* %0)
%7 = icmp eq i32 %6, 2
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %20
10: ; preds = %3
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, %2
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %2)
br label %20
15: ; preds = %10
%16 = tail call i32 @fread(i32* %1, i32 1, i32 %11, i32* %0)
%17 = icmp eq i32 %16, %11
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %20
20: ; preds = %15, %18, %13, %8
%21 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %18 ], [ %11, %15 ]
ret i32 %21
}