#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_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ my_ip_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BINDTODEVICE;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char const *,
                      scalar_t__);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int createDeviceBoundUDPSocket(uint32_t sip, uint16_t sport,
                               const char *bind_dev) {
  printf("bind_dev = %s", bind_dev);
  int s = socket(AF_INET, SOCK_DGRAM, 0);
  int result;
  struct sockaddr_in my_ip_addr;
  if (s < 0) {
    perror("socket");
    return s;
  }
  memset(&my_ip_addr, 0, sizeof(my_ip_addr));
  my_ip_addr.sin_family = AF_INET;
  my_ip_addr.sin_addr.s_addr = htonl(sip);
  my_ip_addr.sin_port = htons(sport);
  if (bind_dev) {
    if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, bind_dev,
                   strlen(bind_dev) + 1)) {
      perror("Error binding to device");
      return -1;
    }
  }
  return s;
}


x86 assembly (clang, -O0):
<createDeviceBoundUDPSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe63(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <createDeviceBoundUDPSocket+0x5f>
lea    0xe3f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <createDeviceBoundUDPSocket+0x111>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128b <createDeviceBoundUDPSocket+0x10b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    %rax,%r8
add    $0x1,%r8
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1286 <createDeviceBoundUDPSocket+0x106>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <createDeviceBoundUDPSocket+0x111>
jmp    128b <createDeviceBoundUDPSocket+0x10b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDeviceBoundUDPSocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xe64(%rip),%rdi
mov    %rdx,%rsi
call   1060 <printf@plt>
mov    0x2e35(%rip),%r12
mov    (%r12),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     123c <createDeviceBoundUDPSocket+0xbc>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
test   %rbx,%rbx
je     122d <createDeviceBoundUDPSocket+0xad>
mov    0x2de9(%rip),%rax
mov    (%rax),%r14d
mov    0x2dbf(%rip),%rax
mov    (%rax),%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%r8
mov    %ebp,%edi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    124a <createDeviceBoundUDPSocket+0xca>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcb(%rip),%rdi
call   1090 <perror@plt>
jmp    122d <createDeviceBoundUDPSocket+0xad>
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    122d <createDeviceBoundUDPSocket+0xad>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [14 x i8] c"bind_dev = %s\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str.2 = [24 x i8] c"Error binding to device\00"
define i32 @createDeviceBoundUDPSocket(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %7
%12 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %8
store i32 %20, i32* %4
br label %49
21: ; preds = %3
%22 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* %5
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %6
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %47
34: ; preds = %21
%35 = load i32, i32* %8
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_BINDTODEVICE
%38 = load i8*, i8** %7
%39 = load i8*, i8** %7
%40 = call i64 @strlen(i8* %39)
%41 = add nsw i64 %40, 1
%42 = call i64 @setsockopt(i32 %35, i32 %36, i32 %37, i8* %38, i64 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %34
%45 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
46: ; preds = %34
br label %47
47: ; preds = %46, %21
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %44, %18
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [14 x i8] c"bind_dev = %s\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str.2 = [24 x i8] c"Error binding to device\00"
define i32 @createDeviceBoundUDPSocket(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %2)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = icmp slt i32 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %31
13: ; preds = %3
%14 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htonl(i32 %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %19, i32* %20
%21 = icmp eq i8* %2, null
br i1 %21, label %31, label %22
22: ; preds = %13
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_BINDTODEVICE
%25 = call i64 @strlen(i8* %2)
%26 = add nsw i64 %25, 1
%27 = call i64 @setsockopt(i32 %8, i32 %23, i32 %24, i8* %2, i64 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %13, %22, %29, %11
%32 = phi i32 [ %8, %11 ], [ -1, %29 ], [ %8, %22 ], [ %8, %13 ]
ret i32 %32
}