#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 */

/* Variables and functions */
unsigned int FS_IMMUTABLE_FL;
int /*<<< orphan*/ FS_IOC_GETFLAGS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, unsigned int *);
int open(char const *, int /*<<< orphan*/);
int get_immutable(const char *path) {
  unsigned int flags;
  int fd;
  int rc;
  int error;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return fd;
  rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
  if (rc < 0) {
    error = errno;
    close(fd);
    errno = error;
    return rc;
  }
  close(fd);
  if (flags & FS_IMMUTABLE_FL)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<get_immutable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1166 <get_immutable+0x36>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <get_immutable+0xb3>
mov    -0x18(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b3 <get_immutable+0x83>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2e2a(%rip),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <get_immutable+0xb3>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    0x2e23(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11dc <get_immutable+0xac>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <get_immutable+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_immutable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1197 <get_immutable+0x67>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     117f <get_immutable+0x4f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
xor    %ebx,%ebx
test   %eax,0x4(%rsp)
setne  %bl
jmp    1197 <get_immutable+0x67>
mov    %eax,%ebp
mov    0x2e48(%rip),%r14
mov    (%r14),%r15d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15d,(%r14)
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@FS_IOC_GETFLAGS = global i32 0
@errno = global i32 0
@FS_IMMUTABLE_FL = global i32 0
define i32 @get_immutable(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %5
store i32 %14, i32* %2
br label %36
15: ; preds = %1
%16 = load i32, i32* %5
%17 = load i32, i32* @FS_IOC_GETFLAGS
%18 = call i32 @ioctl(i32 %16, i32 %17, i32* %4)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i32, i32* @errno
store i32 %22, i32* %7
%23 = load i32, i32* %5
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %7
store i32 %25, i32* @errno
%26 = load i32, i32* %6
store i32 %26, i32* %2
br label %36
27: ; preds = %15
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %4
%31 = load i32, i32* @FS_IMMUTABLE_FL
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 1, i32* %2
br label %36
35: ; preds = %27
store i32 0, i32* %2
br label %36
36: ; preds = %35, %34, %21, %13
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@FS_IOC_GETFLAGS = global i32 0
@errno = global i32 0
@FS_IMMUTABLE_FL = global i32 0
define i32 @get_immutable(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %1
%8 = load i32, i32* @FS_IOC_GETFLAGS
%9 = call i32 @ioctl(i32 %5, i32 %8, i32* %2)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = call i32 @close(i32 %5)
store i32 %12, i32* @errno
br label %21
14: ; preds = %7
%15 = call i32 @close(i32 %5)
%16 = load i32, i32* %2
%17 = load i32, i32* @FS_IMMUTABLE_FL
%18 = and i32 %17, %16
%19 = icmp ne i32 %18, 0
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %14, %1, %11
%22 = phi i32 [ %9, %11 ], [ %5, %1 ], [ %20, %14 ]
ret i32 %22
}