#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 uint8_t;
typedef int uint16_t;

/* Variables and functions */
int memcmp(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ print_addr_dec(int *);
int /*<<< orphan*/ printf(char *, ...);
int tftp_negotiate(int *, int, int *, int, int *, int);
int tftp_transfer(int *, int, int *, int, int *, int);
int tftp_transfer_src_port();
int /*<<< orphan*/ udp_get_broadcast_ip(int /*<<< orphan*/ *);
int /*<<< orphan*/ udp_get_localhost_ip(int /*<<< orphan*/ *);
uint8_t udp_packet_demultiplexer(uint8_t *src, uint16_t src_port, uint8_t *dst,
                                 uint16_t dst_port, uint8_t *payload,
                                 uint16_t len) {
  uint8_t retVal;
  uint8_t different = 0;
  different = memcmp(udp_get_localhost_ip(NULL), dst, 4);
  if (different) {
    different = memcmp(udp_get_broadcast_ip(NULL), dst, 4);
  }
  if (!different) {
    if (dst_port == 69) {
      retVal = tftp_negotiate(src, src_port, dst, dst_port, payload, len - 8);
      if (retVal)
        printf("tftp_negotiate(), error=%d\n", retVal);
    } else if (dst_port == tftp_transfer_src_port()) {
      retVal = tftp_transfer(src, src_port, dst, dst_port, payload, len - 8);
      if (retVal)
        printf("tftp_transfer(), error=%d\n", retVal);
    } else if (dst_port == 12345) {
      printf("New neighbour:\nIP = %d.%d.%d.%d\n", src[0], src[1], src[2],
             src[3]);
    } else {
      printf("packet received for unassigned port: %d from ", dst_port);
      print_addr_dec(src);
    }
  } else {
    printf("packet for someone else: ");
    print_addr_dec(dst);
    printf("port: %d\n", dst_port);
  }
  return 0;
}


x86 assembly (clang, -O0):
<udp_packet_demultiplexer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x34(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <udp_get_localhost_ip@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e7 <udp_packet_demultiplexer+0x67>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <udp_get_broadcast_ip@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    12fe <udp_packet_demultiplexer+0x17e>
cmpl   $0x45,-0x1c(%rbp)
jne    123d <udp_packet_demultiplexer+0xbd>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
sub    $0x8,%r9d
call   1080 <tftp_negotiate@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1238 <udp_packet_demultiplexer+0xb8>
mov    -0x30(%rbp),%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f9 <udp_packet_demultiplexer+0x179>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <tftp_transfer_src_port@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jne    1299 <udp_packet_demultiplexer+0x119>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
sub    $0x8,%r9d
call   1090 <tftp_transfer@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1294 <udp_packet_demultiplexer+0x114>
mov    -0x30(%rbp),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f4 <udp_packet_demultiplexer+0x174>
cmpl   $0x3039,-0x1c(%rbp)
jne    12d5 <udp_packet_demultiplexer+0x155>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%r8d
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ef <udp_packet_demultiplexer+0x16f>
mov    -0x1c(%rbp),%esi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1070 <print_addr_dec@plt>
jmp    12f4 <udp_packet_demultiplexer+0x174>
jmp    12f9 <udp_packet_demultiplexer+0x179>
jmp    1326 <udp_packet_demultiplexer+0x1a6>
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <print_addr_dec@plt>
mov    -0x1c(%rbp),%esi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_packet_demultiplexer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r15d
mov    %r8,%r12
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%r13d
mov    %rdi,%r14
xor    %edi,%edi
call   10a0 <udp_get_localhost_ip@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %eax,%eax
je     11f1 <udp_packet_demultiplexer+0x71>
xor    %edi,%edi
call   1050 <udp_get_broadcast_ip@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %eax,%eax
je     11f1 <udp_packet_demultiplexer+0x71>
lea    0xeaf(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <print_addr_dec@plt>
lea    0xeb3(%rip),%rdi
mov    %ebp,%esi
jmp    1252 <udp_packet_demultiplexer+0xd2>
cmp    $0x45,%ebp
jne    1220 <udp_packet_demultiplexer+0xa0>
add    $0xfffffff8,%r15d
mov    %r14,%rdi
mov    %r13d,%esi
mov    %rbx,%rdx
mov    $0x45,%ecx
mov    %r12,%r8
mov    %r15d,%r9d
call   1080 <tftp_negotiate@plt>
test   %eax,%eax
je     1259 <udp_packet_demultiplexer+0xd9>
lea    0xde2(%rip),%rdi
jmp    1250 <udp_packet_demultiplexer+0xd0>
xor    %eax,%eax
call   1030 <tftp_transfer_src_port@plt>
cmp    %ebp,%eax
jne    126a <udp_packet_demultiplexer+0xea>
add    $0xfffffff8,%r15d
mov    %r14,%rdi
mov    %r13d,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %r12,%r8
mov    %r15d,%r9d
call   1090 <tftp_transfer@plt>
test   %eax,%eax
je     1259 <udp_packet_demultiplexer+0xd9>
lea    0xdcc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x3039,%ebp
jne    1291 <udp_packet_demultiplexer+0x111>
mov    (%r14),%esi
mov    0x4(%r14),%edx
mov    0x8(%r14),%ecx
mov    0xc(%r14),%r8d
lea    0xdaf(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1259 <udp_packet_demultiplexer+0xd9>
lea    0xdc0(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   1070 <print_addr_dec@plt>
jmp    1259 <udp_packet_demultiplexer+0xd9>

LLVM IR (-O0):
@.str = [28 x i8] c"tftp_negotiate(), error=%d\0A\00"
@.str.1 = [27 x i8] c"tftp_transfer(), error=%d\0A\00"
@.str.2 = [33 x i8] c"New neighbour:\0AIP = %d.%d.%d.%d\0A\00"
@.str.3 = [46 x i8] c"packet received for unassigned port: %d from \00"
@.str.4 = [26 x i8] c"packet for someone else: \00"
@.str.5 = [10 x i8] c"port: %d\0A\00"
define i32 @udp_packet_demultiplexer(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4, i32 %5) {
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
store i32 %5, i32* %12
store i32 0, i32* %14
%15 = call i32 @udp_get_localhost_ip(i32* null)
%16 = load i32*, i32** %9
%17 = call i32 @memcmp(i32 %15, i32* %16, i32 4)
store i32 %17, i32* %14
%18 = load i32, i32* %14
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %6
%21 = call i32 @udp_get_broadcast_ip(i32* null)
%22 = load i32*, i32** %9
%23 = call i32 @memcmp(i32 %21, i32* %22, i32 4)
store i32 %23, i32* %14
br label %24
24: ; preds = %20, %6
%25 = load i32, i32* %14
%26 = icmp ne i32 %25, 0
br i1 %26, label %89, label %27
27: ; preds = %24
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, 69
br i1 %29, label %30, label %45
30: ; preds = %27
%31 = load i32*, i32** %7
%32 = load i32, i32* %8
%33 = load i32*, i32** %9
%34 = load i32, i32* %10
%35 = load i32*, i32** %11
%36 = load i32, i32* %12
%37 = sub nsw i32 %36, 8
%38 = call i32 @tftp_negotiate(i32* %31, i32 %32, i32* %33, i32 %34, i32* %35, i32 %37)
store i32 %38, i32* %13
%39 = load i32, i32* %13
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i32, i32* %13
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %30
br label %88
45: ; preds = %27
%46 = load i32, i32* %10
%47 = call i32 (...) @tftp_transfer_src_port()
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %64
49: ; preds = %45
%50 = load i32*, i32** %7
%51 = load i32, i32* %8
%52 = load i32*, i32** %9
%53 = load i32, i32* %10
%54 = load i32*, i32** %11
%55 = load i32, i32* %12
%56 = sub nsw i32 %55, 8
%57 = call i32 @tftp_transfer(i32* %50, i32 %51, i32* %52, i32 %53, i32* %54, i32 %56)
store i32 %57, i32* %13
%58 = load i32, i32* %13
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %49
%61 = load i32, i32* %13
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %60, %49
br label %87
64: ; preds = %45
%65 = load i32, i32* %10
%66 = icmp eq i32 %65, 12345
br i1 %66, label %67, label %81
67: ; preds = %64
%68 = load i32*, i32** %7
%69 = getelementptr inbounds i32, i32* %68, i64 0
%70 = load i32, i32* %69
%71 = load i32*, i32** %7
%72 = getelementptr inbounds i32, i32* %71, i64 1
%73 = load i32, i32* %72
%74 = load i32*, i32** %7
%75 = getelementptr inbounds i32, i32* %74, i64 2
%76 = load i32, i32* %75
%77 = load i32*, i32** %7
%78 = getelementptr inbounds i32, i32* %77, i64 3
%79 = load i32, i32* %78
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %70, i32 %73, i32 %76, i32 %79)
br label %86
81: ; preds = %64
%82 = load i32, i32* %10
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %82)
%84 = load i32*, i32** %7
%85 = call i32 @print_addr_dec(i32* %84)
br label %86
86: ; preds = %81, %67
br label %87
87: ; preds = %86, %63
br label %88
88: ; preds = %87, %44
br label %95
89: ; preds = %24
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%91 = load i32*, i32** %9
%92 = call i32 @print_addr_dec(i32* %91)
%93 = load i32, i32* %10
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %93)
br label %95
95: ; preds = %89, %88
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"tftp_negotiate(), error=%d\0A\00"
@.str.1 = [27 x i8] c"tftp_transfer(), error=%d\0A\00"
@.str.2 = [33 x i8] c"New neighbour:\0AIP = %d.%d.%d.%d\0A\00"
@.str.3 = [46 x i8] c"packet received for unassigned port: %d from \00"
@.str.4 = [26 x i8] c"packet for someone else: \00"
@.str.5 = [10 x i8] c"port: %d\0A\00"
define i32 @udp_packet_demultiplexer(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4, i32 %5) {
%7 = tail call i32 @udp_get_localhost_ip(i32* null)
%8 = tail call i32 @memcmp(i32 %7, i32* %2, i32 4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = tail call i32 @udp_get_broadcast_ip(i32* null)
%12 = tail call i32 @memcmp(i32 %11, i32* %2, i32 4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %6, %10
%15 = icmp eq i32 %3, 69
br i1 %15, label %16, label %22
16: ; preds = %14
%17 = add nsw i32 %5, -8
%18 = tail call i32 @tftp_negotiate(i32* %0, i32 %1, i32* %2, i32 69, i32* %4, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %49, label %20
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %49
22: ; preds = %14
%23 = tail call i32 (...) @tftp_transfer_src_port()
%24 = icmp eq i32 %23, %3
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = add nsw i32 %5, -8
%27 = tail call i32 @tftp_transfer(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %49, label %29
29: ; preds = %25
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %49
31: ; preds = %22
%32 = icmp eq i32 %3, 12345
br i1 %32, label %33, label %42
33: ; preds = %31
%34 = load i32, i32* %0
%35 = getelementptr inbounds i32, i32* %0, i64 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds i32, i32* %0, i64 2
%38 = load i32, i32* %37
%39 = getelementptr inbounds i32, i32* %0, i64 3
%40 = load i32, i32* %39
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %36, i32 %38, i32 %40)
br label %49
42: ; preds = %31
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %3)
%44 = tail call i32 @print_addr_dec(i32* %0)
br label %49
45: ; preds = %10
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%47 = tail call i32 @print_addr_dec(i32* %2)
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %3)
br label %49
49: ; preds = %20, %16, %33, %42, %25, %29, %45
ret i32 0
}