#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct ifreq {
  int /*<<< orphan*/ ifr_flags;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ IFF_PROMISC;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int create_socket(char *iface_name) {
  int sock_fd;
  if ((sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    die("Erro na criacao do socket.\n");
  }
  struct ifreq ifr;
  strcpy(ifr.ifr_name, iface_name);
  if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) < 0) {
    die("ioctl error!");
  }
  ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
  ifr.ifr_flags |= IFF_PROMISC;
  ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
  struct timeval tv;
  tv.tv_sec = 10;
  tv.tv_usec = 0;
  setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
             sizeof(struct timeval));
  return sock_fd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11b7 <create_socket+0x57>
lea    0xe4e(%rip),%rdi
call   1070 <die@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    11ee <create_socket+0x8e>
lea    0xe33(%rip),%rdi
call   1070 <die@plt>
mov    -0xc(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
movl   $0xa,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%ebx
mov    0x2e55(%rip),%rax
mov    (%rax),%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a8 <create_socket+0x48>
lea    0xe5d(%rip),%rdi
call   1070 <die@plt>
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
jns    11d6 <create_socket+0x76>
lea    0xe4b(%rip),%rdi
call   1070 <die@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
movl   $0xa,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.timeval = type { i32, i64 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [28 x i8] c"Erro na criacao do socket.\0A\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [13 x i8] c"ioctl error!\00"
@SIOCGIFFLAGS = global i32 0
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @create_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.ifreq
%5 = alloca %struct.timeval
store i8* %0, i8** %2
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_ALL
%9 = call i32 @htons(i32 %8)
%10 = call i32 @socket(i32 %6, i32 %7, i32 %9)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %2
%18 = call i32 @strcpy(i32 %16, i8* %17)
%19 = load i32, i32* %3
%20 = load i32, i32* @SIOCGIFINDEX
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
%26 = load i32, i32* %3
%27 = load i32, i32* @SIOCGIFFLAGS
%28 = call i64 @ioctl(i32 %26, i32 %27, %struct.ifreq* %4)
%29 = load i32, i32* @IFF_PROMISC
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* %3
%34 = load i32, i32* @SIOCSIFFLAGS
%35 = call i64 @ioctl(i32 %33, i32 %34, %struct.ifreq* %4)
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 10, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %37
%38 = load i32, i32* %3
%39 = load i32, i32* @SOL_SOCKET
%40 = load i32, i32* @SO_RCVTIMEO
%41 = bitcast %struct.timeval* %5 to i8*
%42 = call i32 @setsockopt(i32 %38, i32 %39, i32 %40, i8* %41, i32 16)
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.timeval = type { i32, i64 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [28 x i8] c"Erro na criacao do socket.\0A\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [13 x i8] c"ioctl error!\00"
@SIOCGIFFLAGS = global i32 0
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @create_socket(i8* %0) {
%2 = alloca %struct.ifreq
%3 = alloca %struct.timeval
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @ETH_P_ALL
%7 = tail call i32 @htons(i32 %6)
%8 = tail call i32 @socket(i32 %4, i32 %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = bitcast %struct.ifreq* %2 to i8*
%14 = tail call i32 @strcpy(i32 undef, i8* %0)
%15 = load i32, i32* @SIOCGIFINDEX
%16 = call i64 @ioctl(i32 %8, i32 %15, %struct.ifreq* %2)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = load i32, i32* @SIOCGIFFLAGS
%22 = call i64 @ioctl(i32 %8, i32 %21, %struct.ifreq* %2)
%23 = load i32, i32* @IFF_PROMISC
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @SIOCSIFFLAGS
%28 = call i64 @ioctl(i32 %8, i32 %27, %struct.ifreq* %2)
%29 = bitcast %struct.timeval* %3 to i8*
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 10, i32* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %31
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_RCVTIMEO
%34 = call i32 @setsockopt(i32 %8, i32 %32, i32 %33, i8* %29, i32 16)
ret i32 %8
}