#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned char *NCEP_GDS(unsigned char *, unsigned char);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ printf(char *, int, int);
long rd_grib_msg(int /*<<< orphan*/ *, long, unsigned char **, unsigned char **,
                 unsigned char **, unsigned char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wrt_grib_msg(int /*<<< orphan*/ *, unsigned char *,
                                unsigned char *, unsigned char *,
                                unsigned char *);
int main(int argc, char **argv) {
  long int len_grib, pos = 0;
  unsigned char *pds, *gds, *bms, *bds, *gds0;
  FILE *input, *output;
  int nrec, ngds;
  if (argc != 3) {
    fprintf(stderr, "%s [in gribfile] [out gribfile]\n", argv[0]);
    fprintf(stderr, "adds GDS to NCEP files\n");
    exit(8);
  }
  if ((input = fopen(argv[1], "rb")) == NULL) {
    fprintf(stderr, "could not open file: %s\n", argv[1]);
    exit(7);
  }
  if ((output = fopen(argv[2], "wb")) == NULL) {
    fprintf(stderr, "could not open file: %s\n", argv[2]);
    exit(7);
  }
  nrec = ngds = pos = 0;
  for (;;) {
    len_grib = rd_grib_msg(input, pos, &pds, &gds, &bms, &bds);
    if (len_grib <= 0)
      break;
    nrec++;
    if (((pds[7] & 128) != 0) || (pds[4]) != 7) {
      wrt_grib_msg(output, pds, gds, bms, bds);
    } else {
      gds0 = NCEP_GDS(pds, (pds[6]));
      wrt_grib_msg(output, pds, gds0, bms, bds);
      if (gds0)
        free(gds0);
      ngds++;
    }
    pos += len_grib;
  }
  fclose(output);
  fclose(input);
  printf("%d GDS sections added out of %d records\n", ngds, nrec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11f7 <main+0x67>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x8,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe33(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    1242 <main+0xb2>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x7,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe04(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
jne    128d <main+0xfd>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x7,%edi
call   10a0 <exit@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x60(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x38(%rbp),%r8
lea    -0x40(%rbp),%r9
call   1080 <rd_grib_msg@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     12d4 <main+0x144>
jmp    137e <main+0x1ee>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x28(%rbp),%rax
movzbl 0x7(%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1304 <main+0x174>
mov    -0x28(%rbp),%rax
movzbl 0x4(%rax),%eax
cmp    $0x7,%eax
je     1322 <main+0x192>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1060 <wrt_grib_msg@plt>
jmp    136d <main+0x1dd>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rax
movzbl 0x6(%rax),%esi
call   10b0 <NCEP_GDS@plt>
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1060 <wrt_grib_msg@plt>
cmpq   $0x0,-0x48(%rbp)
je     1364 <main+0x1d4>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    12a3 <main+0x113>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x50(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x60(%rbp),%esi
mov    -0x5c(%rbp),%edx
lea    0xcbb(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1309 <main+0x179>
mov    0x8(%rbx),%rdi
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     133f <main+0x1af>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     134e <main+0x1be>
xor    %ebx,%ebx
mov    %rsp,%rdx
lea    0x20(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <rd_grib_msg@plt>
mov    $0x0,%r12d
test   %rax,%rax
jle    12d5 <main+0x145>
mov    %rax,%rbp
xor    %r12d,%r12d
mov    %rsp,%r15
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    1274 <main+0xe4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rdx
mov    0x18(%rsp),%rcx
mov    0x10(%rsp),%r8
mov    0x8(%rsp),%rdi
call   1060 <wrt_grib_msg@plt>
add    %rbp,%r13
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
lea    0x20(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
call   1080 <rd_grib_msg@plt>
mov    %rax,%rbp
add    $0x1,%ebx
test   %rax,%rax
jle    12d5 <main+0x145>
mov    (%rsp),%rsi
cmpb   $0x0,0x7(%rsi)
js     1230 <main+0xa0>
cmpb   $0x7,0x4(%rsi)
jne    1230 <main+0xa0>
mov    %r14,%r15
mov    %r12,%r14
movzbl 0x6(%rsi),%eax
mov    %rsi,%rdi
mov    %eax,%esi
call   10b0 <NCEP_GDS@plt>
mov    %rax,%r12
mov    (%rsp),%rsi
mov    0x18(%rsp),%rcx
mov    0x10(%rsp),%r8
mov    0x8(%rsp),%rdi
mov    %rax,%rdx
call   1060 <wrt_grib_msg@plt>
test   %r12,%r12
je     12c3 <main+0x133>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%r12
add    $0x1,%r12d
mov    %r15,%r14
mov    %rsp,%r15
jmp    1249 <main+0xb9>
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xd6a(%rip),%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cd0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    (%rbx),%rdx
lea    0xce3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xcf3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x8,%edi
call   10a0 <exit@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
jmp    135b <main+0x1cb>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xcda(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x7,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"%s [in gribfile] [out gribfile]\0A\00"
@.str.1 = [24 x i8] c"adds GDS to NCEP files\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [25 x i8] c"could not open file: %s\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [41 x i8] c"%d GDS sections added out of %d records\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %28
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 8)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %13
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %41
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 7)
unreachable
41: ; preds = %28
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %45, i32** %14
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %54
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 7)
unreachable
54: ; preds = %41
store i64 0, i64* %7
store i32 0, i32* %16
store i32 0, i32* %15
br label %55
55: ; preds = %104, %54
%56 = load i32*, i32** %13
%57 = load i64, i64* %7
%58 = call i64 @rd_grib_msg(i32* %56, i64 %57, i8** %8, i8** %9, i8** %10, i8** %11)
store i64 %58, i64* %6
%59 = load i64, i64* %6
%60 = icmp sle i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %108
62: ; preds = %55
%63 = load i32, i32* %15
%64 = add nsw i32 %63, 1
store i32 %64, i32* %15
%65 = load i8*, i8** %8
%66 = getelementptr inbounds i8, i8* %65, i64 7
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = and i32 %68, 128
%70 = icmp ne i32 %69, 0
br i1 %70, label %77, label %71
71: ; preds = %62
%72 = load i8*, i8** %8
%73 = getelementptr inbounds i8, i8* %72, i64 4
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = icmp ne i32 %75, 7
br i1 %76, label %77, label %84
77: ; preds = %71, %62
%78 = load i32*, i32** %14
%79 = load i8*, i8** %8
%80 = load i8*, i8** %9
%81 = load i8*, i8** %10
%82 = load i8*, i8** %11
%83 = call i32 @wrt_grib_msg(i32* %78, i8* %79, i8* %80, i8* %81, i8* %82)
br label %104
84: ; preds = %71
%85 = load i8*, i8** %8
%86 = load i8*, i8** %8
%87 = getelementptr inbounds i8, i8* %86, i64 6
%88 = load i8, i8* %87
%89 = call i8* @NCEP_GDS(i8* %85, i8 zeroext %88)
store i8* %89, i8** %12
%90 = load i32*, i32** %14
%91 = load i8*, i8** %8
%92 = load i8*, i8** %12
%93 = load i8*, i8** %10
%94 = load i8*, i8** %11
%95 = call i32 @wrt_grib_msg(i32* %90, i8* %91, i8* %92, i8* %93, i8* %94)
%96 = load i8*, i8** %12
%97 = icmp ne i8* %96, null
br i1 %97, label %98, label %101
98: ; preds = %84
%99 = load i8*, i8** %12
%100 = call i32 @free(i8* %99)
br label %101
101: ; preds = %98, %84
%102 = load i32, i32* %16
%103 = add nsw i32 %102, 1
store i32 %103, i32* %16
br label %104
104: ; preds = %101, %77
%105 = load i64, i64* %6
%106 = load i64, i64* %7
%107 = add nsw i64 %106, %105
store i64 %107, i64* %7
br label %55
108: ; preds = %61
%109 = load i32*, i32** %14
%110 = call i32 @fclose(i32* %109)
%111 = load i32*, i32** %13
%112 = call i32 @fclose(i32* %111)
%113 = load i32, i32* %16
%114 = load i32, i32* %15
%115 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %113, i32 %114)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"%s [in gribfile] [out gribfile]\0A\00"
@.str.1 = [24 x i8] c"adds GDS to NCEP files\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [25 x i8] c"could not open file: %s\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [41 x i8] c"%d GDS sections added out of %d records\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = bitcast i8** %3 to i8*
%8 = bitcast i8** %4 to i8*
%9 = bitcast i8** %5 to i8*
%10 = bitcast i8** %6 to i8*
%11 = icmp eq i32 %0, 3
br i1 %11, label %19, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 8)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %20
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = tail call i32 @exit(i32 7)
unreachable
29: ; preds = %19
%30 = getelementptr inbounds i8*, i8** %1, i64 2
%31 = load i8*, i8** %30
%32 = tail call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = call i64 @rd_grib_msg(i32* %22, i64 0, i8** %3, i8** %4, i8** %5, i8** %6)
%36 = icmp slt i64 %35, 1
br i1 %36, label %79, label %42
37: ; preds = %29
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %30
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = tail call i32 @exit(i32 7)
unreachable
42: ; preds = %34, %74
%43 = phi i64 [ %77, %74 ], [ %35, %34 ]
%44 = phi i32 [ %75, %74 ], [ 0, %34 ]
%45 = phi i32 [ %47, %74 ], [ 0, %34 ]
%46 = phi i64 [ %76, %74 ], [ 0, %34 ]
%47 = add nuw nsw i32 %45, 1
%48 = load i8*, i8** %3
%49 = getelementptr inbounds i8, i8* %48, i64 7
%50 = load i8, i8* %49
%51 = icmp sgt i8 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %42
%53 = getelementptr inbounds i8, i8* %48, i64 4
%54 = load i8, i8* %53
%55 = icmp eq i8 %54, 7
br i1 %55, label %61, label %56
56: ; preds = %52, %42
%57 = load i8*, i8** %4
%58 = load i8*, i8** %5
%59 = load i8*, i8** %6
%60 = call i32 @wrt_grib_msg(i32* %32, i8* %48, i8* %57, i8* %58, i8* %59)
br label %74
61: ; preds = %52
%62 = getelementptr inbounds i8, i8* %48, i64 6
%63 = load i8, i8* %62
%64 = call i8* @NCEP_GDS(i8* %48, i8 zeroext %63)
%65 = load i8*, i8** %3
%66 = load i8*, i8** %5
%67 = load i8*, i8** %6
%68 = call i32 @wrt_grib_msg(i32* %32, i8* %65, i8* %64, i8* %66, i8* %67)
%69 = icmp eq i8* %64, null
br i1 %69, label %72, label %70
70: ; preds = %61
%71 = call i32 @free(i8* %64)
br label %72
72: ; preds = %70, %61
%73 = add nsw i32 %44, 1
br label %74
74: ; preds = %72, %56
%75 = phi i32 [ %44, %56 ], [ %73, %72 ]
%76 = add nsw i64 %43, %46
%77 = call i64 @rd_grib_msg(i32* %22, i64 %76, i8** %3, i8** %4, i8** %5, i8** %6)
%78 = icmp slt i64 %77, 1
br i1 %78, label %79, label %42
79: ; preds = %74, %34
%80 = phi i32 [ 0, %34 ], [ %47, %74 ]
%81 = phi i32 [ 0, %34 ], [ %75, %74 ]
%82 = call i32 @fclose(i32* %32)
%83 = call i32 @fclose(i32* %22)
%84 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %81, i32 %80)
ret i32 0
}