#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 int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ netflow5_recv_pkt(char *, int, struct sockaddr_in *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
void mainloop(int netflow_sockfd) {
  char pkt_buf[8192];
  int pkt_len;
  struct sockaddr_in src_addr;
  socklen_t plen = sizeof(struct sockaddr_in);
  uint16_t *version = NULL;
  while (1) {
    memset(pkt_buf, 0, 8192);
    if ((pkt_len = recvfrom(netflow_sockfd, pkt_buf, 8192, 0,
                            (struct sockaddr *)&src_addr, &plen)) == -1) {
      perror("recvfrom()");
      continue;
    }
    version = (uint16_t *)pkt_buf;
    switch (ntohs(*version)) {
    case 5:
      netflow5_recv_pkt(pkt_buf, pkt_len, &src_addr);
      break;
    default:
      break;
    }
  }
}


x86 assembly (clang, -O0):
<mainloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0x201c(%rbp)
movq   $0x0,-0x2028(%rbp)
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x2010(%rbp),%rsi
lea    -0x2018(%rbp),%r8
mov    $0x2000,%edx
xor    %ecx,%ecx
lea    -0x201c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x2014(%rbp)
cmp    $0xffffffff,%eax
jne    11ca <mainloop+0x7a>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
jmp    1173 <mainloop+0x23>
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2028(%rbp)
mov    -0x2028(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
sub    $0x5,%eax
jne    1212 <mainloop+0xc2>
jmp    11f4 <mainloop+0xa4>
lea    -0x2010(%rbp),%rdi
mov    -0x2014(%rbp),%esi
lea    -0x2018(%rbp),%rdx
call   1030 <netflow5_recv_pkt@plt>
jmp    1217 <mainloop+0xc7>
jmp    1217 <mainloop+0xc7>
jmp    1173 <mainloop+0x23>

x86 assembly (clang, -O2):
<mainloop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %edi,%r13d
movl   $0x4,0x4(%rsp)
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xe7e(%rip),%r12
jmp    1198 <mainloop+0x48>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1040 <memset@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1050 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1190 <mainloop+0x40>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1060 <ntohs@plt>
cmp    $0x5,%eax
jne    1198 <mainloop+0x48>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <netflow5_recv_pkt@plt>
jmp    1198 <mainloop+0x48>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"recvfrom()\00"
define void @mainloop(i32 %0) {
%2 = alloca i32
%3 = alloca [8192 x i8]
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %2
store i32 4, i32* %6
store i32* null, i32** %7
br label %8
8: ; preds = %1, %16, %29
%9 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 8192)
%11 = load i32, i32* %2
%12 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%13 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%14 = call i32 @recvfrom(i32 %11, i8* %12, i32 8192, i32 0, %struct.sockaddr* %13, i32* %6)
store i32 %14, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %8
18: ; preds = %8
%19 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%20 = bitcast i8* %19 to i32*
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = load i32, i32* %21
%23 = call i32 @ntohs(i32 %22)
switch i32 %23, label %28 [
i32 5, label %24
]
24: ; preds = %18
%25 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @netflow5_recv_pkt(i8* %25, i32 %26, %struct.sockaddr_in* %5)
br label %29
28: ; preds = %18
br label %29
29: ; preds = %28, %24
br label %8
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"recvfrom()\00"
define void @mainloop(i32 %0) {
%2 = alloca [8192 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%8 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%9 = bitcast [8192 x i8]* %2 to i32*
br label %10
10: ; preds = %22, %1
%11 = call i32 @memset(i8* %5, i32 0, i32 8192)
%12 = call i32 @recvfrom(i32 %0, i8* %5, i32 8192, i32 0, %struct.sockaddr* %8, i32* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %22
16: ; preds = %10
%17 = load i32, i32* %9
%18 = call i32 @ntohs(i32 %17)
%19 = icmp eq i32 %18, 5
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @netflow5_recv_pkt(i8* %5, i32 %12, %struct.sockaddr_in* %3)
br label %22
22: ; preds = %20, %16, %14
br label %10
}