#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*/ err_sys(char *);
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
void set_fl(int fd, int flags) {
  int val = fcntl(fd, F_GETFL, 0);
  if (val < 0) {
    err_sys("fcntl F_GETFL error");
  }
  val |= flags;
  if (fcntl(fd, F_SETFL, flags) < 0) {
    err_sys("fcntl F_SETFL error");
  }
}


x86 assembly (clang, -O0):
<set_fl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    115a <set_fl+0x3a>
lea    0xeab(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x8(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    118d <set_fl+0x6d>
lea    0xe8c(%rip),%rdi
call   1030 <err_sys@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_fl>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   $0x80000000,%eax
je     114a <set_fl+0x2a>
lea    0xebb(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1040 <fcntl@plt>
test   %rax,%rax
js     1168 <set_fl+0x48>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xea5(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <err_sys@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [20 x i8] c"fcntl F_GETFL error\00"
@F_SETFL = global i32 0
@.str.1 = [20 x i8] c"fcntl F_SETFL error\00"
define void @set_fl(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 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %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* %4
%21 = call i64 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [20 x i8] c"fcntl F_GETFL error\00"
@F_SETFL = global i32 0
@.str.1 = [20 x i8] c"fcntl F_SETFL error\00"
define void @set_fl(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i64 @fcntl(i32 %0, i32 %3, i32 0)
%5 = and i64 %4, 2147483648
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @F_SETFL
%11 = tail call i64 @fcntl(i32 %0, i32 %10, i32 %1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
ret void
}