#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 */

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int ETH_P_ALL;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int send_socket(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strtol(char *, char **, int);
int main(int argc, char **argv) {
  int rc;
  int fd, raw_fd;
  char *endp;
  int protocol;
  int ret = EXIT_FAILURE;
  if (argc < 2 || argc > 3)
    goto out;
  if (argc == 3) {
    protocol = strtol(argv[2], &endp, 10);
    if (*argv[2] && *endp)
      goto out;
  } else {
    protocol = ETH_P_ALL;
  }
  fd = strtol(argv[1], &endp, 10);
  if (*endp)
    goto out;
  raw_fd = socket(AF_PACKET, SOCK_RAW, htons(protocol));
  if (raw_fd == -1)
    goto out;
  rc = send_socket(fd, raw_fd);
  if (rc == -1) {
    close(raw_fd);
    goto out;
  }
  ret = EXIT_SUCCESS;
out:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1186 <main+0x36>
cmpl   $0x3,-0x8(%rbp)
jle    118b <main+0x3b>
jmp    1283 <main+0x133>
cmpl   $0x3,-0x8(%rbp)
jne    11dc <main+0x8c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d7 <main+0x87>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d7 <main+0x87>
jmp    1283 <main+0x133>
jmp    11e8 <main+0x98>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1213 <main+0xc3>
jmp    1283 <main+0x133>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <htons@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1252 <main+0x102>
jmp    1283 <main+0x133>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <send_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1277 <main+0x127>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
jmp    1283 <main+0x133>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    0x2e62(%rip),%rax
mov    (%rax),%r14d
cmp    $0x3,%edi
jne    1185 <main+0x35>
mov    0x10(%rbp),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%ebx
jmp    118e <main+0x3e>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rbp),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11c4 <main+0x74>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e05(%rip),%rax
mov    (%rax),%r15d
mov    0x2e13(%rip),%rax
mov    (%rax),%r12d
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %r15d,%edi
mov    %r12d,%esi
mov    %eax,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11ad <main+0x5d>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <send_socket@plt>
cmp    $0xffffffff,%eax
je     120d <main+0xbd>
mov    0x2db8(%rip),%rax
mov    (%rax),%r14d
jmp    11ad <main+0x5d>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11ad <main+0x5d>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@ETH_P_ALL = global i32 0
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %11
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 3
br i1 %17, label %18, label %19
18: ; preds = %15, %2
br label %71
19: ; preds = %15
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @strtol(i8* %25, i8** %9, i32 10)
store i32 %26, i32* %10
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %22
%34 = load i8*, i8** %9
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %71
39: ; preds = %33, %22
br label %42
40: ; preds = %19
%41 = load i32, i32* @ETH_P_ALL
store i32 %41, i32* %10
br label %42
42: ; preds = %40, %39
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 @strtol(i8* %45, i8** %9, i32 10)
store i32 %46, i32* %7
%47 = load i8*, i8** %9
%48 = load i8, i8* %47
%49 = icmp ne i8 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %42
br label %71
51: ; preds = %42
%52 = load i32, i32* @AF_PACKET
%53 = load i32, i32* @SOCK_RAW
%54 = load i32, i32* %10
%55 = call i32 @htons(i32 %54)
%56 = call i32 @socket(i32 %52, i32 %53, i32 %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %60
59: ; preds = %51
br label %71
60: ; preds = %51
%61 = load i32, i32* %7
%62 = load i32, i32* %8
%63 = call i32 @send_socket(i32 %61, i32 %62)
store i32 %63, i32* %6
%64 = load i32, i32* %6
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
br label %71
69: ; preds = %60
%70 = load i32, i32* @EXIT_SUCCESS
store i32 %70, i32* %11
br label %71
71: ; preds = %69, %66, %59, %50, %38, %18
%72 = load i32, i32* %7
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %11
ret i32 %74
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@ETH_P_ALL = global i32 0
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = load i32, i32* @EXIT_FAILURE
%6 = and i32 %0, -2
%7 = icmp eq i32 %6, 2
tail call void @llvm.assume(i1 %7)
%8 = icmp eq i32 %0, 3
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = call i32 @strtol(i8* %11, i8** %3, i32 10)
%13 = load i8*, i8** %10
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %9
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
tail call void @llvm.assume(i1 %19)
br label %22
20: ; preds = %2
%21 = load i32, i32* @ETH_P_ALL
br label %22
22: ; preds = %16, %9, %20
%23 = phi i32 [ %12, %16 ], [ %12, %9 ], [ %21, %20 ]
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strtol(i8* %25, i8** %3, i32 10)
%27 = load i8*, i8** %3
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %22
%31 = load i32, i32* @AF_PACKET
%32 = load i32, i32* @SOCK_RAW
%33 = tail call i32 @htons(i32 %23)
%34 = tail call i32 @socket(i32 %31, i32 %32, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %43, label %36
36: ; preds = %30
%37 = tail call i32 @send_socket(i32 %26, i32 %34)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 @close(i32 %34)
br label %43
41: ; preds = %36
%42 = load i32, i32* @EXIT_SUCCESS
br label %43
43: ; preds = %30, %22, %41, %39
%44 = phi i32 [ %5, %22 ], [ %5, %30 ], [ %5, %39 ], [ %42, %41 ]
%45 = tail call i32 @close(i32 %26)
ret i32 %44
}