#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 */
typedef scalar_t__ uint8_t;
struct sockaddr {
  int sa_family;
  scalar_t__ *sa_data;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int device_ifindex(char *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct sockaddr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sock, ret, idx;
  struct sockaddr sa;
  char buff[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03,
                 0x04, 0x05, 0x06, 0xac, 0xdc, 0xee, 0xee, 0xee, 0xee};
  sock = socket(27, SOCK_RAW, 0);
  if (sock < 0) {
    perror("socket");
    return 0;
  }
  idx = device_ifindex("eth0");
  if (idx < 0) {
    ret = idx;
    goto out;
  }
  printf("Hit key1!\n");
  getchar();
  memset(&sa, 0, sizeof(sa));
  sa.sa_family = 27;
  sa.sa_data[0] = (uint8_t)idx;
  ret = bind(sock, &sa, sizeof(sa));
  if (ret < 0) {
    perror("bind");
    goto out;
  }
  while (1) {
    ret = sendto(sock, buff, sizeof(buff), 0, &sa, sizeof(sa));
    if (ret < 0) {
      perror("sendmsg");
      goto out;
    }
    printf("sent packet!\n");
  }
  ret = 0;
out:
  close(sock);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe54(%rip),%ax
mov    %ax,-0x30(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    $0x1b,%edi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fa <main+0x6a>
lea    0xe29(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <main+0x151>
lea    0xe18(%rip),%rdi
call   1090 <device_ifindex@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    121e <main+0x8e>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    12d3 <main+0x143>
lea    0xdf9(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movl   $0x1b,-0x20(%rbp)
movslq -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1282 <main+0xf2>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d3 <main+0x143>
jmp    1287 <main+0xf7>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
mov    $0x10,%r9d
call   1050 <sendto@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12c2 <main+0x132>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d3 <main+0x143>
lea    0xd6d(%rip),%rdi
call   1030 <printf@plt>
jmp    1287 <main+0xf7>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movaps 0xe5b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0xeeee,0x20(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    $0x1b,%edi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     11fa <main+0x6a>
mov    %eax,%r15d
lea    0xe43(%rip),%rdi
call   1090 <device_ifindex@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1208 <main+0x78>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
jmp    11e9 <main+0x59>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movl   $0x1b,(%rsp)
mov    %ebp,%eax
mov    0x8(%rsp),%rcx
mov    %rax,(%rcx)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
test   %eax,%eax
jns    125d <main+0xcd>
mov    %eax,%ebp
lea    0xdc3(%rip),%r14
jmp    12c9 <main+0x139>
lea    0x10(%rsp),%rsi
mov    %rsp,%r8
mov    %r15d,%edi
mov    $0x12,%edx
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1050 <sendto@plt>
lea    0xda2(%rip),%r14
test   %eax,%eax
js     12c7 <main+0x137>
lea    0xda9(%rip),%r12
lea    0x10(%rsp),%r13
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0x10,%r9d
call   1050 <sendto@plt>
test   %eax,%eax
jns    12a0 <main+0x110>
mov    %eax,%ebp
mov    %r14,%rdi
call   10a0 <perror@plt>
jmp    11e1 <main+0x51>

LLVM IR (-O0):
%struct.sockaddr = type { i32, i64* }
@__const.main.buff = [18 x i8] c"\FF\FF\FF\FF\FF\FF\01\02\03\04\05\06\AC\DC\EE\EE\EE\EE"
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"eth0\00"
@.str.2 = [11 x i8] c"Hit key1!\0A\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [8 x i8] c"sendmsg\00"
@.str.5 = [14 x i8] c"sent packet!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr
%6 = alloca [18 x i8]
store i32 0, i32* %1
%7 = bitcast [18 x i8]* %6 to i8*
%8 = load i32, i32* @SOCK_RAW
%9 = call i32 @socket(i32 27, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %51
14: ; preds = %0
%15 = call i32 @device_ifindex(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %4
store i32 %19, i32* %3
br label %47
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (...) @getchar()
%23 = call i32 @memset(%struct.sockaddr* %5, i32 0, i32 16)
%24 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 0
store i32 27, i32* %24
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 1
%28 = load i64*, i64** %27
%29 = getelementptr inbounds i64, i64* %28, i64 0
store i64 %26, i64* %29
%30 = load i32, i32* %2
%31 = call i32 @bind(i32 %30, %struct.sockaddr* %5, i32 16)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %47
36: ; preds = %20
br label %37
37: ; preds = %36, %45
%38 = load i32, i32* %2
%39 = getelementptr inbounds [18 x i8], [18 x i8]* %6, i64 0, i64 0
%40 = call i32 @sendto(i32 %38, i8* %39, i32 18, i32 0, %struct.sockaddr* %5, i32 16)
store i32 %40, i32* %3
%41 = load i32, i32* %3
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %47
45: ; preds = %37
%46 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %37
47: ; preds = %43, %34, %18
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %3
store i32 %50, i32* %1
br label %51
51: ; preds = %47, %12
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr = type { i32, i64* }
@__const.main.buff = [18 x i8] c"\FF\FF\FF\FF\FF\FF\01\02\03\04\05\06\AC\DC\EE\EE\EE\EE"
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"eth0\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [8 x i8] c"sendmsg\00"
@str = [10 x i8] c"Hit key1!\00"
@str.6 = [13 x i8] c"sent packet!\00"
define i32 @main() {
%1 = alloca %struct.sockaddr
%2 = alloca [18 x i8]
%3 = bitcast %struct.sockaddr* %1 to i8*
%4 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @SOCK_RAW
%6 = tail call i32 @socket(i32 27, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %37
10: ; preds = %0
%11 = tail call i32 @device_ifindex(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp slt i32 %11, 0
br i1 %12, label %34, label %13
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 (...) @getchar()
%16 = call i32 @memset(%struct.sockaddr* %1, i32 0, i32 16)
%17 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
store i32 27, i32* %17
%18 = zext i32 %11 to i64
%19 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 1
%20 = load i64*, i64** %19
store i64 %18, i64* %20
%21 = call i32 @bind(i32 %6, %struct.sockaddr* %1, i32 16)
%22 = icmp slt i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %13
%24 = call i32 @sendto(i32 %6, i8* %4, i32 18, i32 0, %struct.sockaddr* %1, i32 16)
%25 = icmp slt i32 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23, %26
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.6, i64 0, i64 0))
%28 = call i32 @sendto(i32 %6, i8* %4, i32 18, i32 0, %struct.sockaddr* %1, i32 16)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %26
30: ; preds = %26, %23, %13
%31 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %23 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %26 ]
%32 = phi i32 [ %21, %13 ], [ %24, %23 ], [ %28, %26 ]
%33 = call i32 @perror(i8* %31)
br label %34
34: ; preds = %30, %10
%35 = phi i32 [ %11, %10 ], [ %32, %30 ]
%36 = call i32 @close(i32 %6)
br label %37
37: ; preds = %34, %8
%38 = phi i32 [ 0, %8 ], [ %35, %34 ]
ret i32 %38
}