#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  unsigned int s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct TYPE_4__ {
  int /*<<< orphan*/ ifrn_name;
};
struct ifreq {
  TYPE_2__ ifr_ifrn;
};
typedef int /*<<< orphan*/ n;
typedef int /*<<< orphan*/ interface;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BINDTODEVICE;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int listen_socket(unsigned int ip, int port, char *inf) {
  struct ifreq interface;
  int fd;
  struct sockaddr_in addr;
  int n = 1;
  if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = ip;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n)) == -1) {
    close(fd);
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&n, sizeof(n)) == -1) {
    close(fd);
    return -1;
  }
  strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
  if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface,
                 sizeof(interface)) < 0) {
    close(fd);
    return -1;
  }
  printf("bind to inf: %s\n", inf);
  if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
    printf("bind error!\n");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x34(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d1 <listen_socket+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <listen_socket+0x1aa>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    123f <listen_socket+0xbf>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <listen_socket+0x1aa>
mov    -0x24(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1280 <listen_socket+0x100>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <listen_socket+0x1aa>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
mov    0x2d00(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%eax
jge    12d6 <listen_socket+0x156>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <listen_socket+0x1aa>
mov    -0x18(%rbp),%rsi
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1324 <listen_socket+0x1a4>
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <listen_socket+0x1aa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%r12d
movl   $0x1,0x4(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     12ca <listen_socket+0x13a>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r12d,0x8(%rsp)
mov    0x2dd7(%rip),%rbx
mov    (%rbx),%esi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12c3 <listen_socket+0x133>
mov    (%rbx),%esi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12c3 <listen_socket+0x133>
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    (%rbx),%esi
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
test   %eax,%eax
js     12c3 <listen_socket+0x133>
lea    0xd6e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12b7 <listen_socket+0x127>
mov    %ebp,%r15d
jmp    12ca <listen_socket+0x13a>
lea    0xd53(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AF_INET = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_BROADCAST = global i32 0
@IFNAMSIZ = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str = [17 x i8] c"bind to inf: %s\0A\00"
@.str.1 = [13 x i8] c"bind error!\0A\00"
define i32 @listen_socket(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.ifreq
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 1, i32* %11
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = load i32, i32* @IPPROTO_UDP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %76
18: ; preds = %3
%19 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %6
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* %5
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %9
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = bitcast i32* %11 to i8*
%32 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i8* %31, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %18
%35 = load i32, i32* %9
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %4
br label %76
37: ; preds = %18
%38 = load i32, i32* %9
%39 = load i32, i32* @SOL_SOCKET
%40 = load i32, i32* @SO_BROADCAST
%41 = bitcast i32* %11 to i8*
%42 = call i32 @setsockopt(i32 %38, i32 %39, i32 %40, i8* %41, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = load i32, i32* %9
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %4
br label %76
47: ; preds = %37
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%49 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i8*, i8** %7
%52 = load i32, i32* @IFNAMSIZ
%53 = call i32 @strncpy(i32 %50, i8* %51, i32 %52)
%54 = load i32, i32* %9
%55 = load i32, i32* @SOL_SOCKET
%56 = load i32, i32* @SO_BINDTODEVICE
%57 = bitcast %struct.ifreq* %8 to i8*
%58 = call i32 @setsockopt(i32 %54, i32 %55, i32 %56, i8* %57, i32 4)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %47
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %4
br label %76
63: ; preds = %47
%64 = load i8*, i8** %7
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %9
%67 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%68 = call i32 @bind(i32 %66, %struct.sockaddr* %67, i32 4)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %74
70: ; preds = %63
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%72 = load i32, i32* %9
%73 = call i32 @close(i32 %72)
store i32 -1, i32* %4
br label %76
74: ; preds = %63
%75 = load i32, i32* %9
store i32 %75, i32* %4
br label %76
76: ; preds = %74, %70, %60, %44, %34, %17
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AF_INET = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_BROADCAST = global i32 0
@IFNAMSIZ = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str = [17 x i8] c"bind to inf: %s\0A\00"
@str = [12 x i8] c"bind error!\00"
define i32 @listen_socket(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.ifreq* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %47, label %15
15: ; preds = %3
%16 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %0, i32* %21
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %13, i32 %22, i32 %23, i8* %9, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %45, label %26
26: ; preds = %15
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_BROADCAST
%29 = call i32 @setsockopt(i32 %13, i32 %27, i32 %28, i8* %9, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %45, label %31
31: ; preds = %26
%32 = load i32, i32* @IFNAMSIZ
%33 = call i32 @strncpy(i32 undef, i8* %2, i32 %32)
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_BINDTODEVICE
%36 = call i32 @setsockopt(i32 %13, i32 %34, i32 %35, i8* %7, i32 4)
%37 = icmp slt i32 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %31
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %2)
%40 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%41 = call i32 @bind(i32 %13, %struct.sockaddr* %40, i32 4)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %31, %26, %15, %43
%46 = call i32 @close(i32 %13)
br label %47
47: ; preds = %45, %38, %3
%48 = phi i32 [ -1, %3 ], [ %13, %38 ], [ -1, %45 ]
ret i32 %48
}