#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 pollfd {
  int fd;
  int revents;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bind_instance_interface(int);
int /*<<< orphan*/ free(struct pollfd *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ malloc(int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char buf[1024];
  int bytes;
  struct pollfd *pollfd;
  int i = 0;
  int nummonitor = 0;
  int numready;
  int errno;
  char *str;
  int skfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  if (skfd < 0) {
    printf("Failed to create raw socket!\n");
    return -1;
  }
  bind_instance_interface(skfd);
  pollfd = (struct pollfd *)malloc(sizeof(struct pollfd));
  if (pollfd == NULL) {
    printf("Failed to malloc memory!\n");
    return -1;
  }
  for (i; i < 1; i++) {
    (pollfd + i)->fd = skfd;
    if ((pollfd + i)->fd >= 0)
      (pollfd + i)->events = POLLIN;
  }
  while (1) {
    int ret;
    ret = poll(pollfd, 1, 1);
    if (ret < 0 && errno != EINTR) {
      printf("Poll failed!\n");
      return -1;
    }
    if (ret == 0)
      continue;
    if (ret > 0) {
      for (i = 0; i < 1; i++) {
        if (pollfd[i].revents & (0x010 | 0x2000 | 0x008 | 0x020)) {
          if (pollfd[i].revents & (0x010 | 0x2000))
            printf("Hang-up on a packet socket\n");
          else if (pollfd[i].revents & 0x008)
            printf("Encountered error condition on a packet socket\n");
          else if (pollfd[i].revents & 0x020)
            printf("Invalid polling request on a packet socket\n");
        }
      }
    }
  }
  free(pollfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x424(%rbp)
movl   $0x0,-0x428(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x448(%rbp),%edi
mov    -0x444(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0x0,-0x43c(%rbp)
jge    11f7 <main+0x97>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143d <main+0x2dd>
mov    -0x43c(%rbp),%edi
call   1080 <bind_instance_interface@plt>
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1239 <main+0xd9>
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143d <main+0x2dd>
cmpl   $0x1,-0x424(%rbp)
jge    12bb <main+0x15b>
mov    -0x43c(%rbp),%ecx
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,(%rax)
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
cmpl   $0x0,(%rax)
jl     12a2 <main+0x142>
mov    0x2d50(%rip),%rax
mov    (%rax),%ecx
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
jmp    12a7 <main+0x147>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    1239 <main+0xd9>
jmp    12c0 <main+0x160>
mov    -0x420(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <poll@plt>
mov    %eax,-0x440(%rbp)
cmpl   $0x0,-0x440(%rbp)
jge    1313 <main+0x1b3>
mov    -0x430(%rbp),%eax
mov    0x2cd5(%rip),%rcx
cmp    (%rcx),%eax
je     1313 <main+0x1b3>
lea    0xd36(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143d <main+0x2dd>
cmpl   $0x0,-0x440(%rbp)
jne    1325 <main+0x1c5>
jmp    12c0 <main+0x160>
cmpl   $0x0,-0x440(%rbp)
jle    1438 <main+0x2d8>
movl   $0x0,-0x424(%rbp)
cmpl   $0x1,-0x424(%rbp)
jge    1433 <main+0x2d3>
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%eax
and    $0x2038,%eax
cmp    $0x0,%eax
je     141a <main+0x2ba>
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%eax
and    $0x2010,%eax
cmp    $0x0,%eax
je     13a6 <main+0x246>
lea    0xcaa(%rip),%rdi
call   1040 <printf@plt>
jmp    1415 <main+0x2b5>
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     13db <main+0x27b>
lea    0xc91(%rip),%rdi
call   1040 <printf@plt>
jmp    1410 <main+0x2b0>
mov    -0x420(%rbp),%rax
movslq -0x424(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     140b <main+0x2ab>
lea    0xc8c(%rip),%rdi
call   1040 <printf@plt>
jmp    1410 <main+0x2b0>
jmp    1415 <main+0x2b5>
jmp    141a <main+0x2ba>
jmp    141f <main+0x2bf>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    133c <main+0x1dc>
jmp    1438 <main+0x2d8>
jmp    12c0 <main+0x160>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e82(%rip),%rax
mov    (%rax),%ebx
mov    0x2e69(%rip),%rax
mov    (%rax),%ebp
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     121f <main+0xbf>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <bind_instance_interface@plt>
mov    $0xc,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1228 <main+0xc8>
mov    %rax,%rbx
mov    %ebp,(%rax)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
lea    0xe90(%rip),%rbp
lea    0xe5a(%rip),%r14
lea    0xe28(%rip),%r15
jmp    11e5 <main+0x85>
nopw   0x0(%rax,%rax,1)
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1060 <poll@plt>
test   %eax,%eax
jle    11e5 <main+0x85>
mov    0x4(%rbx),%eax
test   $0x2038,%eax
je     11e5 <main+0x85>
mov    %rbp,%rdi
test   $0x2010,%eax
jne    11e0 <main+0x80>
mov    %r14,%rdi
test   $0x8,%al
jne    11e0 <main+0x80>
mov    %r15,%rdi
test   $0x20,%al
je     11e5 <main+0x85>
jmp    11e0 <main+0x80>
lea    0xe68(%rip),%rdi
jmp    122f <main+0xcf>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@.str = [30 x i8] c"Failed to create raw socket!\0A\00"
@.str.1 = [26 x i8] c"Failed to malloc memory!\0A\00"
@POLLIN = global i32 0
@EINTR = global i32 0
@.str.2 = [14 x i8] c"Poll failed!\0A\00"
@.str.3 = [28 x i8] c"Hang-up on a packet socket\0A\00"
@.str.4 = [48 x i8] c"Encountered error condition on a packet socket\0A\00"
@.str.5 = [44 x i8] c"Invalid polling request on a packet socket\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 %struct.pollfd*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i32 0, i32* %10
%16 = load i32, i32* @PF_PACKET
%17 = load i32, i32* @SOCK_RAW
%18 = load i32, i32* @ETH_P_IP
%19 = call i32 @htons(i32 %18)
%20 = call i32 @socket(i32 %16, i32 %17, i32 %19)
store i32 %20, i32* %14
%21 = load i32, i32* %14
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %138
25: ; preds = %2
%26 = load i32, i32* %14
%27 = call i32 @bind_instance_interface(i32 %26)
%28 = call i64 @malloc(i32 12)
%29 = inttoptr i64 %28 to %struct.pollfd*
store %struct.pollfd* %29, %struct.pollfd** %8
%30 = load %struct.pollfd*, %struct.pollfd** %8
%31 = icmp eq %struct.pollfd* %30, null
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %138
34: ; preds = %25
%35 = load i32, i32* %9
br label %36
36: ; preds = %61, %34
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %64
39: ; preds = %36
%40 = load i32, i32* %14
%41 = load %struct.pollfd*, %struct.pollfd** %8
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i64 %43
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %44, i32 0, i32 0
store i32 %40, i32* %45
%46 = load %struct.pollfd*, %struct.pollfd** %8
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %46, i64 %48
%50 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = icmp sge i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %39
%54 = load i32, i32* @POLLIN
%55 = load %struct.pollfd*, %struct.pollfd** %8
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i64 %57
%59 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %58, i32 0, i32 2
store i32 %54, i32* %59
br label %60
60: ; preds = %53, %39
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %36
64: ; preds = %36
br label %65
65: ; preds = %64, %79, %137
%66 = load %struct.pollfd*, %struct.pollfd** %8
%67 = call i32 @poll(%struct.pollfd* %66, i32 1, i32 1)
store i32 %67, i32* %15
%68 = load i32, i32* %15
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %76
70: ; preds = %65
%71 = load i32, i32* %12
%72 = load i32, i32* @EINTR
%73 = icmp ne i32 %71, %72
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %138
76: ; preds = %70, %65
%77 = load i32, i32* %15
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %76
br label %65
80: ; preds = %76
%81 = load i32, i32* %15
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %137
83: ; preds = %80
store i32 0, i32* %9
br label %84
84: ; preds = %133, %83
%85 = load i32, i32* %9
%86 = icmp slt i32 %85, 1
br i1 %86, label %87, label %136
87: ; preds = %84
%88 = load %struct.pollfd*, %struct.pollfd** %8
%89 = load i32, i32* %9
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %88, i64 %90
%92 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %91, i32 0, i32 1
%93 = load i32, i32* %92
%94 = and i32 %93, 8248
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %132
96: ; preds = %87
%97 = load %struct.pollfd*, %struct.pollfd** %8
%98 = load i32, i32* %9
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %97, i64 %99
%101 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %100, i32 0, i32 1
%102 = load i32, i32* %101
%103 = and i32 %102, 8208
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %107
105: ; preds = %96
%106 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %131
107: ; preds = %96
%108 = load %struct.pollfd*, %struct.pollfd** %8
%109 = load i32, i32* %9
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %108, i64 %110
%112 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %111, i32 0, i32 1
%113 = load i32, i32* %112
%114 = and i32 %113, 8
%115 = icmp ne i32 %114, 0
br i1 %115, label %116, label %118
116: ; preds = %107
%117 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
br label %130
118: ; preds = %107
%119 = load %struct.pollfd*, %struct.pollfd** %8
%120 = load i32, i32* %9
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %119, i64 %121
%123 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %122, i32 0, i32 1
%124 = load i32, i32* %123
%125 = and i32 %124, 32
%126 = icmp ne i32 %125, 0
br i1 %126, label %127, label %129
127: ; preds = %118
%128 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %129
129: ; preds = %127, %118
br label %130
130: ; preds = %129, %116
br label %131
131: ; preds = %130, %105
br label %132
132: ; preds = %131, %87
br label %133
133: ; preds = %132
%134 = load i32, i32* %9
%135 = add nsw i32 %134, 1
store i32 %135, i32* %9
br label %84
136: ; preds = %84
br label %137
137: ; preds = %136, %80
br label %65
138: ; preds = %74, %32, %23
%139 = load i32, i32* %3
ret i32 %139
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@POLLIN = global i32 0
@EINTR = global i32 0
@str = [43 x i8] c"Invalid polling request on a packet socket\00"
@str.6 = [47 x i8] c"Encountered error condition on a packet socket\00"
@str.7 = [27 x i8] c"Hang-up on a packet socket\00"
@str.8 = [25 x i8] c"Failed to malloc memory!\00"
@str.9 = [29 x i8] c"Failed to create raw socket!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PF_PACKET
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @ETH_P_IP
%6 = tail call i32 @htons(i32 %5)
%7 = tail call i32 @socket(i32 %3, i32 %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %39, label %9
9: ; preds = %2
%10 = tail call i32 @bind_instance_interface(i32 %7)
%11 = tail call i64 @malloc(i32 12)
%12 = inttoptr i64 %11 to %struct.pollfd*
%13 = icmp eq i64 %11, 0
br i1 %13, label %39, label %14
14: ; preds = %9
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i64 0, i32 0
store i32 %7, i32* %15
%16 = load i32, i32* @POLLIN
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i64 0, i32 1
br label %19
19: ; preds = %38, %14
%20 = tail call i32 @poll(%struct.pollfd* %12, i32 1, i32 1)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %19
%23 = load i32, i32* %18
%24 = and i32 %23, 8248
%25 = icmp eq i32 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %22
%27 = and i32 %23, 8208
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = and i32 %23, 8
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = and i32 %23, 32
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32, %29, %26
%36 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0), %26 ], [ getelementptr inbounds ([47 x i8], [47 x i8]* @str.6, i64 0, i64 0), %29 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0), %32 ]
%37 = tail call i32 @puts(i8* dereferenceable(1) %36)
br label %38
38: ; preds = %35, %32, %22, %19
br label %19
39: ; preds = %9, %2
%40 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @str.9, i64 0, i64 0), %2 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0), %9 ]
%41 = tail call i32 @puts(i8* dereferenceable(1) %40)
ret i32 -1
}