#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*/ FS_IOC_SETFLAGS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, unsigned int *);
int open(char const *, int /*<<< orphan*/);
int set_immutable(const char *path, int immutable) {
  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;
  }
  if (immutable) {
    flags |= FS_IMMUTABLE_FL;
  } else {
    flags &= ~FS_IMMUTABLE_FL;
  }
  rc = ioctl(fd, FS_IOC_SETFLAGS, &flags);
  error = errno;
  close(fd);
  errno = error;
  return rc;
}


x86 assembly (clang, -O0):
<set_immutable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1169 <set_immutable+0x39>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <set_immutable+0xf4>
mov    -0x1c(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11b6 <set_immutable+0x86>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e27(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <set_immutable+0xf4>
cmpl   $0x0,-0x14(%rbp)
je     11d4 <set_immutable+0xa4>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11e6 <set_immutable+0xb6>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2db4(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_immutable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b2 <set_immutable+0x82>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     119a <set_immutable+0x6a>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
test   %r14d,%r14d
je     117b <set_immutable+0x4b>
or     0x4(%rsp),%eax
jmp    1181 <set_immutable+0x51>
not    %eax
and    0x4(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %eax,%ebp
mov    0x2e2d(%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
@FS_IOC_SETFLAGS = global i32 0
define i32 @set_immutable(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* %7
store i32 %16, i32* %3
br label %50
17: ; preds = %2
%18 = load i32, i32* %7
%19 = load i32, i32* @FS_IOC_GETFLAGS
%20 = call i32 @ioctl(i32 %18, i32 %19, i32* %6)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i32, i32* @errno
store i32 %24, i32* %9
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %9
store i32 %27, i32* @errno
%28 = load i32, i32* %8
store i32 %28, i32* %3
br label %50
29: ; preds = %17
%30 = load i32, i32* %5
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @FS_IMMUTABLE_FL
%34 = load i32, i32* %6
%35 = or i32 %34, %33
store i32 %35, i32* %6
br label %41
36: ; preds = %29
%37 = load i32, i32* @FS_IMMUTABLE_FL
%38 = xor i32 %37, -1
%39 = load i32, i32* %6
%40 = and i32 %39, %38
store i32 %40, i32* %6
br label %41
41: ; preds = %36, %32
%42 = load i32, i32* %7
%43 = load i32, i32* @FS_IOC_SETFLAGS
%44 = call i32 @ioctl(i32 %42, i32 %43, i32* %6)
store i32 %44, i32* %8
%45 = load i32, i32* @errno
store i32 %45, i32* %9
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %9
store i32 %48, i32* @errno
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %50
50: ; preds = %41, %23, %15
%51 = load i32, i32* %3
ret i32 %51
}

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