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


x86 assembly (clang, -O0):
<set_fl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1166 <set_fl+0x46>
lea    0xeab(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <set_fl+0x8b>
mov    -0xc(%rbp),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11a4 <set_fl+0x84>
lea    0xe7b(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <set_fl+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_fl>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1161 <set_fl+0x41>
or     %ebx,%eax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     116a <set_fl+0x4a>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe98(%rip),%rdi
jmp    1171 <set_fl+0x51>
lea    0xe9d(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1159 <set_fl+0x39>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [14 x i8] c"fcntl F_GETFL\00"
@F_SETFL = global i32 0
@.str.1 = [14 x i8] c"fcntl F_SETFL\00"
define i32 @set_fl(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @F_GETFL
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
14: ; preds = %2
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = or i32 %16, %15
store i32 %17, i32* %6
%18 = load i32, i32* %4
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %6
%21 = call i32 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
25: ; preds = %14
store i32 0, i32* %3
br label %26
26: ; preds = %25, %23, %12
%27 = load i32, i32* %3
ret i32 %27
}

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