#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_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int knot_map_errno();
int socket(int, int, int);
int socket_create(int family, int type, int proto) {
  int sock = socket(family, type, proto);
  if (sock < 0) {
    return knot_map_errno();
  }
  if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
    int ret = knot_map_errno();
    close(sock);
    return ret;
  }
  return sock;
}


x86 assembly (clang, -O0):
<socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117b <socket_create+0x3b>
mov    $0x0,%al
call   1050 <knot_map_errno@plt>
mov    %eax,-0x4(%rbp)
jmp    11c2 <socket_create+0x82>
mov    -0x14(%rbp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     11bc <socket_create+0x7c>
mov    $0x0,%al
call   1050 <knot_map_errno@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c2 <socket_create+0x82>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_create>:
push   %rbp
push   %rbx
push   %rax
call   1060 <socket@plt>
test   %eax,%eax
js     1180 <socket_create+0x40>
mov    %eax,%ebx
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
je     118d <socket_create+0x4d>
xor    %eax,%eax
call   1050 <knot_map_errno@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
jmp    118f <socket_create+0x4f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <knot_map_errno@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @socket_create(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 (...) @knot_map_errno()
store i32 %17, i32* %4
br label %31
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = call i64 @fcntl(i32 %19, i32 %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = call i32 (...) @knot_map_errno()
store i32 %25, i32* %9
%26 = load i32, i32* %8
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %9
store i32 %28, i32* %4
br label %31
29: ; preds = %18
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %24, %16
%32 = load i32, i32* %4
ret i32 %32
}

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