#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 {
  int /*<<< orphan*/ sa_data;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ saddr;
typedef int /*<<< orphan*/ err;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int bind(int, struct sockaddr *, int);
int errno;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int /*<<< orphan*/ memset(struct sockaddr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pcap_snprintf(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ pcap_strerror(int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int iface_bind_old(int fd, const char *device, char *ebuf) {
  struct sockaddr saddr;
  int err;
  socklen_t errlen = sizeof(err);
  memset(&saddr, 0, sizeof(saddr));
  strncpy(saddr.sa_data, device, sizeof(saddr.sa_data));
  if (bind(fd, &saddr, sizeof(saddr)) == -1) {
    pcap_snprintf(ebuf, 256, "bind: %s", pcap_strerror(errno));
    return -1;
  }
  if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
    pcap_snprintf(ebuf, 256, "getsockopt: %s", pcap_strerror(errno));
    return -1;
  }
  if (err > 0) {
    pcap_snprintf(ebuf, 256, "bind: %s", pcap_strerror(err));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<iface_bind_old>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ee <iface_bind_old+0x8e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1060 <pcap_strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe23(%rip),%rdx
call   1070 <pcap_snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <iface_bind_old+0x136>
mov    -0x8(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1050 <getsockopt@plt>
cmp    $0xffffffff,%eax
jne    1252 <iface_bind_old+0xf2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1060 <pcap_strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdc8(%rip),%rdx
call   1070 <pcap_snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <iface_bind_old+0x136>
cmpl   $0x0,-0x24(%rbp)
jle    128f <iface_bind_old+0x12f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <pcap_strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xd82(%rip),%rdx
call   1070 <pcap_snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <iface_bind_old+0x136>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iface_bind_old>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x4,0x10(%rsp)
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     11e7 <iface_bind_old+0x87>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebp,%edi
call   1050 <getsockopt@plt>
cmp    $0xffffffff,%eax
je     11f9 <iface_bind_old+0x99>
mov    0x14(%rsp),%edi
test   %edi,%edi
jg     11f0 <iface_bind_old+0x90>
xor    %eax,%eax
jmp    1225 <iface_bind_old+0xc5>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rbp
jmp    1209 <iface_bind_old+0xa9>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rbp
call   1060 <pcap_strerror@plt>
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1070 <pcap_snprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"bind: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str.1 = [15 x i8] c"getsockopt: %s\00"
define i32 @iface_bind_old(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.sockaddr
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 4, i32* %10
%11 = call i32 @memset(%struct.sockaddr* %8, i32 0, i32 4)
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i8*, i8** %6
%15 = call i32 @strncpy(i32 %13, i8* %14, i32 4)
%16 = load i32, i32* %5
%17 = call i32 @bind(i32 %16, %struct.sockaddr* %8, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %3
%20 = load i8*, i8** %7
%21 = load i32, i32* @errno
%22 = call i32 @pcap_strerror(i32 %21)
%23 = call i32 @pcap_snprintf(i8* %20, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 -1, i32* %4
br label %44
24: ; preds = %3
%25 = load i32, i32* %5
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_ERROR
%28 = call i32 @getsockopt(i32 %25, i32 %26, i32 %27, i32* %9, i32* %10)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8*, i8** %7
%32 = load i32, i32* @errno
%33 = call i32 @pcap_strerror(i32 %32)
%34 = call i32 @pcap_snprintf(i8* %31, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %33)
store i32 -1, i32* %4
br label %44
35: ; preds = %24
%36 = load i32, i32* %9
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = load i32, i32* %9
%41 = call i32 @pcap_strerror(i32 %40)
%42 = call i32 @pcap_snprintf(i8* %39, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %41)
store i32 -1, i32* %4
br label %44
43: ; preds = %35
store i32 0, i32* %4
br label %44
44: ; preds = %43, %38, %30, %19
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"bind: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str.1 = [15 x i8] c"getsockopt: %s\00"
define i32 @iface_bind_old(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.sockaddr* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%10 = call i32 @memset(%struct.sockaddr* %4, i32 0, i32 4)
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strncpy(i32 %12, i8* %1, i32 4)
%14 = call i32 @bind(i32 %0, %struct.sockaddr* %4, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* @errno
br label %28
18: ; preds = %3
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_ERROR
%21 = call i32 @getsockopt(i32 %0, i32 %19, i32 %20, i32* %5, i32* %6)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i32, i32* @errno
br label %28
25: ; preds = %18
%26 = load i32, i32* %5
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25, %16, %23
%29 = phi i32 [ %24, %23 ], [ %17, %16 ], [ %26, %25 ]
%30 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %23 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %25 ]
%31 = call i32 @pcap_strerror(i32 %29)
%32 = call i32 @pcap_snprintf(i8* %2, i32 256, i8* %30, i32 %31)
br label %33
33: ; preds = %28, %25
%34 = phi i32 [ 0, %25 ], [ -1, %28 ]
ret i32 %34
}