#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_DIRECT;
int O_SYNC;
int /*<<< orphan*/ _IOC(int /*<<< orphan*/, float, int /*<<< orphan*/, int);
int /*<<< orphan*/ _IOC_WRITE;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, char const *);
int open(char *, int);
int fpui_open(int flags, const char *regname) {
  int fd = 0;
  int errcd = 0;
  if (flags & O_DIRECT) {
    flags = (flags & ~O_DIRECT) | O_SYNC;
  }
  if ((fd = open("/dev/fpi", flags)) < 0) {
    return (fd);
  }
  if ((errcd = ioctl(fd, _IOC(_IOC_WRITE, 'f', 0, sizeof(char *)), regname)) <
      0) {
    close(fd);
    return (errcd);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<fpui_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e79(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     118f <fpui_open+0x4f>
mov    -0x8(%rbp),%eax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2e5e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11b5 <fpui_open+0x75>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <fpui_open+0xcb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
movss  0xe34(%rip),%xmm0
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <_IOC@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1205 <fpui_open+0xc5>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <fpui_open+0xcb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fpui_open>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e95(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%eax
not    %eax
and    %edi,%eax
mov    0x2e8e(%rip),%rdx
or     (%rdx),%eax
mov    %rsi,%r14
test   %edi,%ecx
cmove  %edi,%eax
lea    0xe99(%rip),%rdi
mov    %eax,%esi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b0 <fpui_open+0x70>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
movss  0xe77(%rip),%xmm0
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <_IOC@plt>
mov    %ebx,%edi
mov    %eax,%esi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
jns    11b0 <fpui_open+0x70>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_DIRECT = global i32 0
@O_SYNC = global i32 0
@.str = [9 x i8] c"/dev/fpi\00"
@_IOC_WRITE = global i32 0
define i32 @fpui_open(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = load i32, i32* @O_DIRECT
%10 = and i32 %8, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* @O_DIRECT
%15 = xor i32 %14, -1
%16 = and i32 %13, %15
%17 = load i32, i32* @O_SYNC
%18 = or i32 %16, %17
store i32 %18, i32* %4
br label %19
19: ; preds = %12, %2
%20 = load i32, i32* %4
%21 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %38
25: ; preds = %19
%26 = load i32, i32* %6
%27 = load i32, i32* @_IOC_WRITE
%28 = call i32 @_IOC(i32 %27, float 1.020000e+02, i32 0, i32 8)
%29 = load i8*, i8** %5
%30 = call i32 @ioctl(i32 %26, i32 %28, i8* %29)
store i32 %30, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %7
store i32 %35, i32* %3
br label %38
36: ; preds = %25
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %32, %23
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@O_DIRECT = global i32 0
@O_SYNC = global i32 0
@.str = [9 x i8] c"/dev/fpi\00"
@_IOC_WRITE = global i32 0
define i32 @fpui_open(i32 %0, i8* %1) {
%3 = load i32, i32* @O_DIRECT
%4 = and i32 %3, %0
%5 = icmp eq i32 %4, 0
%6 = xor i32 %3, -1
%7 = and i32 %6, %0
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = select i1 %5, i32 %0, i32 %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %2
%14 = load i32, i32* @_IOC_WRITE
%15 = tail call i32 @_IOC(i32 %14, float 1.020000e+02, i32 0, i32 8)
%16 = tail call i32 @ioctl(i32 %11, i32 %15, i8* %1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @close(i32 %11)
br label %20
20: ; preds = %13, %2, %18
%21 = phi i32 [ %16, %18 ], [ %11, %2 ], [ %11, %13 ]
ret i32 %21
}