#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 nlmsghdr {
  int nlmsg_type;
};

/* Variables and functions */
int /*<<< orphan*/ NLMSG_SPACE(int);
#define RTM_DELROUTE 130
#define RTM_GETROUTE 129
#define RTM_NEWROUTE 128
struct nlmsghdr *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct nlmsghdr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int recv(int, void *, int /*<<< orphan*/, int /*<<< orphan*/);
int read_event(int sock) {
  int ret;
  struct nlmsghdr *nlh;
  nlh = malloc(NLMSG_SPACE(1024));
  memset(nlh, 0, NLMSG_SPACE(1024));
  ret = recv(sock, (void *)nlh, NLMSG_SPACE(1024), 0);
  switch (nlh->nlmsg_type) {
  case RTM_NEWROUTE:
    printf("NEWROUTE\n");
    break;
  case RTM_DELROUTE:
    printf("DELROUTE\n");
    break;
  case RTM_GETROUTE:
    printf("GETROUTE\n");
    break;
  default:
    printf("Unknown type\n");
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x400,%edi
mov    %edi,-0x28(%rbp)
call   1060 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   1070 <malloc@plt>
mov    -0x28(%rbp),%edi
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
call   1060 <NLMSG_SPACE@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
mov    %esi,-0x18(%rbp)
call   1050 <memset@plt>
mov    -0x28(%rbp),%edi
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
call   1060 <NLMSG_SPACE@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%ecx
mov    %eax,%edx
call   1030 <recv@plt>
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     11fc <read_event+0xac>
jmp    11d6 <read_event+0x86>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
je     121e <read_event+0xce>
jmp    11e9 <read_event+0x99>
mov    -0x14(%rbp),%eax
sub    $0x82,%eax
je     120d <read_event+0xbd>
jmp    122f <read_event+0xdf>
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
jmp    123b <read_event+0xeb>
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
jmp    123b <read_event+0xeb>
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
jmp    123b <read_event+0xeb>
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_event>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x400,%edi
call   1060 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    $0x400,%edi
call   1060 <NLMSG_SPACE@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1050 <memset@plt>
mov    $0x400,%edi
call   1060 <NLMSG_SPACE@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    (%rbx),%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
ja     11b3 <read_event+0x63>
cltq
lea    0xe7e(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    11ba <read_event+0x6a>
lea    0xe61(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
@.str = [10 x i8] c"NEWROUTE\0A\00"
@.str.1 = [10 x i8] c"DELROUTE\0A\00"
@.str.2 = [10 x i8] c"GETROUTE\0A\00"
@.str.3 = [14 x i8] c"Unknown type\0A\00"
define i32 @read_event(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.nlmsghdr*
store i32 %0, i32* %2
%5 = call i32 @NLMSG_SPACE(i32 1024)
%6 = call %struct.nlmsghdr* @malloc(i32 %5)
store %struct.nlmsghdr* %6, %struct.nlmsghdr** %4
%7 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%8 = call i32 @NLMSG_SPACE(i32 1024)
%9 = call i32 @memset(%struct.nlmsghdr* %7, i32 0, i32 %8)
%10 = load i32, i32* %2
%11 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%12 = bitcast %struct.nlmsghdr* %11 to i8*
%13 = call i32 @NLMSG_SPACE(i32 1024)
%14 = call i32 @recv(i32 %10, i8* %12, i32 %13, i32 0)
store i32 %14, i32* %3
%15 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%16 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %15, i32 0, i32 0
%17 = load i32, i32* %16
switch i32 %17, label %24 [
i32 128, label %18
i32 130, label %20
i32 129, label %22
]
18: ; preds = %1
%19 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %26
20: ; preds = %1
%21 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %26
22: ; preds = %1
%23 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %26
24: ; preds = %1
%25 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %22, %20, %18
ret i32 0
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
@str = [9 x i8] c"GETROUTE\00"
@str.4 = [9 x i8] c"DELROUTE\00"
@str.5 = [9 x i8] c"NEWROUTE\00"
@str.6 = [13 x i8] c"Unknown type\00"
@reltable.read_event = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @str.5 to i64), i64 ptrtoint ([3 x i32]* @reltable.read_event to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @str to i64), i64 ptrtoint ([3 x i32]* @reltable.read_event to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @str.4 to i64), i64 ptrtoint ([3 x i32]* @reltable.read_event to i64)) to i32)]
define i32 @read_event(i32 %0) {
%2 = tail call i32 @NLMSG_SPACE(i32 1024)
%3 = tail call %struct.nlmsghdr* @malloc(i32 %2)
%4 = tail call i32 @NLMSG_SPACE(i32 1024)
%5 = tail call i32 @memset(%struct.nlmsghdr* %3, i32 0, i32 %4)
%6 = bitcast %struct.nlmsghdr* %3 to i8*
%7 = tail call i32 @NLMSG_SPACE(i32 1024)
%8 = tail call i32 @recv(i32 %0, i8* %6, i32 %7, i32 0)
%9 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = add i32 %10, -128
%12 = icmp ult i32 %11, 3
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = sext i32 %11 to i64
%15 = shl i64 %14, 2
%16 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.read_event to i8*), i64 %15)
br label %17
17: ; preds = %1, %13
%18 = phi i8* [ %16, %13 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.6, i64 0, i64 0), %1 ]
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
ret i32 0
}