#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 *, ...);
int /*<<< orphan*/ sleep(int);
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 & 0x1FF) == mode) {
    return 0;
  }
  printf("%s has mode %x while expecting %x\n", file, stats.st_mode, mode);
  sleep(1);
  ret = lstat(file, &stats);
  if (ret < 0) {
    printf("lstat(%s) %s\n", file, strerror(errno));
    return -1;
  }
  if ((stats.st_mode & 0x1FF) != 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    11a0 <check_rights+0x60>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <check_rights+0x12c>
mov    -0x18(%rbp),%eax
and    $0x1ff,%eax
cmp    -0x14(%rbp),%eax
jne    11bd <check_rights+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    126c <check_rights+0x12c>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1230 <check_rights+0xf0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <check_rights+0x12c>
mov    -0x18(%rbp),%eax
and    $0x1ff,%eax
cmp    -0x14(%rbp),%eax
je     1265 <check_rights+0x125>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <check_rights+0x12c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_rights>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <lstat@plt>
test   %eax,%eax
js     11c5 <check_rights+0x85>
mov    0x8(%rsp),%edx
mov    %edx,%eax
and    $0x1ff,%eax
xor    %ebp,%ebp
cmp    %r14d,%eax
je     11ec <check_rights+0xac>
lea    0xe9d(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
mov    %r14d,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <lstat@plt>
test   %eax,%eax
js     11c5 <check_rights+0x85>
mov    0x8(%rsp),%edx
mov    %edx,%eax
and    $0x1ff,%eax
cmp    %r14d,%eax
je     11ec <check_rights+0xac>
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11e7 <check_rights+0xa7>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe26(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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"
@.str.2 = [39 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 %53
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, 511
%21 = load i32, i32* %5
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 0, i32* %3
br label %53
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* %5
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %27, i32 %28)
%30 = call i32 @sleep(i32 1)
%31 = load i8*, i8** %4
%32 = call i32 @lstat(i8* %31, %struct.stat* %6)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %24
%36 = load i8*, i8** %4
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %36, i8* %38)
store i32 -1, i32* %3
br label %53
40: ; preds = %24
%41 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%42 = load i32, i32* %41
%43 = and i32 %42, 511
%44 = load i32, i32* %5
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %40
%47 = load i8*, i8** %4
%48 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%49 = load i32, i32* %48
%50 = load i32, i32* %5
%51 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %49, i32 %50)
store i32 -1, i32* %3
br label %53
52: ; preds = %40
store i32 0, i32* %3
br label %53
53: ; preds = %52, %46, %35, %23, %12
%54 = load i32, i32* %3
ret i32 %54
}

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"
@.str.2 = [39 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 %31
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, 511
%15 = icmp eq i32 %14, %1
br i1 %15, label %31, 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)
%18 = tail call i32 @sleep(i32 1)
%19 = call i32 @lstat(i8* %0, %struct.stat* %3)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %23)
br label %31
25: ; preds = %16
%26 = load i32, i32* %12
%27 = and i32 %26, 511
%28 = icmp eq i32 %27, %1
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %26, i32 %1)
br label %31
31: ; preds = %25, %11, %29, %21, %7
%32 = phi i32 [ -1, %7 ], [ -1, %21 ], [ -1, %29 ], [ 0, %11 ], [ 0, %25 ]
ret i32 %32
}