#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 */
struct ifreq {
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int if_exists(const char *nic) {
  int sockd;
  int retval;
  if ((sockd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Failed to create socket");
    exit(1);
  }
  struct ifreq interface;
  strncpy(interface.ifr_name, nic, 10);
  if (ioctl(sockd, SIOCGIFINDEX, &interface) == 0)
    retval = 1;
  else
    retval = 0;
  close(sockd);
  return retval;
}


x86 assembly (clang, -O0):
<if_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <if_exists+0x47>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    11e3 <if_exists+0x83>
movl   $0x1,-0x10(%rbp)
jmp    11ea <if_exists+0x8a>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_exists>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11c8 <if_exists+0x68>
mov    %eax,%ebx
mov    %r14,%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@SIOCGIFINDEX = global i32 0
define i32 @if_exists(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.ifreq
store i8* %0, i8** %2
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %2
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 10)
%18 = load i32, i32* %3
%19 = load i32, i32* @SIOCGIFINDEX
%20 = call i64 @ioctl(i32 %18, i32 %19, %struct.ifreq* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
store i32 1, i32* %4
br label %24
23: ; preds = %13
store i32 0, i32* %4
br label %24
24: ; preds = %23, %22
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.ifreq = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@SIOCGIFINDEX = global i32 0
define i32 @if_exists(i8* %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = bitcast %struct.ifreq* %2 to i8*
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i32 10)
%13 = load i32, i32* @SIOCGIFINDEX
%14 = call i64 @ioctl(i32 %5, i32 %13, %struct.ifreq* %2)
%15 = icmp eq i64 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @close(i32 %5)
ret i32 %16
}