#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 */
struct xray_monitor {
  int dummy;
};
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(void *);
int fstat(int, struct stat *);
void *malloc(int);
int monitor_deserialize(struct xray_monitor **, void *);
int open(char const *, int /*<<< orphan*/, int);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
int monitor_deserialize_from_file(const char *filename,
                                  struct xray_monitor **pxrm) {
  int fd = open(filename, O_RDONLY, 0644);
  if (fd == -1) {
    fprintf(stderr, "could not open serialized monitor, filename %s\n",
            filename);
    return -1;
  }
  struct stat buf;
  int rc = fstat(fd, &buf);
  if (rc) {
    fprintf(stderr, "could not fstat serialized  monitor, filename: %s\n",
            filename);
    return -1;
  }
  int file_size = buf.st_size;
  void *ptr = malloc(file_size);
  rc = read(fd, ptr, file_size);
  if (rc != file_size) {
    fprintf(
        stderr,
        "could not read serialized monitor, expected %d bytes, got %d bytes\n",
        file_size, rc);
    return -1;
  }
  rc = monitor_deserialize(pxrm, ptr);
  if (rc != file_size) {
    fprintf(stderr,
            "[WARN] monitor size %d does not match seralized file size %d\n",
            rc, file_size);
  }
  free(ptr);
  return 0;
}


x86 assembly (clang, -O0):
<monitor_deserialize_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cb <monitor_deserialize_from_file+0x5b>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <monitor_deserialize_from_file+0x13d>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     120b <monitor_deserialize_from_file+0x9b>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <monitor_deserialize_from_file+0x13d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1264 <monitor_deserialize_from_file+0xf4>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <monitor_deserialize_from_file+0x13d>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <monitor_deserialize@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     129d <monitor_deserialize_from_file+0x12d>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor_deserialize_from_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11b7 <monitor_deserialize_from_file+0x47>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %eax,%eax
je     11e1 <monitor_deserialize_from_file+0x71>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
jmp    11c7 <monitor_deserialize_from_file+0x57>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
jne    1234 <monitor_deserialize_from_file+0xc4>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <monitor_deserialize@plt>
cmp    %ebx,%eax
je     1228 <monitor_deserialize_from_file+0xb8>
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edi
lea    0xe8a(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11d6 <monitor_deserialize_from_file+0x66>
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%edi
lea    0xe1f(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11d1 <monitor_deserialize_from_file+0x61>

LLVM IR (-O0):
%struct.xray_monitor = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"could not open serialized monitor, filename %s\0A\00"
@.str.1 = [51 x i8] c"could not fstat serialized monitor, filename: %s\0A\00"
@.str.2 = [68 x i8] c"could not read serialized monitor, expected %d bytes, got %d bytes\0A\00"
@.str.3 = [62 x i8] c"[WARN] monitor size %d does not match seralized file size %d\0A\00"
define i32 @monitor_deserialize_from_file(i8* %0, %struct.xray_monitor** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.xray_monitor**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store %struct.xray_monitor** %1, %struct.xray_monitor*** %5
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12, i32 420)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %4
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %61
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i32 @fstat(i32 %21, %struct.stat* %7)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %4
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %61
29: ; preds = %20
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%31 = load i32, i32* %30
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %10
%34 = load i32, i32* %6
%35 = load i8*, i8** %10
%36 = load i32, i32* %9
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %29
%42 = load i32, i32* @stderr
%43 = load i32, i32* %9
%44 = load i32, i32* %8
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %44)
store i32 -1, i32* %3
br label %61
46: ; preds = %29
%47 = load %struct.xray_monitor**, %struct.xray_monitor*** %5
%48 = load i8*, i8** %10
%49 = call i32 @monitor_deserialize(%struct.xray_monitor** %47, i8* %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %58
53: ; preds = %46
%54 = load i32, i32* @stderr
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i32 %55, i32 %56)
br label %58
58: ; preds = %53, %46
%59 = load i8*, i8** %10
%60 = call i32 @free(i8* %59)
store i32 0, i32* %3
br label %61
61: ; preds = %58, %41, %25, %16
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.xray_monitor = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"could not open serialized monitor, filename %s\0A\00"
@.str.1 = [51 x i8] c"could not fstat serialized monitor, filename: %s\0A\00"
@.str.2 = [68 x i8] c"could not read serialized monitor, expected %d bytes, got %d bytes\0A\00"
@.str.3 = [62 x i8] c"[WARN] monitor size %d does not match seralized file size %d\0A\00"
define i32 @monitor_deserialize_from_file(i8* %0, %struct.xray_monitor** %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4, i32 420)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %36
10: ; preds = %2
%11 = bitcast %struct.stat* %3 to i8*
%12 = call i32 @fstat(i32 %5, %struct.stat* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
17: ; preds = %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i8* @malloc(i32 %19)
%21 = tail call i32 @read(i32 %5, i8* %20, i32 %19)
%22 = icmp eq i32 %21, %19
br i1 %22, label %26, label %23
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %21)
br label %34
26: ; preds = %17
%27 = tail call i32 @monitor_deserialize(%struct.xray_monitor** %1, i8* %20)
%28 = icmp eq i32 %27, %19
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %19)
br label %32
32: ; preds = %29, %26
%33 = tail call i32 @free(i8* %20)
br label %34
34: ; preds = %23, %32, %14
%35 = phi i32 [ -1, %14 ], [ -1, %23 ], [ 0, %32 ]
br label %36
36: ; preds = %34, %7
%37 = phi i32 [ -1, %7 ], [ %35, %34 ]
ret i32 %37
}