#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int uint32_t;
typedef int uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int atoi(char *);
int htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, struct sockaddr_in *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  if (argc != 6) {
    printf("%s [src_ip] [dst_ip] [src_port] [dst_port] [div]\n", argv[0]);
    return EXIT_FAILURE;
  }
  struct sockaddr_in sain_src;
  memset(&sain_src, 0, sizeof(struct sockaddr_in));
  struct sockaddr_in sain_dst;
  memset(&sain_dst, 0, sizeof(struct sockaddr_in));
  inet_pton(AF_INET, argv[1], &sain_src);
  inet_pton(AF_INET, argv[2], &sain_dst);
  uint32_t srcip = (uint32_t)sain_src.sin_addr.s_addr;
  uint32_t dstip = (uint32_t)sain_dst.sin_addr.s_addr;
  uint16_t srcport = htons(atoi(argv[3]));
  uint16_t dstport = htons(atoi(argv[4]));
  int div = atoi(argv[5]);
  uint32_t sum = srcip ^ dstip;
  if (srcport <= dstport) {
    sum = srcport << 16 | dstport;
  } else {
    sum = dstport << 16 | srcport;
  }
  printf("%d\n", sum % div);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x6,-0x8(%rbp)
je     1196 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1284 <main+0x134>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <inet_pton@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <inet_pton@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
xor    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jg     125a <main+0x10a>
mov    -0x2c(%rbp),%eax
shl    $0x10,%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1266 <main+0x116>
mov    -0x30(%rbp),%eax
shl    $0x10,%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cltd
idivl  -0x34(%rbp)
mov    %edx,%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x6,%edi
jne    1211 <main+0xc1>
lea    0x8(%rsp),%r14
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e50(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   1060 <inet_pton@plt>
mov    0x0(%rbp),%edi
mov    0x10(%rbx),%rsi
mov    %r15,%rdx
call   1060 <inet_pton@plt>
mov    0x18(%rbx),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,%r14d
mov    0x20(%rbx),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,%ebp
mov    0x28(%rbx),%rdi
call   1070 <atoi@plt>
mov    %eax,%ecx
mov    %r14d,%edx
shl    $0x10,%edx
or     %ebp,%edx
mov    %ebp,%eax
shl    $0x10,%eax
or     %r14d,%eax
cmp    %ebp,%r14d
cmovle %edx,%eax
cltd
idiv   %ecx
lea    0xe2c(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    122c <main+0xdc>
mov    (%rbx),%rsi
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [50 x i8] c"%s [src_ip] [dst_ip] [src_port] [dst_port] [div]\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 6
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
store i32 %21, i32* %3
br label %76
22: ; preds = %2
%23 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 4)
%24 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 4)
%25 = load i32, i32* @AF_INET
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @inet_pton(i32 %25, i8* %28, %struct.sockaddr_in* %6)
%30 = load i32, i32* @AF_INET
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @inet_pton(i32 %30, i8* %33, %struct.sockaddr_in* %7)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load i32, i32* %36
store i32 %37, i32* %8
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %9
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 3
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
%45 = call i32 @htons(i32 %44)
store i32 %45, i32* %10
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 4
%48 = load i8*, i8** %47
%49 = call i32 @atoi(i8* %48)
%50 = call i32 @htons(i32 %49)
store i32 %50, i32* %11
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 5
%53 = load i8*, i8** %52
%54 = call i32 @atoi(i8* %53)
store i32 %54, i32* %12
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = xor i32 %55, %56
store i32 %57, i32* %13
%58 = load i32, i32* %10
%59 = load i32, i32* %11
%60 = icmp sle i32 %58, %59
br i1 %60, label %61, label %66
61: ; preds = %22
%62 = load i32, i32* %10
%63 = shl i32 %62, 16
%64 = load i32, i32* %11
%65 = or i32 %63, %64
store i32 %65, i32* %13
br label %71
66: ; preds = %22
%67 = load i32, i32* %11
%68 = shl i32 %67, 16
%69 = load i32, i32* %10
%70 = or i32 %68, %69
store i32 %70, i32* %13
br label %71
71: ; preds = %66, %61
%72 = load i32, i32* %13
%73 = load i32, i32* %12
%74 = srem i32 %72, %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %74)
store i32 0, i32* %3
br label %76
76: ; preds = %71, %16
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [50 x i8] c"%s [src_ip] [dst_ip] [src_port] [dst_port] [div]\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = icmp eq i32 %0, 6
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
br label %42
10: ; preds = %2
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 4)
%13 = bitcast %struct.sockaddr_in* %4 to i8*
%14 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 4)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @inet_pton(i32 %15, i8* %17, %struct.sockaddr_in* %3)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @inet_pton(i32 %19, i8* %21, %struct.sockaddr_in* %4)
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds i8*, i8** %1, i64 4
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds i8*, i8** %1, i64 5
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = icmp sgt i32 %26, %30
%35 = shl i32 %26, 16
%36 = or i32 %30, %35
%37 = shl i32 %30, 16
%38 = or i32 %37, %26
%39 = select i1 %34, i32 %38, i32 %36
%40 = srem i32 %39, %33
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %10, %6
%43 = phi i32 [ %9, %6 ], [ 0, %10 ]
ret i32 %43
}