#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 sockaddr_ll {
  int sll_ifindex;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ARP;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bind_arp(int ifindex, int *fd) {
  printf("bind_arp: ifindex=%i", ifindex);
  printf("\n");
  ;
  int ret = -1;
  *fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
  if (*fd < 1) {
    perror("socket()");
    goto out;
  }
  printf("Binding to ifindex %i", ifindex);
  printf("\n");
  ;
  struct sockaddr_ll sll;
  memset(&sll, 0, sizeof(struct sockaddr_ll));
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = ifindex;
  if (bind(*fd, (struct sockaddr *)&sll, sizeof(struct sockaddr_ll)) < 0) {
    perror("bind");
    goto out;
  }
  ret = 0;
out:
  if (ret && *fd > 0) {
    printf("Cleanup socket");
    printf("\n");
    ;
    close(*fd);
  }
  return ret;
}


x86 assembly (clang, -O0):
<bind_arp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x1,(%rax)
jge    11fe <bind_arp+0x8e>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
jmp    1275 <bind_arp+0x105>
mov    -0x4(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    126e <bind_arp+0xfe>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
jmp    1275 <bind_arp+0x105>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12b3 <bind_arp+0x143>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jle    12b3 <bind_arp+0x143>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_arp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r12d
lea    0xe68(%rip),%rdi
xor    %r14d,%r14d
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2e22(%rip),%r13
mov    0x0(%r13),%ebx
mov    0x2e27(%rip),%rax
mov    (%rax),%ebp
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    %eax,(%r15)
test   %eax,%eax
jle    1243 <bind_arp+0xc3>
lea    0xe34(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    %r12d,(%rsp)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     124c <bind_arp+0xcc>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcb(%rip),%rdi
jmp    1253 <bind_arp+0xd3>
lea    0xde1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
cmpl   $0x0,(%r15)
jle    1231 <bind_arp+0xb1>
lea    0xdce(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%r15),%edi
call   1070 <close@plt>
jmp    1231 <bind_arp+0xb1>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"bind_arp: ifindex=%i\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ARP = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@.str.3 = [22 x i8] c"Binding to ifindex %i\00"
@.str.4 = [5 x i8] c"bind\00"
@.str.5 = [15 x i8] c"Cleanup socket\00"
define i32 @bind_arp(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.sockaddr_ll
store i32 %0, i32* %3
store i32* %1, i32** %4
%7 = load i32, i32* %3
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
%10 = load i32, i32* @AF_PACKET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @ETH_P_ARP
%13 = call i32 @htons(i32 %12)
%14 = call i32 @socket(i32 %10, i32 %11, i32 %13)
%15 = load i32*, i32** %4
store i32 %14, i32* %15
%16 = load i32*, i32** %4
%17 = load i32, i32* %16
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %38
21: ; preds = %2
%22 = load i32, i32* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @memset(%struct.sockaddr_ll* %6, i32 0, i32 8)
%26 = load i32, i32* @AF_PACKET
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* %3
%29 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32*, i32** %4
%31 = load i32, i32* %30
%32 = bitcast %struct.sockaddr_ll* %6 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %38
37: ; preds = %21
store i32 0, i32* %5
br label %38
38: ; preds = %37, %35, %19
%39 = load i32, i32* %5
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = load i32*, i32** %4
%43 = load i32, i32* %42
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32*, i32** %4
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %45, %41, %38
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"bind_arp: ifindex=%i\00"
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ARP = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@.str.3 = [22 x i8] c"Binding to ifindex %i\00"
@.str.4 = [5 x i8] c"bind\00"
@.str.5 = [15 x i8] c"Cleanup socket\00"
define i32 @bind_arp(i32 %0, i32* nocapture %1) {
%3 = alloca %struct.sockaddr_ll
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = tail call i32 @putchar(i32 10)
%6 = load i32, i32* @AF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_ARP
%9 = tail call i32 @htons(i32 %8)
%10 = tail call i32 @socket(i32 %6, i32 %7, i32 %9)
store i32 %10, i32* %1
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %27
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%16 = tail call i32 @putchar(i32 10)
%17 = call i32 @memset(%struct.sockaddr_ll* %3, i32 0, i32 8)
%18 = load i32, i32* @AF_PACKET
%19 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 0
store i32 %0, i32* %20
%21 = load i32, i32* %1
%22 = bitcast %struct.sockaddr_ll* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
%28 = load i32, i32* %1
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%32 = call i32 @putchar(i32 10)
%33 = load i32, i32* %1
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %14, %30, %27
%36 = phi i32 [ -1, %30 ], [ -1, %27 ], [ 0, %14 ]
ret i32 %36
}