#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 */
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ write(int, unsigned int *, int);
int reg_option(unsigned int add, unsigned int val, unsigned int mask) {
  int fd;
  unsigned int reg_val[2];
  fd = open("/dev/regopt", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd < 0) {
    printf("can't open!\n");
    return -1;
  }
  reg_val[0] = add;
  read(fd, reg_val, 8);
  reg_val[1] &= ~mask;
  reg_val[1] |= val;
  write(fd, reg_val, 8);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<reg_option>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ad <reg_option+0x5d>
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <reg_option+0xa9>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    -0x10(%rbp),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reg_option>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%rcx
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
call   1070 <open@plt>
test   %eax,%eax
js     11cb <reg_option+0x7b>
mov    %eax,%ebp
mov    %r15d,(%rsp)
mov    %rsp,%r15
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
not    %ebx
and    0x4(%rsp),%ebx
or     %r14d,%ebx
mov    %ebx,0x4(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11dc <reg_option+0x8c>
lea    0xe3a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/regopt\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [13 x i8] c"can't open!\0A\00"
define i32 @reg_option(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i32]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NDELAY
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %40
20: ; preds = %3
%21 = load i32, i32* %5
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
store i32 %21, i32* %22
%23 = load i32, i32* %8
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%25 = call i32 @read(i32 %23, i32* %24, i32 8)
%26 = load i32, i32* %7
%27 = xor i32 %26, -1
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* %6
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%33 = load i32, i32* %32
%34 = or i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* %8
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%37 = call i32 @write(i32 %35, i32* %36, i32 8)
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
store i32 0, i32* %4
br label %40
40: ; preds = %20, %18
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/regopt\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@str = [12 x i8] c"can't open!\00"
define i32 @reg_option(i32 %0, i32 %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %25
15: ; preds = %3
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
store i32 %0, i32* %16
%17 = call i32 @read(i32 %11, i32* %16, i32 8)
%18 = xor i32 %2, -1
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = and i32 %20, %18
%22 = or i32 %21, %1
store i32 %22, i32* %19
%23 = call i32 @write(i32 %11, i32* %16, i32 8)
%24 = tail call i32 @close(i32 %11)
br label %25
25: ; preds = %15, %13
%26 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %26
}