#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*/ dd_log(int, char *, ...);
int read(int, char *, int);
int read_struct(int fd, char *struct_data, int struct_size, char *struct_name) {
  int buffer_read_bytes = 0;
  if ((buffer_read_bytes = read(fd, struct_data, struct_size)) == -1) {
    dd_log(10, "read_struct: Failed to read");
    return -1;
  }
  if (buffer_read_bytes != struct_size) {
    dd_log(10, "read_struct: Failed to read %llu bytes, actual read was %llu",
           struct_size, buffer_read_bytes);
    return -1;
  }
  return (0);
}


x86 assembly (clang, -O0):
<read_struct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1177 <read_struct+0x57>
mov    $0xa,%edi
lea    0xe9c(%rip),%rsi
mov    $0x0,%al
call   1030 <dd_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <read_struct+0x8f>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11a8 <read_struct+0x88>
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    $0xa,%edi
lea    0xe87(%rip),%rsi
mov    $0x0,%al
call   1030 <dd_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <read_struct+0x8f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_struct>:
push   %rbx
mov    %edx,%ebx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     114c <read_struct+0x2c>
xor    %ecx,%ecx
cmp    %ebx,%eax
je     1164 <read_struct+0x44>
lea    0xee2(%rip),%rsi
mov    $0xa,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <dd_log@plt>
jmp    115f <read_struct+0x3f>
lea    0xead(%rip),%rsi
mov    $0xa,%edi
xor    %eax,%eax
call   1030 <dd_log@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [28 x i8] c"read_struct: Failed to read\00"
@.str.1 = [61 x i8] c"read_struct: Failed to read %llu bytes, actual read was %llu\00"
define i32 @read_struct(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
%11 = load i32, i32* %6
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = call i32 @read(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %27
18: ; preds = %4
%19 = load i32, i32* %10
%20 = load i32, i32* %8
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* %8
%24 = load i32, i32* %10
%25 = call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
store i32 -1, i32* %5
br label %27
26: ; preds = %18
store i32 0, i32* %5
br label %27
27: ; preds = %26, %22, %16
%28 = load i32, i32* %5
ret i32 %28
}

LLVM IR (-O2):
@.str = [28 x i8] c"read_struct: Failed to read\00"
@.str.1 = [61 x i8] c"read_struct: Failed to read %llu bytes, actual read was %llu\00"
define i32 @read_struct(i32 %0, i8* nocapture %1, i32 %2, i8* nocapture readnone %3) {
%5 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %13
9: ; preds = %4
%10 = icmp eq i32 %5, %2
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %5)
br label %13
13: ; preds = %9, %11, %7
%14 = phi i32 [ -1, %7 ], [ -1, %11 ], [ 0, %9 ]
ret i32 %14
}