#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 */
int Strlen(char *);
int /*<<< orphan*/ Strncmp(char *, char *, int);
int /*<<< orphan*/ protected_mode_access(int, int, unsigned long, char *);
int FindStringInSeg(unsigned int segment_address, char *string_ptr) {
  int string_length = Strlen(string_ptr);
  char *psegment_buf;
  unsigned long mem_ptr = (unsigned long)segment_address << 16;
  char segment_buffer[0xFFFF + 1];
  int i, cursor;
  (void)cursor;
  (void)mem_ptr;
  (void)segment_buffer;
  for (cursor = 0; (cursor * 0x1000) < 0xFFFF; cursor++) {
    protected_mode_access(3, 0x1000, mem_ptr + (cursor * 0x1000),
                          &(segment_buffer[(cursor * 0x1000)]));
  }
  psegment_buf = segment_buffer;
  for (i = 0; i < 0xFFFF + 1; i++) {
    if (*(psegment_buf + i) == *string_ptr) {
      if (!Strncmp(string_ptr, (psegment_buf + i), string_length)) {
        return (1);
      }
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<FindStringInSeg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <Strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x10,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x10038(%rbp)
mov    -0x10038(%rbp),%eax
shl    $0xc,%eax
cmp    $0xffff,%eax
jge    11c1 <FindStringInSeg+0x91>
mov    -0x28(%rbp),%rdx
mov    -0x10038(%rbp),%eax
shl    $0xc,%eax
cltq
add    %rax,%rdx
mov    -0x10038(%rbp),%eax
shl    $0xc,%eax
cltq
lea    -0x10030(%rbp),%rcx
add    %rax,%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
mov    -0x10038(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10038(%rbp)
jmp    1163 <FindStringInSeg+0x33>
lea    -0x10030(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x10034(%rbp)
cmpl   $0x10000,-0x10034(%rbp)
jge    1251 <FindStringInSeg+0x121>
mov    -0x20(%rbp),%rax
movslq -0x10034(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1238 <FindStringInSeg+0x108>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x10034(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1040 <Strncmp@plt>
cmp    $0x0,%eax
jne    1233 <FindStringInSeg+0x103>
movl   $0x1,-0x4(%rbp)
jmp    1258 <FindStringInSeg+0x128>
jmp    1238 <FindStringInSeg+0x108>
jmp    123d <FindStringInSeg+0x10d>
mov    -0x10034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10034(%rbp)
jmp    11d6 <FindStringInSeg+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FindStringInSeg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10000,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1050 <Strlen@plt>
mov    %eax,%r14d
mov    %ebp,%ebp
shl    $0x10,%rbp
mov    %rsp,%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
mov    %rbp,%rdx
call   1030 <protected_mode_access@plt>
lea    0x1000(%rbp),%rdx
lea    0x1000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x2000(%rbp),%rdx
lea    0x2000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x3000(%rbp),%rdx
lea    0x3000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x4000(%rbp),%rdx
lea    0x4000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x5000(%rbp),%rdx
lea    0x5000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x6000(%rbp),%rdx
lea    0x6000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x7000(%rbp),%rdx
lea    0x7000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x8000(%rbp),%rdx
lea    0x8000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0x9000(%rbp),%rdx
lea    0x9000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0xa000(%rbp),%rdx
lea    0xa000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0xb000(%rbp),%rdx
lea    0xb000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0xc000(%rbp),%rdx
lea    0xc000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0xd000(%rbp),%rdx
lea    0xd000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
lea    0xe000(%rbp),%rdx
lea    0xe000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
call   1030 <protected_mode_access@plt>
or     $0xf000,%rbp
lea    0xf000(%rsp),%rcx
mov    $0x3,%edi
mov    $0x1000,%esi
mov    %rbp,%rdx
call   1030 <protected_mode_access@plt>
xor    %ebp,%ebp
jmp    133d <FindStringInSeg+0x20d>
nop
add    $0x1,%rbp
cmp    $0x10000,%rbp
je     135f <FindStringInSeg+0x22f>
movzbl (%rsp,%rbp,1),%eax
cmp    (%rbx),%al
jne    1330 <FindStringInSeg+0x200>
lea    (%rsp,%rbp,1),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1040 <Strncmp@plt>
test   %eax,%eax
jne    1330 <FindStringInSeg+0x200>
mov    $0x1,%eax
jmp    1361 <FindStringInSeg+0x231>
xor    %eax,%eax
add    $0x10000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @FindStringInSeg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [65536 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%12 = load i8*, i8** %5
%13 = call i32 @Strlen(i8* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %4
%15 = zext i32 %14 to i64
%16 = shl i64 %15, 16
store i64 %16, i64* %8
%17 = load i32, i32* %11
%18 = load i64, i64* %8
%19 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
store i32 0, i32* %11
br label %20
20: ; preds = %35, %2
%21 = load i32, i32* %11
%22 = mul nsw i32 %21, 4096
%23 = icmp slt i32 %22, 65535
br i1 %23, label %24, label %38
24: ; preds = %20
%25 = load i64, i64* %8
%26 = load i32, i32* %11
%27 = mul nsw i32 %26, 4096
%28 = sext i32 %27 to i64
%29 = add i64 %25, %28
%30 = load i32, i32* %11
%31 = mul nsw i32 %30, 4096
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 %32
%34 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %29, i8* %33)
br label %35
35: ; preds = %24
%36 = load i32, i32* %11
%37 = add nsw i32 %36, 1
store i32 %37, i32* %11
br label %20
38: ; preds = %20
%39 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
store i8* %39, i8** %7
store i32 0, i32* %10
br label %40
40: ; preds = %66, %38
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 65536
br i1 %42, label %43, label %69
43: ; preds = %40
%44 = load i8*, i8** %7
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = load i8*, i8** %5
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %49, %52
br i1 %53, label %54, label %65
54: ; preds = %43
%55 = load i8*, i8** %5
%56 = load i8*, i8** %7
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i32, i32* %6
%61 = call i32 @Strncmp(i8* %55, i8* %59, i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %64, label %63
63: ; preds = %54
store i32 1, i32* %3
br label %70
64: ; preds = %54
br label %65
65: ; preds = %64, %43
br label %66
66: ; preds = %65
%67 = load i32, i32* %10
%68 = add nsw i32 %67, 1
store i32 %68, i32* %10
br label %40
69: ; preds = %40
store i32 0, i32* %3
br label %70
70: ; preds = %69, %63
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
define i32 @FindStringInSeg(i32 %0, i8* %1) {
%3 = alloca [65536 x i8]
%4 = tail call i32 @Strlen(i8* %1)
%5 = zext i32 %0 to i64
%6 = shl nuw nsw i64 %5, 16
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%8 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %6, i8* %7)
%9 = or i64 %6, 4096
%10 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 4096
%11 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %9, i8* %10)
%12 = or i64 %6, 8192
%13 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 8192
%14 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %12, i8* %13)
%15 = or i64 %6, 12288
%16 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 12288
%17 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %15, i8* %16)
%18 = or i64 %6, 16384
%19 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 16384
%20 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %18, i8* %19)
%21 = or i64 %6, 20480
%22 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 20480
%23 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %21, i8* %22)
%24 = or i64 %6, 24576
%25 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 24576
%26 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %24, i8* %25)
%27 = or i64 %6, 28672
%28 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 28672
%29 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %27, i8* %28)
%30 = or i64 %6, 32768
%31 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 32768
%32 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %30, i8* %31)
%33 = or i64 %6, 36864
%34 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 36864
%35 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %33, i8* %34)
%36 = or i64 %6, 40960
%37 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 40960
%38 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %36, i8* %37)
%39 = or i64 %6, 45056
%40 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 45056
%41 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %39, i8* %40)
%42 = or i64 %6, 49152
%43 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 49152
%44 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %42, i8* %43)
%45 = or i64 %6, 53248
%46 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 53248
%47 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %45, i8* %46)
%48 = or i64 %6, 57344
%49 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 57344
%50 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %48, i8* %49)
%51 = or i64 %6, 61440
%52 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 61440
%53 = call i32 @protected_mode_access(i32 3, i32 4096, i64 %51, i8* %52)
br label %54
54: ; preds = %2, %63
%55 = phi i64 [ 0, %2 ], [ %64, %63 ]
%56 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = load i8, i8* %1
%59 = icmp eq i8 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %54
%61 = call i32 @Strncmp(i8* %1, i8* %56, i32 %4)
%62 = icmp eq i32 %61, 0
br i1 %62, label %66, label %63
63: ; preds = %54, %60
%64 = add nuw nsw i64 %55, 1
%65 = icmp eq i64 %64, 65536
br i1 %65, label %66, label %54
66: ; preds = %63, %60
%67 = phi i32 [ 1, %60 ], [ 0, %63 ]
ret i32 %67
}