#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 {
  int st_mode;
};
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int lstat(char *, struct stat *);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int check_rights(char *file, mode_t mode) {
  struct stat stats;
  int ret;
  ret = lstat(file, &stats);
  if (ret < 0) {
    printf("lstat(%s) %s\n", file, strerror(errno));
    return -1;
  }
  if ((stats.st_mode & 0xFFF) != mode) {
    printf("%s has mode %x while expecting %x\n", file, stats.st_mode, mode);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_rights>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1190 <check_rights+0x60>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <check_rights+0x9c>
mov    -0x18(%rbp),%eax
and    $0xfff,%eax
cmp    -0x14(%rbp),%eax
je     11c5 <check_rights+0x95>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <check_rights+0x9c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_rights>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %eax,%eax
js     116a <check_rights+0x3a>
mov    (%rsp),%edx
mov    %edx,%ecx
and    $0xfff,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
je     1191 <check_rights+0x61>
lea    0xeb2(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    118c <check_rights+0x5c>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe81(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"lstat(%s) %s\0A\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"%s has mode %x while expecting %x\0A\00"
define i32 @check_rights(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @lstat(i8* %8, %struct.stat* %6)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %15)
store i32 -1, i32* %3
br label %30
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, 4095
%21 = load i32, i32* %5
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i8*, i8** %4
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* %5
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %26, i32 %27)
store i32 -1, i32* %3
br label %30
29: ; preds = %17
store i32 0, i32* %3
br label %30
30: ; preds = %29, %23, %12
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"lstat(%s) %s\0A\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"%s has mode %x while expecting %x\0A\00"
define i32 @check_rights(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %18
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, 4095
%15 = icmp eq i32 %14, %1
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %13, i32 %1)
br label %18
18: ; preds = %11, %16, %7
%19 = phi i32 [ -1, %7 ], [ -1, %16 ], [ 0, %11 ]
ret i32 %19
}