#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*/ PF_INET;
int /*<<< orphan*/ PF_INET6;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_DGRAM;
int errno;
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_ctl_fd(void) {
  int fd;
  int s_errno;
  if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) >= 0)
    return fd;
  s_errno = errno;
  if ((fd = socket(PF_PACKET, SOCK_DGRAM, 0)) >= 0)
    return fd;
  if ((fd = socket(PF_INET6, SOCK_DGRAM, 0)) >= 0)
    return fd;
  errno = s_errno;
  return -1;
}


x86 assembly (clang, -O0):
<get_ctl_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     1148 <get_ctl_fd+0x38>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <get_ctl_fd+0xb7>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     1184 <get_ctl_fd+0x74>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <get_ctl_fd+0xb7>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     11b4 <get_ctl_fd+0xa4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <get_ctl_fd+0xb7>
mov    -0xc(%rbp),%ecx
mov    0x2e0a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ctl_fd>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
mov    0x2eb4(%rip),%rbx
mov    (%rbx),%esi
xor    %edx,%edx
call   1030 <socket@plt>
test   %eax,%eax
jns    116f <get_ctl_fd+0x5f>
mov    0x2e90(%rip),%r14
mov    (%r14),%ebp
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
xor    %edx,%edx
call   1030 <socket@plt>
test   %eax,%eax
jns    116f <get_ctl_fd+0x5f>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
xor    %edx,%edx
call   1030 <socket@plt>
test   %eax,%eax
jns    116f <get_ctl_fd+0x5f>
mov    %ebp,(%r14)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@PF_PACKET = global i32 0
@PF_INET6 = global i32 0
define i32 @get_ctl_fd() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* %2
store i32 %9, i32* %1
br label %27
10: ; preds = %0
%11 = load i32, i32* @errno
store i32 %11, i32* %3
%12 = load i32, i32* @PF_PACKET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %2
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = load i32, i32* %2
store i32 %17, i32* %1
br label %27
18: ; preds = %10
%19 = load i32, i32* @PF_INET6
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %2
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i32, i32* %2
store i32 %24, i32* %1
br label %27
25: ; preds = %18
%26 = load i32, i32* %3
store i32 %26, i32* @errno
store i32 -1, i32* %1
br label %27
27: ; preds = %25, %23, %16, %8
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@PF_PACKET = global i32 0
@PF_INET6 = global i32 0
define i32 @get_ctl_fd() {
%1 = load i32, i32* @PF_INET
%2 = load i32, i32* @SOCK_DGRAM
%3 = tail call i32 @socket(i32 %1, i32 %2, i32 0)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %17, label %5
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = load i32, i32* @PF_PACKET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %17, label %11
11: ; preds = %5
%12 = load i32, i32* @PF_INET6
%13 = load i32, i32* @SOCK_DGRAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %17, label %16
16: ; preds = %11
store i32 %6, i32* @errno
br label %17
17: ; preds = %11, %5, %0, %16
%18 = phi i32 [ -1, %16 ], [ %3, %0 ], [ %9, %5 ], [ %14, %11 ]
ret i32 %18
}