#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct message {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ source;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *const);
int /*<<< orphan*/ handle_message(char *const, struct message *const);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
scalar_t__ is_local_address(char *const);
int /*<<< orphan*/ message_destroy(struct message *const);
struct message *parse_message(unsigned char *, size_t);
int recvfrom(int const, unsigned char *, int, int /*<<< orphan*/,
             struct sockaddr *, int *);
char *strdup(int /*<<< orphan*/);
void receive_message(const int fd) {
  static struct sockaddr_in source;
  static socklen_t size = sizeof(source);
  static unsigned char buffer[256];
  const ssize_t received =
      recvfrom(fd, buffer, 256, 0, (struct sockaddr *)&source, &size);
  if (received == -1) {
    return;
  }
  char *const ip = strdup(inet_ntoa(source.sin_addr));
  if (ip == NULL) {
    return;
  }
  if (is_local_address(ip)) {
    free(ip);
    return;
  }
  struct message *const message = parse_message(buffer, (size_t)received);
  if (message == NULL) {
    free(ip);
    return;
  }
  handle_message(ip, message);
  message_destroy(message);
  free(ip);
}


x86 assembly (clang, -O0):
<receive_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0x2efb(%rip),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
lea    0x2edd(%rip),%r8
lea    0x2eb6(%rip),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c1 <receive_message+0x41>
jmp    1254 <receive_message+0xd4>
mov    0x2eb9(%rip),%edi
call   1060 <inet_ntoa@plt>
mov    %eax,%edi
call   10a0 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e7 <receive_message+0x67>
jmp    1254 <receive_message+0xd4>
mov    -0x10(%rbp),%rdi
call   1070 <is_local_address@plt>
cmp    $0x0,%rax
je     1208 <receive_message+0x88>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    1254 <receive_message+0xd4>
movslq -0x8(%rbp),%rsi
lea    0x2e7d(%rip),%rdi
call   1050 <parse_message@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1235 <receive_message+0xb5>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    1254 <receive_message+0xd4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <handle_message@plt>
mov    -0x18(%rbp),%rdi
call   1040 <message_destroy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_message>:
push   %rbp
push   %rbx
push   %rax
lea    0x2f06(%rip),%rsi
lea    0x2eef(%rip),%r8
lea    0x2ec8(%rip),%r9
mov    $0x100,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     120a <receive_message+0x8a>
mov    %eax,%ebp
mov    0x2ecf(%rip),%edi
call   1060 <inet_ntoa@plt>
mov    %eax,%edi
call   10a0 <strdup@plt>
test   %rax,%rax
je     120a <receive_message+0x8a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <is_local_address@plt>
test   %rax,%rax
jne    11fc <receive_message+0x7c>
movslq %ebp,%rsi
lea    0x2eb4(%rip),%rdi
call   1050 <parse_message@plt>
test   %rax,%rax
je     11fc <receive_message+0x7c>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
call   1080 <handle_message@plt>
mov    %rbp,%rdi
call   1040 <message_destroy@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.message = type { i32 }
%struct.sockaddr = type { i32 }
@receive_message.source = internal global %struct.sockaddr_in zeroinitializer
@receive_message.size = internal global i32 4
@receive_message.buffer = internal global [256 x i8] zeroinitializer
define void @receive_message(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.message*
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @recvfrom(i32 %6, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @receive_message.buffer, i64 0, i64 0), i32 256, i32 0, %struct.sockaddr* bitcast (%struct.sockaddr_in* @receive_message.source to %struct.sockaddr*), i32* @receive_message.size)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
br label %42
11: ; preds = %1
%12 = load i32, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @receive_message.source, i32 0, i32 0)
%13 = call i32 @inet_ntoa(i32 %12)
%14 = call i8* @strdup(i32 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %11
br label %42
18: ; preds = %11
%19 = load i8*, i8** %4
%20 = call i64 @is_local_address(i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
br label %42
25: ; preds = %18
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = call %struct.message* @parse_message(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @receive_message.buffer, i64 0, i64 0), i64 %27)
store %struct.message* %28, %struct.message** %5
%29 = load %struct.message*, %struct.message** %5
%30 = icmp eq %struct.message* %29, null
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i8*, i8** %4
%33 = call i32 @free(i8* %32)
br label %42
34: ; preds = %25
%35 = load i8*, i8** %4
%36 = load %struct.message*, %struct.message** %5
%37 = call i32 @handle_message(i8* %35, %struct.message* %36)
%38 = load %struct.message*, %struct.message** %5
%39 = call i32 @message_destroy(%struct.message* %38)
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %34, %31, %22, %17, %10
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
%struct.message = type { i32 }
@receive_message.source = internal global %struct.sockaddr_in zeroinitializer
@receive_message.size = internal global i32 4
@receive_message.buffer = internal global [256 x i8] zeroinitializer
define void @receive_message(i32 %0) {
%2 = tail call i32 @recvfrom(i32 %0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @receive_message.buffer, i64 0, i64 0), i32 256, i32 0, %struct.sockaddr* bitcast (%struct.sockaddr_in* @receive_message.source to %struct.sockaddr*), i32* @receive_message.size)
%3 = icmp eq i32 %2, -1
br i1 %3, label %21, label %4
4: ; preds = %1
%5 = load i32, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @receive_message.source, i64 0, i32 0)
%6 = tail call i32 @inet_ntoa(i32 %5)
%7 = tail call i8* @strdup(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %4
%10 = tail call i64 @is_local_address(i8* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = sext i32 %2 to i64
%14 = tail call %struct.message* @parse_message(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @receive_message.buffer, i64 0, i64 0), i64 %13)
%15 = icmp eq %struct.message* %14, null
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @handle_message(i8* %7, %struct.message* %14)
%18 = tail call i32 @message_destroy(%struct.message* %14)
br label %19
19: ; preds = %12, %9, %16
%20 = tail call i32 @free(i8* %7)
br label %21
21: ; preds = %19, %4, %1
ret void
}