#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 */

/* Variables and functions */
scalar_t__ memcmp(char const *, char *, unsigned int const);
unsigned int payload_get_offset_by_name(char *, char const *,
                                        unsigned int const);
unsigned int strlen(char *);
int payload_extract_ent_srv_port(const char **port_fwd,
                                 unsigned int *n_port_fwd, const char *payload,
                                 const unsigned int n_payload) {
  unsigned int offset =
      payload_get_offset_by_name("EntServerMPort0", payload, n_payload);
  const unsigned int n_key_ent_srv_mport = strlen("EntServerMPort0");
  if (memcmp(&payload[offset], "EntServerMPort0", n_key_ent_srv_mport) != 0)
    return 1;
  offset += (n_key_ent_srv_mport + 1);
  unsigned int mport_offset = offset;
  while (offset < n_payload && payload[offset] != 0x1)
    ++offset;
  if (offset == n_payload)
    return 1;
  *port_fwd = &payload[mport_offset];
  *n_port_fwd = offset - mport_offset;
  return 0;
}


x86 assembly (clang, -O0):
<payload_extract_ent_srv_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xeab(%rip),%rdi
call   1050 <payload_get_offset_by_name@plt>
mov    %eax,-0x28(%rbp)
lea    0xe9c(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%eax
add    %rax,%rdi
mov    -0x2c(%rbp),%edx
lea    0xe80(%rip),%rsi
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     119b <payload_extract_ent_srv_port+0x6b>
movl   $0x1,-0x4(%rbp)
jmp    122c <payload_extract_ent_srv_port+0xfc>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x31(%rbp)
jae    11d2 <payload_extract_ent_srv_port+0xa2>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x1,%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11e2 <payload_extract_ent_srv_port+0xb2>
jmp    11f0 <payload_extract_ent_srv_port+0xc0>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ad <payload_extract_ent_srv_port+0x7d>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    1208 <payload_extract_ent_srv_port+0xd8>
movl   $0x1,-0x4(%rbp)
jmp    122c <payload_extract_ent_srv_port+0xfc>
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%eax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%ecx
sub    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<payload_extract_ent_srv_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %rdx,%rbx
mov    %rsi,(%rsp)
mov    %rdi,%r15
lea    0xeb1(%rip),%r14
mov    %r14,%rdi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1050 <payload_get_offset_by_name@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r13d,%edi
add    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <memcmp@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
je     1195 <payload_extract_ent_srv_port+0x65>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1(%r13,%rbp,1),%ecx
mov    %ecx,%edx
cmp    %r12d,%ecx
mov    (%rsp),%rdi
jae    11c1 <payload_extract_ent_srv_port+0x91>
mov    %ecx,%edx
mov    %r12d,%esi
nopw   0x0(%rax,%rax,1)
cmpb   $0x1,(%rbx,%rdx,1)
je     11c1 <payload_extract_ent_srv_port+0x91>
add    $0x1,%rdx
cmp    %rsi,%rdx
jb     11b0 <payload_extract_ent_srv_port+0x80>
jmp    1186 <payload_extract_ent_srv_port+0x56>
cmp    %r12d,%edx
je     1186 <payload_extract_ent_srv_port+0x56>
mov    %ecx,%eax
add    %rax,%rbx
mov    %rbx,(%r15)
sub    %ecx,%edx
mov    %edx,(%rdi)
xor    %eax,%eax
jmp    1186 <payload_extract_ent_srv_port+0x56>

LLVM IR (-O0):
@.str = [16 x i8] c"EntServerMPort0\00"
define i32 @payload_extract_ent_srv_port(i8** %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8** %0, i8*** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i8*, i8** %8
%14 = load i32, i32* %9
%15 = call i32 @payload_get_offset_by_name(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %11
%17 = load i8*, i8** %8
%18 = load i32, i32* %10
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %11
%22 = call i64 @memcmp(i8* %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 1, i32* %5
br label %63
25: ; preds = %4
%26 = load i32, i32* %11
%27 = add i32 %26, 1
%28 = load i32, i32* %10
%29 = add i32 %28, %27
store i32 %29, i32* %10
%30 = load i32, i32* %10
store i32 %30, i32* %12
br label %31
31: ; preds = %45, %25
%32 = load i32, i32* %10
%33 = load i32, i32* %9
%34 = icmp ult i32 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = load i32, i32* %10
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 1
br label %43
43: ; preds = %35, %31
%44 = phi i1 [ false, %31 ], [ %42, %35 ]
br i1 %44, label %45, label %48
45: ; preds = %43
%46 = load i32, i32* %10
%47 = add i32 %46, 1
store i32 %47, i32* %10
br label %31
48: ; preds = %43
%49 = load i32, i32* %10
%50 = load i32, i32* %9
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 1, i32* %5
br label %63
53: ; preds = %48
%54 = load i8*, i8** %8
%55 = load i32, i32* %12
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8**, i8*** %6
store i8* %57, i8** %58
%59 = load i32, i32* %10
%60 = load i32, i32* %12
%61 = sub i32 %59, %60
%62 = load i32*, i32** %7
store i32 %61, i32* %62
store i32 0, i32* %5
br label %63
63: ; preds = %53, %52, %24
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
@.str = [16 x i8] c"EntServerMPort0\00"
define i32 @payload_extract_ent_srv_port(i8** nocapture writeonly %0, i32* nocapture writeonly %1, i8* %2, i32 %3) {
%5 = tail call i32 @payload_get_offset_by_name(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %3)
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = zext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %2, i64 %7
%9 = tail call i64 @memcmp(i8* %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %4
%12 = add i32 %5, 1
%13 = add i32 %12, %6
%14 = icmp ult i32 %13, %3
br i1 %14, label %15, label %28
15: ; preds = %11
%16 = zext i32 %13 to i64
%17 = zext i32 %3 to i64
br label %18
18: ; preds = %15, %23
%19 = phi i64 [ %16, %15 ], [ %24, %23 ]
%20 = getelementptr inbounds i8, i8* %2, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = add nuw nsw i64 %19, 1
%25 = icmp ult i64 %24, %17
br i1 %25, label %18, label %35
26: ; preds = %18
%27 = trunc i64 %19 to i32
br label %28
28: ; preds = %26, %11
%29 = phi i32 [ %13, %11 ], [ %27, %26 ]
%30 = icmp eq i32 %29, %3
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = zext i32 %13 to i64
%33 = getelementptr inbounds i8, i8* %2, i64 %32
store i8* %33, i8** %0
%34 = sub i32 %29, %13
store i32 %34, i32* %1
br label %35
35: ; preds = %23, %31, %28, %4
%36 = phi i32 [ 1, %4 ], [ 0, %31 ], [ 1, %28 ], [ 1, %23 ]
ret i32 %36
}