#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_ALL;
int /*<<< orphan*/ IFF_PROMISC;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ 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) {
  int sock;
  struct ifreq ifr;
  if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 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);
  }
  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,-0x20(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11d1 <getrawsock+0x61>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    122b <getrawsock+0xbb>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    126f <getrawsock+0xff>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%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    0x2e2c(%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     121f <getrawsock+0xaf>
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    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1228 <getrawsock+0xb8>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1231 <getrawsock+0xc1>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdda(%rip),%rdi
jmp    1238 <getrawsock+0xc8>
lea    0xde7(%rip),%rdi
jmp    1238 <getrawsock+0xc8>
lea    0xdf4(%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_ALL = 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"
define i32 @getrawsock(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.ifreq
store i8* %0, i8** %2
%5 = load i32, i32* @PF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @ETH_P_ALL
%8 = call i32 @htons(i32 %7)
%9 = call i32 @socket(i32 %5, i32 %6, i32 %8)
store i32 %9, i32* %3
%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* %4, i32 0, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, 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* %3
%22 = load i32, i32* @SIOCGIFFLAGS
%23 = call i64 @ioctl(i32 %21, i32 %22, %struct.ifreq* %4)
%24 = icmp slt i64 %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* %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 = icmp slt i64 %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* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = 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"
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_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 %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 i64 @ioctl(i32 %8, i32 %19, %struct.ifreq* %2)
%21 = icmp slt i64 %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 i64 @ioctl(i32 %8, i32 %30, %struct.ifreq* %2)
%32 = icmp slt i64 %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
ret i32 %8
}