#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 /*<<< orphan*/ strcpy(char *, char *);
int unpackgrid(int ngrid, char *grid) {
  char c[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
              'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
              'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' '};
  int dlat, dlong;
  ngrid = ngrid >> 7;
  if (ngrid < 32400) {
    dlat = (ngrid % 180) - 90;
    dlong = (ngrid / 180) * 2 - 180 + 2;
    if (dlong < -180)
      dlong = dlong + 360;
    if (dlong > 180)
      dlong = dlong + 360;
    int nlong = 60.0 * (180.0 - dlong) / 5.0;
    int n1 = nlong / 240;
    int n2 = (nlong - 240 * n1) / 24;
    grid[0] = c[10 + n1];
    grid[2] = c[n2];
    int nlat = 60.0 * (dlat + 90) / 2.5;
    n1 = nlat / 240;
    n2 = (nlat - 240 * n1) / 24;
    grid[1] = c[10 + n1];
    grid[3] = c[n2];
  } else {
    strcpy(grid, "XXXX");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<unpackgrid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xee6(%rip),%rsi
mov    $0x25,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%eax
sar    $0x7,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x7e90,-0x8(%rbp)
jge    129f <unpackgrid+0x17f>
mov    -0x8(%rbp),%eax
mov    $0xb4,%ecx
cltd
idiv   %ecx
sub    $0x5a,%edx
mov    %edx,-0x44(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xb4,%ecx
cltd
idiv   %ecx
shl    $0x1,%eax
sub    $0xb4,%eax
add    $0x2,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0xffffff4c,-0x48(%rbp)
jge    119c <unpackgrid+0x7c>
mov    -0x48(%rbp),%eax
add    $0x168,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0xb4,-0x48(%rbp)
jle    11b4 <unpackgrid+0x94>
mov    -0x48(%rbp),%eax
add    $0x168,%eax
mov    %eax,-0x48(%rbp)
cvtsi2sdl -0x48(%rbp),%xmm0
movsd  0xe57(%rip),%xmm1
subsd  %xmm0,%xmm1
movsd  0xe3b(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe37(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    $0xf0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
imul   $0xf0,-0x50(%rbp),%ecx
sub    %ecx,%eax
mov    $0x18,%ecx
cltd
idiv   %ecx
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%eax
add    $0xa,%eax
cltq
mov    -0x40(%rbp,%rax,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movslq -0x54(%rbp),%rax
mov    -0x40(%rbp,%rax,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x44(%rbp),%eax
add    $0x5a,%eax
cvtsi2sd %eax,%xmm1
movsd  0xdcc(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xdb8(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    $0xf0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x50(%rbp)
mov    -0x58(%rbp),%eax
imul   $0xf0,-0x50(%rbp),%ecx
sub    %ecx,%eax
mov    $0x18,%ecx
cltd
idiv   %ecx
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%eax
add    $0xa,%eax
cltq
mov    -0x40(%rbp,%rax,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
movslq -0x54(%rbp),%rax
mov    -0x40(%rbp,%rax,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
jmp    12bb <unpackgrid+0x19b>
mov    -0x10(%rbp),%rdi
lea    0xd9b(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c2 <unpackgrid+0x1a2>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpackgrid>:
push   %rax
cmp    $0x3f47ff,%edi
jg     123f <unpackgrid+0x12f>
mov    %edi,%eax
sar    $0x7,%eax
cltq
imul   $0xffffffffb60b60b7,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x7,%ecx
add    %edx,%ecx
imul   $0xb4,%ecx,%edx
sub    %edx,%eax
cmp    $0xffff4c80,%edi
lea    0xb6(%rcx,%rcx,1),%edx
lea    -0xb2(%rcx,%rcx,1),%ecx
cmovl  %edx,%ecx
cvtsi2sd %ecx,%xmm0
movsd  0xe9a(%rip),%xmm1
subsd  %xmm0,%xmm1
movsd  0xe96(%rip),%xmm0
mulsd  %xmm0,%xmm1
divsd  0xe92(%rip),%xmm1
cvttsd2si %xmm1,%ecx
movslq %ecx,%rcx
imul   $0xffffffff88888889,%rcx,%rdx
shr    $0x20,%rdx
add    %ecx,%edx
mov    %edx,%edi
shr    $0x1f,%edi
sar    $0x7,%edx
add    %edi,%edx
imul   $0xffffff10,%edx,%edi
add    %ecx,%edi
movslq %edi,%rcx
imul   $0x2aaaaaab,%rcx,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rdi
sar    $0x2,%edi
add    %ecx,%edi
movslq %edx,%rdx
lea    0xe58(%rip),%rcx
mov    0xa(%rdx,%rcx,1),%dl
mov    %dl,(%rsi)
movslq %edi,%rdx
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
mov    (%rdx,%rcx,1),%al
mulsd  %xmm0,%xmm1
divsd  0xe31(%rip),%xmm1
mov    %al,0x2(%rsi)
cvttsd2si %xmm1,%eax
cltq
imul   $0xffffffff88888889,%rax,%rdx
shr    $0x20,%rdx
add    %eax,%edx
mov    %edx,%edi
shr    $0x1f,%edi
sar    $0x7,%edx
add    %edi,%edx
imul   $0xffffff10,%edx,%edi
add    %eax,%edi
movslq %edi,%rax
imul   $0x2aaaaaab,%rax,%rax
mov    %rax,%rdi
shr    $0x3f,%rdi
sar    $0x22,%rax
add    %edi,%eax
movslq %edx,%rdx
mov    0xa(%rdx,%rcx,1),%dl
mov    %dl,0x1(%rsi)
cltq
mov    (%rax,%rcx,1),%al
mov    %al,0x3(%rsi)
mov    $0x1,%eax
pop    %rcx
ret
lea    0xdff(%rip),%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@__const.unpackgrid.c = [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
@.str = [5 x i8] c"XXXX\00"
define i32 @unpackgrid(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [37 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%13 = bitcast [37 x i8]* %6 to i8*
%14 = load i32, i32* %4
%15 = ashr i32 %14, 7
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 32400
br i1 %17, label %18, label %91
18: ; preds = %2
%19 = load i32, i32* %4
%20 = srem i32 %19, 180
%21 = sub nsw i32 %20, 90
store i32 %21, i32* %7
%22 = load i32, i32* %4
%23 = sdiv i32 %22, 180
%24 = mul nsw i32 %23, 2
%25 = sub nsw i32 %24, 180
%26 = add nsw i32 %25, 2
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, -180
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 360
store i32 %31, i32* %8
br label %32
32: ; preds = %29, %18
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 180
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 360
store i32 %37, i32* %8
br label %38
38: ; preds = %35, %32
%39 = load i32, i32* %8
%40 = sitofp i32 %39 to double
%41 = fsub double 1.800000e+02, %40
%42 = fmul double 6.000000e+01, %41
%43 = fdiv double %42, 5.000000e+00
%44 = fptosi double %43 to i32
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = sdiv i32 %45, 240
store i32 %46, i32* %10
%47 = load i32, i32* %9
%48 = load i32, i32* %10
%49 = mul nsw i32 240, %48
%50 = sub nsw i32 %47, %49
%51 = sdiv i32 %50, 24
store i32 %51, i32* %11
%52 = load i32, i32* %10
%53 = add nsw i32 10, %52
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [37 x i8], [37 x i8]* %6, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i64 0
store i8 %56, i8* %58
%59 = load i32, i32* %11
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [37 x i8], [37 x i8]* %6, i64 0, i64 %60
%62 = load i8, i8* %61
%63 = load i8*, i8** %5
%64 = getelementptr inbounds i8, i8* %63, i64 2
store i8 %62, i8* %64
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 90
%67 = sitofp i32 %66 to double
%68 = fmul double 6.000000e+01, %67
%69 = fdiv double %68, 2.500000e+00
%70 = fptosi double %69 to i32
store i32 %70, i32* %12
%71 = load i32, i32* %12
%72 = sdiv i32 %71, 240
store i32 %72, i32* %10
%73 = load i32, i32* %12
%74 = load i32, i32* %10
%75 = mul nsw i32 240, %74
%76 = sub nsw i32 %73, %75
%77 = sdiv i32 %76, 24
store i32 %77, i32* %11
%78 = load i32, i32* %10
%79 = add nsw i32 10, %78
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [37 x i8], [37 x i8]* %6, i64 0, i64 %80
%82 = load i8, i8* %81
%83 = load i8*, i8** %5
%84 = getelementptr inbounds i8, i8* %83, i64 1
store i8 %82, i8* %84
%85 = load i32, i32* %11
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [37 x i8], [37 x i8]* %6, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = load i8*, i8** %5
%90 = getelementptr inbounds i8, i8* %89, i64 3
store i8 %88, i8* %90
br label %94
91: ; preds = %2
%92 = load i8*, i8** %5
%93 = call i32 @strcpy(i8* %92, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %95
94: ; preds = %38
store i32 1, i32* %3
br label %95
95: ; preds = %94, %91
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@__const.unpackgrid.c = [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
@.str = [5 x i8] c"XXXX\00"
define i32 @unpackgrid(i32 %0, i8* %1) {
%3 = ashr i32 %0, 7
%4 = icmp slt i32 %0, 4147200
br i1 %4, label %5, label %46
5: ; preds = %2
%6 = srem i32 %3, 180
%7 = sdiv i32 %3, 180
%8 = shl nsw i32 %7, 1
%9 = icmp slt i32 %0, -45952
%10 = select i1 %9, i32 182, i32 -178
%11 = add nsw i32 %8, %10
%12 = sitofp i32 %11 to double
%13 = fsub double 1.800000e+02, %12
%14 = fmul double %13, 6.000000e+01
%15 = fdiv double %14, 5.000000e+00
%16 = fptosi double %15 to i32
%17 = sdiv i32 %16, 240
%18 = mul nsw i32 %17, -240
%19 = add i32 %18, %16
%20 = sdiv i32 %19, 24
%21 = add nsw i32 %17, 10
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [37 x i8], [37 x i8]* @__const.unpackgrid.c, i64 0, i64 %22
%24 = load i8, i8* %23
store i8 %24, i8* %1
%25 = sext i32 %20 to i64
%26 = getelementptr inbounds [37 x i8], [37 x i8]* @__const.unpackgrid.c, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %27, i8* %28
%29 = sitofp i32 %6 to double
%30 = fmul double %29, 6.000000e+01
%31 = fdiv double %30, 2.500000e+00
%32 = fptosi double %31 to i32
%33 = sdiv i32 %32, 240
%34 = mul nsw i32 %33, -240
%35 = add i32 %34, %32
%36 = sdiv i32 %35, 24
%37 = add nsw i32 %33, 10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [37 x i8], [37 x i8]* @__const.unpackgrid.c, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %40, i8* %41
%42 = sext i32 %36 to i64
%43 = getelementptr inbounds [37 x i8], [37 x i8]* @__const.unpackgrid.c, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %44, i8* %45
br label %48
46: ; preds = %2
%47 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %48
48: ; preds = %5, %46
%49 = phi i32 [ 1, %5 ], [ 0, %46 ]
ret i32 %49
}