#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 */
char lastcharofstring(char *);
int strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
char *gophersecuritycheck(char *GophRequest) {
  int x;
  if (strlen(GophRequest) > 512)
    return ("The gopher request is longer than 512 bytes. RFC 1436 states that "
            "the selector shouldn't be longer than 256 bytes.");
  if (strstr(GophRequest, "\t\t") != NULL)
    return ("Client's request contains two TAB characters, one after the "
            "other. It shouldn't ever happen.");
  if (lastcharofstring(GophRequest) == '\t')
    return ("Client's request ends by a TAB character. There's no situation "
            "where that should happen.");
  for (x = 0; GophRequest[x] != 0; x++) {
    if ((GophRequest[x] > 0) && (GophRequest[x] < 32))
      return ("A control char (ASCII 1..31) has been found in the request. "
              "There's no reason for such char to be present there.");
    if ((GophRequest[x] & 128) == 0)
      continue;
    if ((GophRequest[x] & 224) == 192) {
      if ((GophRequest[x + 1] & 192) == 128) {
        x += 1;
        continue;
      }
    } else if ((GophRequest[x] & 240) == 224) {
      if (((GophRequest[x + 1] & 192) == 128) &&
          ((GophRequest[x + 2] & 192) == 128)) {
        x += 2;
        continue;
      }
    }
    return ("Detected an invalid UTF-8 sequence.");
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<gophersecuritycheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x200,%eax
jle    1160 <gophersecuritycheck+0x30>
lea    0xea9(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <gophersecuritycheck+0x1e0>
mov    -0x10(%rbp),%rdi
lea    0xf08(%rip),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     118a <gophersecuritycheck+0x5a>
lea    0xef5(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <gophersecuritycheck+0x1e0>
mov    -0x10(%rbp),%rdi
call   1030 <lastcharofstring@plt>
movsbl %al,%eax
cmp    $0x9,%eax
jne    11af <gophersecuritycheck+0x7f>
lea    0xf2d(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <gophersecuritycheck+0x1e0>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1308 <gophersecuritycheck+0x1d8>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jle    1205 <gophersecuritycheck+0xd5>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jge    1205 <gophersecuritycheck+0xd5>
lea    0xf30(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <gophersecuritycheck+0x1e0>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1224 <gophersecuritycheck+0xf4>
jmp    12fa <gophersecuritycheck+0x1ca>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    1274 <gophersecuritycheck+0x144>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    126f <gophersecuritycheck+0x13f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12fa <gophersecuritycheck+0x1ca>
jmp    12ea <gophersecuritycheck+0x1ba>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    12e5 <gophersecuritycheck+0x1b5>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    12e0 <gophersecuritycheck+0x1b0>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    12e0 <gophersecuritycheck+0x1b0>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    12fa <gophersecuritycheck+0x1ca>
jmp    12e5 <gophersecuritycheck+0x1b5>
jmp    12ea <gophersecuritycheck+0x1ba>
lea    0xeac(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <gophersecuritycheck+0x1e0>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b6 <gophersecuritycheck+0x86>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gophersecuritycheck>:
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x200,%eax
jle    1149 <gophersecuritycheck+0x19>
lea    0xeb9(%rip),%rax
pop    %rbx
ret
lea    0xf23(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
je     1166 <gophersecuritycheck+0x36>
lea    0xf12(%rip),%rax
pop    %rbx
ret
mov    %rbx,%rdi
call   1030 <lastcharofstring@plt>
cmp    $0x9,%al
jne    117b <gophersecuritycheck+0x4b>
lea    0xf5a(%rip),%rax
pop    %rbx
ret
mov    (%rbx),%dil
xor    %eax,%eax
test   %dil,%dil
je     120d <gophersecuritycheck+0xdd>
lea    0xf9c(%rip),%rdx
lea    0x1006(%rip),%r8
xor    %esi,%esi
jmp    11c0 <gophersecuritycheck+0x90>
movslq %esi,%rsi
movzbl 0x1(%rbx,%rsi,1),%ecx
and    $0xc0,%cl
cmp    $0x80,%cl
jne    1214 <gophersecuritycheck+0xe4>
add    $0x1,%rsi
movslq %esi,%rsi
movzbl 0x1(%rbx,%rsi,1),%edi
add    $0x1,%rsi
test   %dil,%dil
je     120d <gophersecuritycheck+0xdd>
lea    -0x1(%rdi),%ecx
cmp    $0x1f,%cl
jb     120f <gophersecuritycheck+0xdf>
test   $0x80,%dil
je     11af <gophersecuritycheck+0x7f>
movzbl %dil,%edi
mov    %edi,%ecx
and    $0xffffffe0,%ecx
cmp    $0xc0,%ecx
je     119b <gophersecuritycheck+0x6b>
and    $0xfffffff0,%edi
cmp    $0xe0,%edi
jne    1214 <gophersecuritycheck+0xe4>
movslq %esi,%rsi
movzbl 0x1(%rsi,%rbx,1),%ecx
and    $0xc0,%cl
cmp    $0x80,%cl
jne    1214 <gophersecuritycheck+0xe4>
movzbl 0x2(%rbx,%rsi,1),%ecx
and    $0xc0,%cl
cmp    $0x80,%cl
jne    1214 <gophersecuritycheck+0xe4>
add    $0x2,%rsi
jmp    11af <gophersecuritycheck+0x7f>
pop    %rbx
ret
mov    %rdx,%rax
pop    %rbx
ret
mov    %r8,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [115 x i8] c"The gopher request is longer than 512 bytes. RFC 1436 states that the selector shouldn't be longer than 256 bytes.\00"
@.str.1 = [3 x i8] c"\09\09\00"
@.str.2 = [93 x i8] c"Client's request contains two TAB characters, one after the other. It shouldn't ever happen.\00"
@.str.3 = [89 x i8] c"Client's request ends by a TAB character. There's no situation where that should happen.\00"
@.str.4 = [113 x i8] c"A control char (ASCII 1..31) has been found in the request. There's no reason for such char to be present there.\00"
@.str.5 = [36 x i8] c"Detected an invalid UTF-8 sequence.\00"
define i8* @gophersecuritycheck(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @strlen(i8* %5)
%7 = icmp sgt i32 %6, 512
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* getelementptr inbounds ([115 x i8], [115 x i8]* @.str, i64 0, i64 0), i8** %2
br label %118
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32* @strstr(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %9
store i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %118
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call signext i8 @lastcharofstring(i8* %15)
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 9
br i1 %18, label %19, label %20
19: ; preds = %14
store i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %118
20: ; preds = %14
store i32 0, i32* %4
br label %21
21: ; preds = %114, %20
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %117
29: ; preds = %21
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %29
%38 = load i8*, i8** %3
%39 = load i32, i32* %4
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp slt i32 %43, 32
br i1 %44, label %45, label %46
45: ; preds = %37
store i8* getelementptr inbounds ([113 x i8], [113 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %118
46: ; preds = %37, %29
%47 = load i8*, i8** %3
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = and i32 %52, 128
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %46
br label %114
56: ; preds = %46
%57 = load i8*, i8** %3
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = and i32 %62, 224
%64 = icmp eq i32 %63, 192
br i1 %64, label %65, label %79
65: ; preds = %56
%66 = load i8*, i8** %3
%67 = load i32, i32* %4
%68 = add nsw i32 %67, 1
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %66, i64 %69
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = and i32 %72, 192
%74 = icmp eq i32 %73, 128
br i1 %74, label %75, label %78
75: ; preds = %65
%76 = load i32, i32* %4
%77 = add nsw i32 %76, 1
store i32 %77, i32* %4
br label %114
78: ; preds = %65
br label %113
79: ; preds = %56
%80 = load i8*, i8** %3
%81 = load i32, i32* %4
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
%86 = and i32 %85, 240
%87 = icmp eq i32 %86, 224
br i1 %87, label %88, label %112
88: ; preds = %79
%89 = load i8*, i8** %3
%90 = load i32, i32* %4
%91 = add nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %89, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = and i32 %95, 192
%97 = icmp eq i32 %96, 128
br i1 %97, label %98, label %111
98: ; preds = %88
%99 = load i8*, i8** %3
%100 = load i32, i32* %4
%101 = add nsw i32 %100, 2
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %99, i64 %102
%104 = load i8, i8* %103
%105 = sext i8 %104 to i32
%106 = and i32 %105, 192
%107 = icmp eq i32 %106, 128
br i1 %107, label %108, label %111
108: ; preds = %98
%109 = load i32, i32* %4
%110 = add nsw i32 %109, 2
store i32 %110, i32* %4
br label %114
111: ; preds = %98, %88
br label %112
112: ; preds = %111, %79
br label %113
113: ; preds = %112, %78
store i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8** %2
br label %118
114: ; preds = %108, %75, %55
%115 = load i32, i32* %4
%116 = add nsw i32 %115, 1
store i32 %116, i32* %4
br label %21
117: ; preds = %21
store i8* null, i8** %2
br label %118
118: ; preds = %117, %113, %45, %19, %13, %8
%119 = load i8*, i8** %2
ret i8* %119
}

LLVM IR (-O2):
@.str = [115 x i8] c"The gopher request is longer than 512 bytes. RFC 1436 states that the selector shouldn't be longer than 256 bytes.\00"
@.str.1 = [3 x i8] c"\09\09\00"
@.str.2 = [93 x i8] c"Client's request contains two TAB characters, one after the other. It shouldn't ever happen.\00"
@.str.3 = [89 x i8] c"Client's request ends by a TAB character. There's no situation where that should happen.\00"
@.str.4 = [113 x i8] c"A control char (ASCII 1..31) has been found in the request. There's no reason for such char to be present there.\00"
@.str.5 = [36 x i8] c"Detected an invalid UTF-8 sequence.\00"
define i8* @gophersecuritycheck(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 512
br i1 %3, label %56, label %4
4: ; preds = %1
%5 = tail call i32* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %56
7: ; preds = %4
%8 = tail call signext i8 @lastcharofstring(i8* %0)
%9 = icmp eq i8 %8, 9
br i1 %9, label %56, label %10
10: ; preds = %7
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %56, label %13
13: ; preds = %10, %49
%14 = phi i8 [ %54, %49 ], [ %11, %10 ]
%15 = phi i32 [ %51, %49 ], [ 0, %10 ]
%16 = add i8 %14, -1
%17 = icmp ult i8 %16, 31
br i1 %17, label %56, label %18
18: ; preds = %13
%19 = zext i8 %14 to i32
%20 = and i32 %19, 128
%21 = icmp eq i32 %20, 0
br i1 %21, label %49, label %22
22: ; preds = %18
%23 = and i32 %19, 224
%24 = icmp eq i32 %23, 192
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = add nsw i32 %15, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i8, i8* %28
%30 = and i8 %29, -64
%31 = icmp eq i8 %30, -128
br i1 %31, label %49, label %56
32: ; preds = %22
%33 = and i32 %19, 240
%34 = icmp eq i32 %33, 224
br i1 %34, label %35, label %56
35: ; preds = %32
%36 = add nsw i32 %15, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %0, i64 %37
%39 = load i8, i8* %38
%40 = and i8 %39, -64
%41 = icmp eq i8 %40, -128
br i1 %41, label %42, label %56
42: ; preds = %35
%43 = add nsw i32 %15, 2
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %0, i64 %44
%46 = load i8, i8* %45
%47 = and i8 %46, -64
%48 = icmp eq i8 %47, -128
br i1 %48, label %49, label %56
49: ; preds = %42, %25, %18
%50 = phi i32 [ %15, %18 ], [ %26, %25 ], [ %43, %42 ]
%51 = add nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %0, i64 %52
%54 = load i8, i8* %53
%55 = icmp eq i8 %54, 0
br i1 %55, label %56, label %13
56: ; preds = %13, %32, %42, %35, %25, %49, %10, %7, %4, %1
%57 = phi i8* [ getelementptr inbounds ([115 x i8], [115 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([93 x i8], [93 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([89 x i8], [89 x i8]* @.str.3, i64 0, i64 0), %7 ], [ null, %10 ], [ getelementptr inbounds ([113 x i8], [113 x i8]* @.str.4, i64 0, i64 0), %13 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), %32 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), %42 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), %35 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), %25 ], [ null, %49 ]
ret i8* %57
}