#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 __make_fd_async(int);
int /*<<< orphan*/ close(int);
int errno;
int socket(int, int, int);
int aqsocket(int domain, int type, int protocol) {
  int sockfd = socket(domain, type, protocol);
  if ((sockfd) == -1) {
    return -1;
  };
  if ((__make_fd_async(sockfd)) == -1) {
    int tmp_errno = errno;
    do {
      { close(sockfd); }
    } while (0);
    errno = tmp_errno;
    return -1;
  };
  return sockfd;
}


x86 assembly (clang, -O0):
<aqsocket>:
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   1050 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1168 <aqsocket+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <aqsocket+0x7b>
mov    -0x14(%rbp),%edi
call   1040 <__make_fd_async@plt>
cmp    $0xffffffff,%eax
jne    11a5 <aqsocket+0x75>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2e49(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <aqsocket+0x7b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aqsocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1050 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     116c <aqsocket+0x3c>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <__make_fd_async@plt>
cmp    $0xffffffff,%eax
je     1158 <aqsocket+0x28>
mov    %ebp,%ebx
jmp    116c <aqsocket+0x3c>
mov    0x2e81(%rip),%r14
mov    (%r14),%r15d
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r15d,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @aqsocket(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 eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %30
17: ; preds = %3
%18 = load i32, i32* %8
%19 = call i32 @__make_fd_async(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i32, i32* @errno
store i32 %22, i32* %9
br label %23
23: ; preds = %21
%24 = load i32, i32* %8
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23
%27 = load i32, i32* %9
store i32 %27, i32* @errno
store i32 -1, i32* %4
br label %30
28: ; preds = %17
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %30
30: ; preds = %28, %26, %16
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @aqsocket(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @socket(i32 %0, i32 %1, i32 %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = tail call i32 @__make_fd_async(i32 %4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = tail call i32 @close(i32 %4)
store i32 %10, i32* @errno
br label %12
12: ; preds = %6, %3, %9
%13 = phi i32 [ -1, %9 ], [ -1, %3 ], [ %4, %6 ]
ret i32 %13
}