#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 sockaddr_ll {
  int sll_ifindex;
  int /*<<< orphan*/ sll_pkttype;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socket_address;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ PACKET_OTHERHOST;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strtol(char *, char **, int);
int main(int argc, char *argv[]) {
  char buf[1024];
  int sz, i;
  int sk, proto = 0x9000;
  struct sockaddr_ll socket_address;
  if (argc >= 2)
    proto = strtol(argv[1], (char **)NULL, 16);
  printf("Open Raw socket for protocol 0x%x\n", proto);
  sk = socket(AF_PACKET, SOCK_RAW, htons(proto));
  if (sk < 0) {
    perror("open:");
    goto out;
  }
  do {
    sz = recv(sk, buf, 1024, 0);
    if (sz == -1) {
      perror("recv ERROR");
      goto out;
    }
    printf("read %d bytes\n", sz);
    i = 0;
    socket_address.sll_family = PF_PACKET;
    socket_address.sll_protocol = htons(proto);
    socket_address.sll_ifindex = 2;
    socket_address.sll_pkttype = PACKET_OTHERHOST;
    sz = sendto(sk, buf, sz, 0, (struct sockaddr *)&socket_address,
                sizeof(socket_address));
    if (sz == -1) {
      perror("send ERROR");
      goto out;
    }
    printf("send %d bytes\n", sz);
  } while (1);
out:
  if (sk > 0)
    close(sk);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x9000,-0x420(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11c9 <main+0x49>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %eax,-0x420(%rbp)
mov    -0x420(%rbp),%esi
lea    0xe2a(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
mov    -0x420(%rbp),%edi
call   1040 <htons@plt>
mov    -0x438(%rbp),%edi
mov    -0x434(%rbp),%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jge    123b <main+0xbb>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
jmp    1343 <main+0x1c3>
jmp    1240 <main+0xc0>
mov    -0x41c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    127d <main+0xfd>
lea    0xdb6(%rip),%rdi
call   1090 <perror@plt>
jmp    1343 <main+0x1c3>
mov    -0x414(%rbp),%esi
lea    0xdaa(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x418(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    -0x420(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x428(%rbp)
movl   $0x2,-0x430(%rbp)
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x41c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
lea    -0x430(%rbp),%r8
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    131d <main+0x19d>
lea    0xd30(%rip),%rdi
call   1090 <perror@plt>
jmp    1343 <main+0x1c3>
mov    -0x414(%rbp),%esi
lea    0xd24(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1240 <main+0xc0>
jmp    133e <main+0x1be>
jmp    1343 <main+0x1c3>
cmpl   $0x0,-0x41c(%rbp)
jle    135b <main+0x1db>
mov    -0x41c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    $0x9000,%r12d
cmp    $0x2,%edi
jl     11af <main+0x2f>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %eax,%r12d
lea    0xe4a(%rip),%rdi
mov    %r12d,%esi
call   1050 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
mov    0x2e12(%rip),%rax
mov    (%rax),%ebp
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10a0 <socket@plt>
test   %eax,%eax
jns    11f8 <main+0x78>
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
jmp    12cc <main+0x14c>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xe15(%rip),%rdi
cmp    $0xffffffff,%eax
je     12bc <main+0x13c>
mov    %eax,%r15d
lea    0x10(%rsp),%rbx
mov    %rsp,%r13
lea    0xe1f(%rip),%r14
nop
lea    0xdfd(%rip),%rdi
mov    %r15d,%esi
call   1050 <printf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
movl   $0x2,(%rsp)
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0x10,%r9d
call   1060 <sendto@plt>
cmp    $0xffffffff,%eax
je     12b5 <main+0x135>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1230 <main+0xb0>
lea    0xd76(%rip),%rdi
jmp    12bc <main+0x13c>
lea    0xd87(%rip),%rdi
call   1090 <perror@plt>
test   %ebp,%ebp
jle    12cc <main+0x14c>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Open Raw socket for protocol 0x%x\0A\00"
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [6 x i8] c"open:\00"
@.str.2 = [11 x i8] c"recv ERROR\00"
@.str.3 = [15 x i8] c"read %d bytes\0A\00"
@PF_PACKET = global i32 0
@PACKET_OTHERHOST = global i32 0
@.str.4 = [11 x i8] c"send ERROR\00"
@.str.5 = [15 x i8] c"send %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_ll
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 36864, i32* %10
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strtol(i8* %17, i8** null, i32 16)
store i32 %18, i32* %10
br label %19
19: ; preds = %14, %2
%20 = load i32, i32* %10
%21 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @AF_PACKET
%23 = load i32, i32* @SOCK_RAW
%24 = load i32, i32* %10
%25 = call i32 @htons(i32 %24)
%26 = call i32 @socket(i32 %22, i32 %23, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %65
31: ; preds = %19
br label %32
32: ; preds = %63, %31
%33 = load i32, i32* %9
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = call i32 @recv(i32 %33, i8* %34, i32 1024, i32 0)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %65
40: ; preds = %32
%41 = load i32, i32* %7
%42 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %41)
store i32 0, i32* %8
%43 = load i32, i32* @PF_PACKET
%44 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 3
store i32 %43, i32* %44
%45 = load i32, i32* %10
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 2
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 0
store i32 2, i32* %48
%49 = load i32, i32* @PACKET_OTHERHOST
%50 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 1
store i32 %49, i32* %50
%51 = load i32, i32* %9
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%53 = load i32, i32* %7
%54 = bitcast %struct.sockaddr_ll* %11 to %struct.sockaddr*
%55 = call i32 @sendto(i32 %51, i8* %52, i32 %53, i32 0, %struct.sockaddr* %54, i32 16)
store i32 %55, i32* %7
%56 = load i32, i32* %7
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %40
%59 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %65
60: ; preds = %40
%61 = load i32, i32* %7
%62 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %60
br i1 true, label %32, label %64
64: ; preds = %63
br label %65
65: ; preds = %64, %58, %38, %29
%66 = load i32, i32* %9
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* %9
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %65
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Open Raw socket for protocol 0x%x\0A\00"
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [6 x i8] c"open:\00"
@.str.2 = [11 x i8] c"recv ERROR\00"
@.str.3 = [15 x i8] c"read %d bytes\0A\00"
@PF_PACKET = global i32 0
@PACKET_OTHERHOST = global i32 0
@.str.4 = [11 x i8] c"send ERROR\00"
@.str.5 = [15 x i8] c"send %d bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_ll
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_ll* %4 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strtol(i8* nocapture %10, i8** null, i32 16)
br label %12
12: ; preds = %8, %2
%13 = phi i32 [ %11, %8 ], [ 36864, %2 ]
%14 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @AF_PACKET
%16 = load i32, i32* @SOCK_RAW
%17 = tail call i32 @htons(i32 %13)
%18 = tail call i32 @socket(i32 %15, i32 %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %12
%21 = call i32 @recv(i32 %18, i8* %5, i32 1024, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %43, label %23
23: ; preds = %20
%24 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 3
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 2
%26 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 0
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 1
%28 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
br label %31
29: ; preds = %12
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %49
31: ; preds = %23, %39
%32 = phi i32 [ %21, %23 ], [ %41, %39 ]
%33 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @PF_PACKET
store i32 %34, i32* %24
%35 = call i32 @htons(i32 %13)
store i32 %35, i32* %25
store i32 2, i32* %26
%36 = load i32, i32* @PACKET_OTHERHOST
store i32 %36, i32* %27
%37 = call i32 @sendto(i32 %18, i8* %5, i32 %32, i32 0, %struct.sockaddr* %28, i32 16)
%38 = icmp eq i32 %37, -1
br i1 %38, label %43, label %39
39: ; preds = %31
%40 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%41 = call i32 @recv(i32 %18, i8* %5, i32 1024, i32 0)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %31
43: ; preds = %31, %39, %20
%44 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %39 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), %31 ]
%45 = call i32 @perror(i8* %44)
%46 = icmp sgt i32 %18, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @close(i32 %18)
br label %49
49: ; preds = %29, %47, %43
ret i32 0
}