#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 stat {
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, ...);
int fstat(int, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int check_changes(int fd, struct stat *prev_stat, off_t length) {
  struct stat new_stat;
  int ret;
  ret = fstat(fd, &new_stat);
  if (ret) {
    perror("fstat");
    return ret;
  }
  if (prev_stat->st_size == new_stat.st_size) {
    fprintf(stderr, "size has not changed. Size is %ld\n", prev_stat->st_size);
    return -1;
  }
  if (length != new_stat.st_size) {
    fprintf(stderr, "size has changed bug wrongly! (%ld != %ld)\n", length,
            new_stat.st_size);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_changes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1173 <check_changes+0x43>
lea    0xe9d(%rip),%rdi
call   1040 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <check_changes+0xbe>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    11ae <check_changes+0x7e>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <check_changes+0xbe>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11e7 <check_changes+0xb7>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <check_changes+0xbe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_changes>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <fstat@plt>
test   %eax,%eax
jne    11a3 <check_changes+0x73>
mov    (%rbx),%rdx
mov    0x8(%rsp),%rcx
cmp    %rcx,%rdx
jne    1172 <check_changes+0x42>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    1193 <check_changes+0x63>
xor    %ebp,%ebp
cmp    %r14,%rcx
je     1198 <check_changes+0x68>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebp
lea    0xe54(%rip),%rdi
call   1040 <perror@plt>
jmp    1198 <check_changes+0x68>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"size has not changed. Size is %ld\0A\00"
@.str.2 = [44 x i8] c"size has changed bug wrongly! (%ld != %ld)\0A\00"
define i32 @check_changes(i32 %0, %struct.stat* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
store i32 %0, i32* %5
store %struct.stat* %1, %struct.stat** %6
store i64 %2, i64* %7
%10 = load i32, i32* %5
%11 = call i32 @fstat(i32 %10, %struct.stat* %8)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %9
store i32 %16, i32* %4
br label %42
17: ; preds = %3
%18 = load %struct.stat*, %struct.stat** %6
%19 = getelementptr inbounds %struct.stat, %struct.stat* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %20, %22
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load %struct.stat*, %struct.stat** %6
%27 = getelementptr inbounds %struct.stat, %struct.stat* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i64 %28)
store i32 -1, i32* %4
br label %42
30: ; preds = %17
%31 = load i64, i64* %7
%32 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%33 = load i64, i64* %32
%34 = icmp ne i64 %31, %33
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i64, i64* %7
%38 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%39 = load i64, i64* %38
%40 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i64 %37, i64 %39)
store i32 -1, i32* %4
br label %42
41: ; preds = %30
store i32 0, i32* %4
br label %42
42: ; preds = %41, %35, %24, %14
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"size has not changed. Size is %ld\0A\00"
@.str.2 = [44 x i8] c"size has changed bug wrongly! (%ld != %ld)\0A\00"
define i32 @check_changes(i32 %0, %struct.stat* nocapture %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @fstat(i32 %0, %struct.stat* %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %24
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i64, i64* %13
%15 = icmp eq i64 %12, %14
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i64 %12)
br label %24
19: ; preds = %10
%20 = icmp eq i64 %14, %2
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i64 %2, i64 %14)
br label %24
24: ; preds = %19, %21, %16, %8
%25 = phi i32 [ %6, %8 ], [ -1, %16 ], [ -1, %21 ], [ 0, %19 ]
ret i32 %25
}