#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 {
  int dummy;
};
typedef int ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_RAW;
int USHRT_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ handle_packet(unsigned char *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sockfd == -1)
    do {
      perror("socket failed");
      exit(EXIT_FAILURE);
    } while (0);
  unsigned char *buffer =
      (unsigned char *)malloc(sizeof(unsigned char) * USHRT_MAX);
  if (buffer == NULL)
    do {
      perror("malloc buffer failed");
      exit(EXIT_FAILURE);
    } while (0);
  struct sockaddr address;
  socklen_t address_len = sizeof(address);
  ssize_t recv_bytes;
  while (1) {
    recv_bytes = recvfrom(sockfd, buffer, USHRT_MAX, 0, &address, &address_len);
    if (recv_bytes == -1)
      do {
        perror("recvfrom failed");
        exit(EXIT_FAILURE);
      } while (0);
    handle_packet(buffer, recv_bytes);
  }
  close(sockfd);
  return EXIT_SUCCESS;
}


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    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e5 <main+0x75>
jmp    11cb <main+0x5b>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2df4(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1228 <main+0xb8>
jmp    120e <main+0x9e>
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x4,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    127a <main+0x10a>
jmp    1260 <main+0xf0>
lea    0xdbc(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1030 <handle_packet@plt>
jmp    122f <main+0xbf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e45(%rip),%rax
mov    (%rax),%ebx
mov    0x2e5c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
jne    11b8 <main+0x48>
lea    0xe4d(%rip),%rdi
jmp    123c <main+0xcc>
mov    %eax,%ebp
mov    0x2e1f(%rip),%r12
mov    (%r12),%edi
call   1050 <malloc@plt>
test   %rax,%rax
jne    11d8 <main+0x68>
lea    0xe38(%rip),%rdi
jmp    123c <main+0xcc>
mov    %rax,%rbx
movl   $0x4,0x4(%rsp)
mov    (%r12),%edx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebp,%edi
mov    %rax,%rsi
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xc5>
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <handle_packet@plt>
mov    (%r12),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1060 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0xa0>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [14 x i8] c"socket failed\00"
@EXIT_FAILURE = global i32 0
@USHRT_MAX = global i32 0
@.str.1 = [21 x i8] c"malloc buffer failed\00"
@.str.2 = [16 x i8] c"recvfrom failed\00"
@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 i8*
%8 = alloca %struct.sockaddr
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_PACKET
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* @ETH_P_ALL
%14 = call i32 @htons(i32 %13)
%15 = call i32 @socket(i32 %11, i32 %12, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %2
br label %19
19: ; preds = %18
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; No predecessors!
br label %24
24: ; preds = %23, %2
%25 = load i32, i32* @USHRT_MAX
%26 = sext i32 %25 to i64
%27 = mul i64 1, %26
%28 = trunc i64 %27 to i32
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %39
33: ; preds = %24
br label %34
34: ; preds = %33
%35 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; No predecessors!
br label %39
39: ; preds = %38, %24
store i32 4, i32* %9
br label %40
40: ; preds = %39, %53
%41 = load i32, i32* %6
%42 = load i8*, i8** %7
%43 = load i32, i32* @USHRT_MAX
%44 = call i32 @recvfrom(i32 %41, i8* %42, i32 %43, i32 0, %struct.sockaddr* %8, i32* %9)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %40
br label %48
48: ; preds = %47
%49 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; No predecessors!
br label %53
53: ; preds = %52, %40
%54 = load i8*, i8** %7
%55 = load i32, i32* %10
%56 = call i32 @handle_packet(i8* %54, i32 %55)
br label %40
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [14 x i8] c"socket failed\00"
@EXIT_FAILURE = global i32 0
@USHRT_MAX = global i32 0
@.str.1 = [21 x i8] c"malloc buffer failed\00"
@.str.2 = [16 x i8] c"recvfrom failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr
%4 = alloca i32
%5 = load i32, i32* @AF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @ETH_P_ALL
%8 = tail call i32 @htons(i32 %7)
%9 = tail call i32 @socket(i32 %5, i32 %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32, i32* @USHRT_MAX
%17 = tail call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = bitcast %struct.sockaddr* %3 to i8*
%26 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%27 = load i32, i32* @USHRT_MAX
%28 = call i32 @recvfrom(i32 %9, i8* %18, i32 %27, i32 0, %struct.sockaddr* %3, i32* %4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %34, %24
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %24, %34
%35 = phi i32 [ %38, %34 ], [ %28, %24 ]
%36 = call i32 @handle_packet(i8* %18, i32 %35)
%37 = load i32, i32* @USHRT_MAX
%38 = call i32 @recvfrom(i32 %9, i8* %18, i32 %37, i32 0, %struct.sockaddr* %3, i32* %4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %30, label %34
}