#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 /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
void OS_SetFlags(int fd, int flags) {
  int val;
  if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
    exit(errno);
  }
  val |= flags;
  if (fcntl(fd, F_SETFL, val) < 0) {
    exit(errno);
  }
}


x86 assembly (clang, -O0):
<OS_SetFlags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    115b <OS_SetFlags+0x3b>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    -0x8(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1190 <OS_SetFlags+0x70>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OS_SetFlags>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     115b <OS_SetFlags+0x3b>
or     %ebp,%eax
mov    0x2e9c(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     115b <OS_SetFlags+0x3b>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@errno = global i32 0
@F_SETFL = global i32 0
define void @OS_SetFlags(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* @F_GETFL
%8 = call i64 @fcntl(i32 %6, i32 %7, i32 0)
%9 = trunc i64 %8 to i32
store i32 %9, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = or i32 %16, %15
store i32 %17, i32* %5
%18 = load i32, i32* %3
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %5
%21 = call i64 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i32, i32* @errno
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@errno = global i32 0
@F_SETFL = global i32 0
define void @OS_SetFlags(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i64 @fcntl(i32 %0, i32 %3, i32 0)
%5 = trunc i64 %4 to i32
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = or i32 %5, %1
%12 = load i32, i32* @F_SETFL
%13 = tail call i64 @fcntl(i32 %0, i32 %12, i32 %11)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
ret void
}