#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*/ AF_UNIX;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int mksock(int type) {
  int flags;
  int fd = socket(AF_UNIX, type, 0);
  if (fd != -1) {
    if ((flags = fcntl(fd, F_GETFL)) == -1 ||
        fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1 ||
        (flags = fcntl(fd, F_GETFD)) == -1 ||
        fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
      close(fd);
      fd = -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<mksock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     11fd <mksock+0xcd>
mov    -0xc(%rbp),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     11e9 <mksock+0xb9>
mov    -0xc(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e58(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11e9 <mksock+0xb9>
mov    -0xc(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     11e9 <mksock+0xb9>
mov    -0xc(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2de9(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11f8 <mksock+0xc8>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    11fd <mksock+0xcd>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mksock>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%esi
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
xor    %edx,%edx
call   1050 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11ce <mksock+0x9e>
mov    %eax,%ebp
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11c7 <mksock+0x97>
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e70(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11c7 <mksock+0x97>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11c7 <mksock+0x97>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e0f(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11c7 <mksock+0x97>
mov    %ebp,%ebx
jmp    11ce <mksock+0x9e>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@AF_UNIX = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @mksock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* %2
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %40
10: ; preds = %1
%11 = load i32, i32* %4
%12 = load i32, i32* @F_GETFL
%13 = call i32 (i32, i32, ...) @fcntl(i32 %11, i32 %12)
store i32 %13, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %36, label %15
15: ; preds = %10
%16 = load i32, i32* %4
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %3
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %36, label %23
23: ; preds = %15
%24 = load i32, i32* %4
%25 = load i32, i32* @F_GETFD
%26 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25)
store i32 %26, i32* %3
%27 = icmp eq i32 %26, -1
br i1 %27, label %36, label %28
28: ; preds = %23
%29 = load i32, i32* %4
%30 = load i32, i32* @F_SETFD
%31 = load i32, i32* %3
%32 = load i32, i32* @FD_CLOEXEC
%33 = or i32 %31, %32
%34 = call i32 (i32, i32, ...) @fcntl(i32 %29, i32 %30, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %28, %23, %15, %10
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %4
br label %39
39: ; preds = %36, %28
br label %40
40: ; preds = %39, %1
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @mksock(i32 %0) {
%2 = load i32, i32* @AF_UNIX
%3 = tail call i32 @socket(i32 %2, i32 %0, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = load i32, i32* @F_GETFL
%7 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %25, label %9
9: ; preds = %5
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %7
%13 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %10, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %25, label %15
15: ; preds = %9
%16 = load i32, i32* @F_GETFD
%17 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = load i32, i32* @F_SETFD
%21 = load i32, i32* @FD_CLOEXEC
%22 = or i32 %21, %17
%23 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %20, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19, %15, %9, %5
%26 = tail call i32 @close(i32 %3)
br label %27
27: ; preds = %19, %25, %1
%28 = phi i32 [ -1, %25 ], [ %3, %19 ], [ -1, %1 ]
ret i32 %28
}