#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;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int set_flag(int fd, int flag) {
  int value;
  if ((value = fcntl(fd, F_GETFL, 0)) < 0)
    return -1;
  value |= flag;
  if (fcntl(fd, F_SETFL, value) < 0)
    return -1;
  return value;
}


x86 assembly (clang, -O0):
<set_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1149 <set_flag+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1182 <set_flag+0x72>
mov    -0xc(%rbp),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    117c <set_flag+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1182 <set_flag+0x72>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_flag>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2eb8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %rax,%rbx
test   %ebx,%ebx
js     1152 <set_flag+0x42>
or     %r14d,%ebx
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    1157 <set_flag+0x47>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define i32 @set_flag(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 %13, label %7
7: ; preds = %2
%8 = or i32 %5, %1
%9 = load i32, i32* @F_SETFL
%10 = tail call i64 @fcntl(i32 %0, i32 %9, i32 %8)
%11 = icmp slt i64 %10, 0
%12 = select i1 %11, i32 -1, i32 %8
br label %13
13: ; preds = %7, %2
%14 = phi i32 [ -1, %2 ], [ %12, %7 ]
ret i32 %14
}