#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*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int O_CLOEXEC;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fflags(int sock, int flags) {
  int err;
  if (sock < 0)
    return sock;
  if (flags & O_CLOEXEC) {
    if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
      goto error;
  }
  return sock;
error:
  err = errno;
  close(sock);
  errno = err;
  return -1;
}


x86 assembly (clang, -O0):
<fflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1143 <fflags+0x23>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <fflags+0x98>
mov    -0xc(%rbp),%eax
mov    0x2e73(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1186 <fflags+0x66>
mov    -0x8(%rbp),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1181 <fflags+0x61>
jmp    1191 <fflags+0x71>
jmp    1186 <fflags+0x66>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <fflags+0x98>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fflags>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
test   %edi,%edi
js     116c <fflags+0x4c>
mov    0x2e8f(%rip),%rax
test   %esi,(%rax)
je     116c <fflags+0x4c>
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    116c <fflags+0x4c>
mov    0x2e76(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
define i32 @fflags(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 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* %4
store i32 %10, i32* %3
br label %31
11: ; preds = %2
%12 = load i32, i32* %5
%13 = load i32, i32* @O_CLOEXEC
%14 = and i32 %12, %13
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i32, i32* %4
%18 = load i32, i32* @F_SETFD
%19 = load i32, i32* @FD_CLOEXEC
%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 = %16
br label %26
23: ; preds = %16
br label %24
24: ; preds = %23, %11
%25 = load i32, i32* %4
store i32 %25, i32* %3
br label %31
26: ; preds = %22
%27 = load i32, i32* @errno
store i32 %27, i32* %6
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %6
store i32 %30, i32* @errno
store i32 -1, i32* %3
br label %31
31: ; preds = %26, %24, %9
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@O_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
define i32 @fflags(i32 %0, i32 %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %16, label %4
4: ; preds = %2
%5 = load i32, i32* @O_CLOEXEC
%6 = and i32 %5, %1
%7 = icmp eq i32 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = load i32, i32* @F_SETFD
%10 = load i32, i32* @FD_CLOEXEC
%11 = tail call i64 @fcntl(i32 %0, i32 %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i32, i32* @errno
%15 = tail call i32 @close(i32 %0)
store i32 %14, i32* @errno
br label %16
16: ; preds = %4, %8, %2, %13
%17 = phi i32 [ -1, %13 ], [ %0, %2 ], [ %0, %8 ], [ %0, %4 ]
ret i32 %17
}