#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_mode;
};
typedef int /*<<< orphan*/ mode_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, mode_t mode) {
  struct stat new_stat;
  int ret;
  ret = fstat(fd, &new_stat);
  if (ret) {
    perror("fstat");
    return ret;
  }
  if (prev_stat->st_mode == new_stat.st_mode) {
    fprintf(stderr,
            "mode has not been applied. "
            "Previous mode was %d\n",
            prev_stat->st_mode);
    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    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1172 <check_changes+0x42>
lea    0xe9e(%rip),%rdi
call   1040 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <check_changes+0x82>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    11ab <check_changes+0x7b>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe6c(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <check_changes+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_changes>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rsp,%rsi
call   1050 <fstat@plt>
test   %eax,%eax
jne    1172 <check_changes+0x42>
mov    (%r14),%rdx
xor    %ebx,%ebx
cmp    (%rsp),%rdx
jne    1168 <check_changes+0x38>
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0xea8(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%ebx
lea    0xe85(%rip),%rdi
call   1040 <perror@plt>
jmp    1168 <check_changes+0x38>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.1 = [49 x i8] c"mode has not been applied. Previous mode was %d\0A\00"
define i32 @check_changes(i32 %0, %struct.stat* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i32 %0, i32* %5
store %struct.stat* %1, %struct.stat** %6
store i32 %2, i32* %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 %31
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 @fprintf(i32 %25, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 %28)
store i32 -1, i32* %4
br label %31
30: ; preds = %17
store i32 0, i32* %4
br label %31
31: ; preds = %30, %24, %14
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.1 = [49 x i8] c"mode has not been applied. Previous mode was %d\0A\00"
define i32 @check_changes(i32 %0, %struct.stat* nocapture %1, i32 %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 %19
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 @fprintf(i32 %17, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 %12)
br label %19
19: ; preds = %10, %16, %8
%20 = phi i32 [ %6, %8 ], [ -1, %16 ], [ 0, %10 ]
ret i32 %20
}