#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;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int monitor_serialize(struct xray_monitor *, void **);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
int write(int, void *, int);
int monitor_serialize_to_file(const char *filename, struct xray_monitor *xrm) {
  int size_monitor;
  void *ptr;
  size_monitor = monitor_serialize(xrm, &ptr);
  if (size_monitor < 0) {
    fprintf(stderr, "could not serialize error monitor\n");
    return -1;
  }
  int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
  if (fd == -1) {
    fprintf(stderr, "Error creating error monitor file %s\n", filename);
    return -1;
  }
  int rc = write(fd, ptr, size_monitor);
  if (rc != size_monitor) {
    fprintf(stderr, "Wrote %d bytes, expected %d bytes\n", rc, size_monitor);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<monitor_serialize_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <monitor_serialize@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119d <monitor_serialize_to_file+0x4d>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <monitor_serialize_to_file+0x100>
mov    -0x10(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11fa <monitor_serialize_to_file+0xaa>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <monitor_serialize_to_file+0x100>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1241 <monitor_serialize_to_file+0xf1>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <monitor_serialize_to_file+0x100>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor_serialize_to_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1060 <monitor_serialize@plt>
test   %eax,%eax
js     11bd <monitor_serialize_to_file+0x6d>
mov    %eax,%ebx
mov    0x2e60(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d6 <monitor_serialize_to_file+0x86>
mov    %eax,%ebp
mov    0x8(%rsp),%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    11f2 <monitor_serialize_to_file+0xa2>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1212 <monitor_serialize_to_file+0xc2>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    120d <monitor_serialize_to_file+0xbd>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    120d <monitor_serialize_to_file+0xbd>
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%edi
lea    0xe47(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.xray_monitor = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"could not serialize error monitor\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [38 x i8] c"Error creating error monitor file %s\0A\00"
@.str.2 = [35 x i8] c"Wrote %d bytes, expected %d bytes\0A\00"
define i32 @monitor_serialize_to_file(i8* %0, %struct.xray_monitor* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.xray_monitor*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store %struct.xray_monitor* %1, %struct.xray_monitor** %5
%10 = load %struct.xray_monitor*, %struct.xray_monitor** %5
%11 = call i32 @monitor_serialize(%struct.xray_monitor* %10, i8** %7)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %18, i32 %23, i32 420)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %3
br label %47
31: ; preds = %17
%32 = load i32, i32* %8
%33 = load i8*, i8** %7
%34 = load i32, i32* %6
%35 = call i32 @write(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = load i32, i32* %6
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = load i32, i32* @stderr
%41 = load i32, i32* %9
%42 = load i32, i32* %6
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %41, i32 %42)
store i32 -1, i32* %3
br label %47
44: ; preds = %31
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %44, %39, %27, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.xray_monitor = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"could not serialize error monitor\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [38 x i8] c"Error creating error monitor file %s\0A\00"
@.str.2 = [35 x i8] c"Wrote %d bytes, expected %d bytes\0A\00"
define i32 @monitor_serialize_to_file(i8* %0, %struct.xray_monitor* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @monitor_serialize(%struct.xray_monitor* %1, i8** %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %30
10: ; preds = %2
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %0, i32 %15, i32 420)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
21: ; preds = %10
%22 = load i8*, i8** %3
%23 = call i32 @write(i32 %16, i8* %22, i32 %5)
%24 = icmp eq i32 %23, %5
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %5)
br label %30
28: ; preds = %21
%29 = call i32 @close(i32 %16)
br label %30
30: ; preds = %18, %28, %25, %7
%31 = phi i32 [ -1, %7 ], [ -1, %18 ], [ -1, %25 ], [ 0, %28 ]
ret i32 %31
}