#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__ strncmp(char *, char *, int);
int search_next_message(unsigned char *temp,
                        int (*read_func)(void *buf, unsigned int len)) {
  int n;
  if (strncmp((char *)temp, "GRIB", 4) != 0) {
    while (temp[0] != 0x47 || temp[1] != 0x52 || temp[2] != 0x49 ||
           temp[3] != 0x42) {
      switch (temp[1]) {
      case 0x47:
        for (n = 0; n < 3; n++) {
          temp[n] = temp[n + 1];
        }
        if (read_func(&temp[3], 1) == 0) {
          return -1;
        }
        break;
      default:
        switch (temp[2]) {
        case 0x47:
          for (n = 0; n < 2; n++) {
            temp[n] = temp[n + 2];
          }
          if (read_func(&temp[2], 2) == 0) {
            return -1;
          }
          break;
        default:
          switch (temp[3]) {
          case 0x47:
            temp[0] = temp[3];
            if (read_func(&temp[1], 3) == 0) {
              return -1;
            }
            break;
          default:
            if (read_func(temp, 4) == 0) {
              return -1;
            }
            break;
          }
          break;
        }
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<search_next_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xed5(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1327 <search_next_message+0x217>
jmp    1144 <search_next_message+0x34>
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x47,%ecx
mov    %al,-0x1d(%rbp)
jne    1196 <search_next_message+0x86>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    $0x1,%al
cmp    $0x52,%ecx
mov    %al,-0x1d(%rbp)
jne    1196 <search_next_message+0x86>
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%ecx
mov    $0x1,%al
cmp    $0x49,%ecx
mov    %al,-0x1d(%rbp)
jne    1196 <search_next_message+0x86>
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%eax
cmp    $0x42,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11a6 <search_next_message+0x96>
jmp    1322 <search_next_message+0x212>
mov    -0x10(%rbp),%rax
mov    0x1(%rax),%al
sub    $0x47,%al
jne    1221 <search_next_message+0x111>
jmp    11ba <search_next_message+0xaa>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    11f4 <search_next_message+0xe4>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c1 <search_next_message+0xb1>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
add    $0x3,%rdi
mov    $0x1,%esi
call   *%rax
cmp    $0x0,%eax
jne    121c <search_next_message+0x10c>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <search_next_message+0x21e>
jmp    131d <search_next_message+0x20d>
mov    -0x10(%rbp),%rax
mov    0x2(%rax),%al
sub    $0x47,%al
jne    129c <search_next_message+0x18c>
jmp    1235 <search_next_message+0x125>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    126f <search_next_message+0x15f>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    123c <search_next_message+0x12c>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
mov    $0x2,%esi
call   *%rax
cmp    $0x0,%eax
jne    1297 <search_next_message+0x187>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <search_next_message+0x21e>
jmp    1318 <search_next_message+0x208>
mov    -0x10(%rbp),%rax
mov    0x3(%rax),%al
sub    $0x47,%al
jne    12ea <search_next_message+0x1da>
jmp    12b0 <search_next_message+0x1a0>
mov    -0x10(%rbp),%rax
mov    0x3(%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x3,%esi
call   *%rax
cmp    $0x0,%eax
jne    12e5 <search_next_message+0x1d5>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <search_next_message+0x21e>
jmp    1313 <search_next_message+0x203>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   *%rax
cmp    $0x0,%eax
jne    130e <search_next_message+0x1fe>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <search_next_message+0x21e>
jmp    1313 <search_next_message+0x203>
jmp    1318 <search_next_message+0x208>
jmp    131d <search_next_message+0x20d>
jmp    1144 <search_next_message+0x34>
jmp    1327 <search_next_message+0x217>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_next_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0xed8(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11df <search_next_message+0xcf>
lea    0x3(%rbp),%r15
lea    0x1(%rbp),%r12
lea    0x2(%rbp),%rbx
jmp    1167 <search_next_message+0x57>
nopl   0x0(%rax)
movzwl 0x2(%rbp),%eax
mov    %ax,0x0(%rbp)
mov    %rbx,%rdi
mov    $0x2,%esi
call   *%r13
test   %eax,%eax
je     11d9 <search_next_message+0xc9>
movzbl 0x1(%rbp),%eax
cmpb   $0x47,0x0(%rbp)
jne    11a0 <search_next_message+0x90>
cmp    $0x52,%al
jne    11a0 <search_next_message+0x90>
movzbl (%rbx),%eax
cmp    $0x49,%al
jne    11c3 <search_next_message+0xb3>
movzbl (%r15),%eax
cmp    $0x42,%al
je     11df <search_next_message+0xcf>
cmp    $0x47,%al
jne    11cf <search_next_message+0xbf>
movb   $0x47,0x0(%rbp)
mov    %r12,%rdi
mov    $0x3,%esi
jmp    1160 <search_next_message+0x50>
cs nopw 0x0(%rax,%rax,1)
cmp    $0x47,%al
jne    11c0 <search_next_message+0xb0>
movzwl (%r12),%eax
movzbl 0x2(%r12),%ecx
mov    %cl,0x2(%rbp)
mov    %ax,0x0(%rbp)
mov    %r15,%rdi
mov    $0x1,%esi
jmp    1160 <search_next_message+0x50>
movzbl (%rbx),%eax
cmp    $0x47,%al
je     1150 <search_next_message+0x40>
movzbl (%r15),%eax
cmp    $0x47,%al
je     1188 <search_next_message+0x78>
mov    %rbp,%rdi
mov    $0x4,%esi
jmp    1160 <search_next_message+0x50>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"GRIB\00"
define i32 @search_next_message(i8* %0, i32 (i8*, i32)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32 (i8*, i32)*
%6 = alloca i32
store i8* %0, i8** %4
store i32 (i8*, i32)* %1, i32 (i8*, i32)** %5
%7 = load i8*, i8** %4
%8 = call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %128
10: ; preds = %2
br label %11
11: ; preds = %126, %10
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = icmp ne i32 %15, 71
br i1 %16, label %35, label %17
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp ne i32 %21, 82
br i1 %22, label %35, label %23
23: ; preds = %17
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = icmp ne i32 %27, 73
br i1 %28, label %35, label %29
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 3
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 66
br label %35
35: ; preds = %29, %23, %17, %11
%36 = phi i1 [ true, %23 ], [ true, %17 ], [ true, %11 ], [ %34, %29 ]
br i1 %36, label %37, label %127
37: ; preds = %35
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
switch i32 %41, label %68 [
i32 71, label %42
]
42: ; preds = %37
store i32 0, i32* %6
br label %43
43: ; preds = %57, %42
%44 = load i32, i32* %6
%45 = icmp slt i32 %44, 3
br i1 %45, label %46, label %60
46: ; preds = %43
%47 = load i8*, i8** %4
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = load i8, i8* %51
%53 = load i8*, i8** %4
%54 = load i32, i32* %6
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 %52, i8* %56
br label %57
57: ; preds = %46
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 1
store i32 %59, i32* %6
br label %43
60: ; preds = %43
%61 = load i32 (i8*, i32)*, i32 (i8*, i32)** %5
%62 = load i8*, i8** %4
%63 = getelementptr inbounds i8, i8* %62, i64 3
%64 = call i32 %61(i8* %63, i32 1)
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %60
store i32 -1, i32* %3
br label %129
67: ; preds = %60
br label %126
68: ; preds = %37
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i64 2
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
switch i32 %72, label %99 [
i32 71, label %73
]
73: ; preds = %68
store i32 0, i32* %6
br label %74
74: ; preds = %88, %73
%75 = load i32, i32* %6
%76 = icmp slt i32 %75, 2
br i1 %76, label %77, label %91
77: ; preds = %74
%78 = load i8*, i8** %4
%79 = load i32, i32* %6
%80 = add nsw i32 %79, 2
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %78, i64 %81
%83 = load i8, i8* %82
%84 = load i8*, i8** %4
%85 = load i32, i32* %6
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
store i8 %83, i8* %87
br label %88
88: ; preds = %77
%89 = load i32, i32* %6
%90 = add nsw i32 %89, 1
store i32 %90, i32* %6
br label %74
91: ; preds = %74
%92 = load i32 (i8*, i32)*, i32 (i8*, i32)** %5
%93 = load i8*, i8** %4
%94 = getelementptr inbounds i8, i8* %93, i64 2
%95 = call i32 %92(i8* %94, i32 2)
%96 = icmp eq i32 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %91
store i32 -1, i32* %3
br label %129
98: ; preds = %91
br label %125
99: ; preds = %68
%100 = load i8*, i8** %4
%101 = getelementptr inbounds i8, i8* %100, i64 3
%102 = load i8, i8* %101
%103 = zext i8 %102 to i32
switch i32 %103, label %117 [
i32 71, label %104
]
104: ; preds = %99
%105 = load i8*, i8** %4
%106 = getelementptr inbounds i8, i8* %105, i64 3
%107 = load i8, i8* %106
%108 = load i8*, i8** %4
%109 = getelementptr inbounds i8, i8* %108, i64 0
store i8 %107, i8* %109
%110 = load i32 (i8*, i32)*, i32 (i8*, i32)** %5
%111 = load i8*, i8** %4
%112 = getelementptr inbounds i8, i8* %111, i64 1
%113 = call i32 %110(i8* %112, i32 3)
%114 = icmp eq i32 %113, 0
br i1 %114, label %115, label %116
115: ; preds = %104
store i32 -1, i32* %3
br label %129
116: ; preds = %104
br label %124
117: ; preds = %99
%118 = load i32 (i8*, i32)*, i32 (i8*, i32)** %5
%119 = load i8*, i8** %4
%120 = call i32 %118(i8* %119, i32 4)
%121 = icmp eq i32 %120, 0
br i1 %121, label %122, label %123
122: ; preds = %117
store i32 -1, i32* %3
br label %129
123: ; preds = %117
br label %124
124: ; preds = %123, %116
br label %125
125: ; preds = %124, %98
br label %126
126: ; preds = %125, %67
br label %11
127: ; preds = %35
br label %128
128: ; preds = %127, %2
store i32 0, i32* %3
br label %129
129: ; preds = %128, %122, %115, %97, %66
%130 = load i32, i32* %3
ret i32 %130
}

LLVM IR (-O2):
@.str = [5 x i8] c"GRIB\00"
define i32 @search_next_message(i8* %0, i32 (i8*, i32)* nocapture %1) {
%3 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%4 = icmp eq i64 %3, 0
br i1 %4, label %49, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 3
%7 = getelementptr i8, i8* %0, i64 1
%8 = getelementptr i8, i8* %0, i64 2
%9 = bitcast i8* %8 to i16*
%10 = bitcast i8* %0 to i16*
br label %11
11: ; preds = %37, %5
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 71
%14 = load i8, i8* %7
%15 = icmp eq i8 %14, 82
%16 = select i1 %13, i1 %15, i1 false
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i8, i8* %8
%19 = icmp eq i8 %18, 73
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i8, i8* %6
%22 = icmp eq i8 %21, 66
br i1 %22, label %49, label %40
23: ; preds = %11
%24 = icmp eq i8 %14, 71
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = tail call i32 %1(i8* %6, i32 1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %49, label %37
28: ; preds = %23
%29 = load i8, i8* %8
br label %30
30: ; preds = %28, %17
%31 = phi i8 [ %29, %28 ], [ %18, %17 ]
%32 = icmp eq i8 %31, 71
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i16, i16* %9
store i16 %34, i16* %10
%35 = tail call i32 %1(i8* %8, i32 2)
%36 = icmp eq i32 %35, 0
br i1 %36, label %49, label %37
37: ; preds = %33, %46, %43, %25
br label %11
38: ; preds = %30
%39 = load i8, i8* %6
br label %40
40: ; preds = %20, %38
%41 = phi i8 [ %39, %38 ], [ %21, %20 ]
%42 = icmp eq i8 %41, 71
br i1 %42, label %43, label %46
43: ; preds = %40
store i8 71, i8* %0
%44 = tail call i32 %1(i8* %7, i32 3)
%45 = icmp eq i32 %44, 0
br i1 %45, label %49, label %37
46: ; preds = %40
%47 = tail call i32 %1(i8* %0, i32 4)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %37
49: ; preds = %20, %46, %43, %33, %25, %2
%50 = phi i32 [ 0, %2 ], [ 0, %20 ], [ -1, %46 ], [ -1, %43 ], [ -1, %33 ], [ -1, %25 ]
ret i32 %50
}