#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_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int detect_link(int, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int get_link_status(char *ifname) {
  int skfd = -1;
  int retval;
  if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    printf("Error: failed to open socket < %s >\n", strerror(errno));
    exit(-1);
  }
  retval = detect_link(skfd, ifname);
  close(skfd);
  return retval;
}


x86 assembly (clang, -O0):
<get_link_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11bf <get_link_status+0x5f>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1040 <detect_link@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_link_status>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11a1 <get_link_status+0x41>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <detect_link@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4a(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [37 x i8] c"Error: failed to open socket < %s >\0A\00"
@errno = global i32 0
define i32 @get_link_status(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i8*, i8** %2
%17 = call i32 @detect_link(i32 %15, i8* %16)
store i32 %17, i32* %4
%18 = load i32, i32* %3
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %4
ret i32 %20
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [37 x i8] c"Error: failed to open socket < %s >\0A\00"
@errno = global i32 0
define i32 @get_link_status(i8* %0) {
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_DGRAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %1
%12 = tail call i32 @detect_link(i32 %4, i8* %0)
%13 = tail call i32 @close(i32 %4)
ret i32 %12
}