#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 ifreq {
  int /*<<< orphan*/ ifr_flags;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ IFF_PROMISC;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int getrawsock(char *intname) {
  struct ifreq ifr;
  int sock;
  if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) {
    perror("can't open raw socket");
    exit(1);
  }
  memset(&ifr, 0x00, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, intname, IFNAMSIZ);
  if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
    perror("can't get socket info");
    exit(1);
  }
  ifr.ifr_flags |= IFF_PROMISC;
  if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
    perror("can't set promic");
    exit(1);
  }
  if (ioctl(sock, SIOCGIFINDEX, &ifr) == -1) {
    perror("can't retrieve etherenet index\n");
    exit(1);
  }
  return (sock);
}


x86 assembly (clang, -O0):
<getrawsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d1 <getrawsock+0x61>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%eax
jge    122a <getrawsock+0xba>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%eax
jge    126d <getrawsock+0xfd>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12a1 <getrawsock+0x131>
lea    0xdab(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrawsock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e66(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     123b <getrawsock+0xcb>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %eax,%eax
js     1244 <getrawsock+0xd4>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     124d <getrawsock+0xdd>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1256 <getrawsock+0xe6>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdbe(%rip),%rdi
jmp    125d <getrawsock+0xed>
lea    0xdcb(%rip),%rdi
jmp    125d <getrawsock+0xed>
lea    0xdd8(%rip),%rdi
jmp    125d <getrawsock+0xed>
lea    0xde0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@.str = [22 x i8] c"can't open raw socket\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [22 x i8] c"can't get socket info\00"
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [17 x i8] c"can't set promic\00"
@SIOCGIFINDEX = global i32 0
@.str.3 = [32 x i8] c"can't retrieve etherenet index\0A\00"
define i32 @getrawsock(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifreq
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @PF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @ETH_P_IP
%8 = call i32 @htons(i32 %7)
%9 = call i32 @socket(i32 %5, i32 %6, i32 %8)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %2
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %4
%22 = load i32, i32* @SIOCGIFFLAGS
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %3)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %14
%29 = load i32, i32* @IFF_PROMISC
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* %4
%34 = load i32, i32* @SIOCSIFFLAGS
%35 = call i32 @ioctl(i32 %33, i32 %34, %struct.ifreq* %3)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %28
%41 = load i32, i32* %4
%42 = load i32, i32* @SIOCGIFINDEX
%43 = call i32 @ioctl(i32 %41, i32 %42, %struct.ifreq* %3)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@.str = [22 x i8] c"can't open raw socket\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [22 x i8] c"can't get socket info\00"
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [17 x i8] c"can't set promic\00"
@SIOCGIFINDEX = global i32 0
@.str.3 = [32 x i8] c"can't retrieve etherenet index\0A\00"
define i32 @getrawsock(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @ETH_P_IP
%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 %13
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i32 %16, i8* %0, i32 %17)
%19 = load i32, i32* @SIOCGIFFLAGS
%20 = call i32 @ioctl(i32 %8, i32 %19, %struct.ifreq* %2)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %13
%26 = load i32, i32* @IFF_PROMISC
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%28 = load i32, i32* %27
%29 = or i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* @SIOCSIFFLAGS
%31 = call i32 @ioctl(i32 %8, i32 %30, %struct.ifreq* %2)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
%37 = load i32, i32* @SIOCGIFINDEX
%38 = call i32 @ioctl(i32 %8, i32 %37, %struct.ifreq* %2)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
ret i32 %8
}