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


x86 assembly (clang, -O0):
<get_ctl_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1159 <get_ctl_fd+0x39>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <get_ctl_fd+0xc6>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1196 <get_ctl_fd+0x76>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <get_ctl_fd+0xc6>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11c7 <get_ctl_fd+0xa7>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <get_ctl_fd+0xc6>
mov    -0x8(%rbp),%ecx
mov    0x2df7(%rip),%rax
mov    %ecx,(%rax)
lea    0xe26(%rip),%rdi
call   1030 <perror@plt>
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    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2ea4(%rip),%rbx
mov    (%rbx),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
jns    1177 <get_ctl_fd+0x57>
mov    0x2e80(%rip),%r14
mov    (%r14),%ebp
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
jns    1177 <get_ctl_fd+0x57>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
js     117c <get_ctl_fd+0x5c>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,(%r14)
lea    0xe7a(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%eax
jmp    1177 <get_ctl_fd+0x57>

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
@.str = [29 x i8] c"Cannot create control socket\00"
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* %3
%7 = load i32, i32* %3
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = load i32, i32* %3
store i32 %10, i32* %1
br label %31
11: ; preds = %0
%12 = load i32, i32* @errno
store i32 %12, i32* %2
%13 = load i32, i32* @PF_PACKET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i32, i32* %3
store i32 %19, i32* %1
br label %31
20: ; preds = %11
%21 = load i32, i32* @PF_INET6
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i32, i32* %3
store i32 %27, i32* %1
br label %31
28: ; preds = %20
%29 = load i32, i32* %2
store i32 %29, i32* @errno
%30 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %31
31: ; preds = %28, %26, %18, %9
%32 = load i32, i32* %1
ret i32 %32
}

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
@.str = [29 x i8] c"Cannot create control socket\00"
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 %18, 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 %18, 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 %18, label %16
16: ; preds = %11
store i32 %6, i32* @errno
%17 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %11, %5, %0, %16
%19 = phi i32 [ -1, %16 ], [ %3, %0 ], [ %9, %5 ], [ %14, %11 ]
ret i32 %19
}