#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 */
typedef size_t socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BINDTODEVICE;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, size_t *);
int /*<<< orphan*/ perror(char *);
int get_bind_to_device(int sd, char *name, size_t len) {
  socklen_t optlen = len;
  int rc;
  name[0] = '\0';
  rc = getsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, name, &optlen);
  if (rc < 0)
    perror("setsockopt(SO_BINDTODEVICE)");
  return rc;
}


x86 assembly (clang, -O0):
<get_bind_to_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    117d <get_bind_to_device+0x5d>
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bind_to_device>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rcx
mov    %rdx,0x8(%rsp)
movb   $0x0,(%rsi)
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%r8
call   1030 <getsockopt@plt>
mov    %eax,%ebx
test   %eax,%eax
js     115a <get_bind_to_device+0x3a>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe9f(%rip),%rdi
call   1040 <perror@plt>
jmp    1152 <get_bind_to_device+0x32>

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str = [28 x i8] c"setsockopt(SO_BINDTODEVICE)\00"
define i32 @get_bind_to_device(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
store i64 %9, i64* %7
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8 0, i8* %11
%12 = load i32, i32* %4
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_BINDTODEVICE
%15 = load i8*, i8** %5
%16 = call i32 @getsockopt(i32 %12, i32 %13, i32 %14, i8* %15, i64* %7)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %3
%22 = load i32, i32* %8
ret i32 %22
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str = [28 x i8] c"setsockopt(SO_BINDTODEVICE)\00"
define i32 @get_bind_to_device(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 %2, i64* %4
store i8 0, i8* %1
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_BINDTODEVICE
%8 = call i32 @getsockopt(i32 %0, i32 %6, i32 %7, i8* %1, i64* %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
ret i32 %8
}