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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseeko64(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftello64(int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int *, char *, int);
int /*<<< orphan*/ *plat_fopen(int *, char *);
scalar_t__ wcscasecmp(int *, char *);
int wcslen(int const *);
int image_is_vhd(const wchar_t *s, int check_signature) {
  int len;
  FILE *f;
  uint64_t filelen;
  uint64_t signature;
  char *ws = (char *)s;
  wchar_t ext[5] = {0, 0, 0, 0, 0};
  len = wcslen(s);
  if ((len < 4) || (s[0] == L'.'))
    return 0;
  memcpy(ext, ws + ((len - 4) << 1), 8);
  if (wcscasecmp(ext, L".VHD") == 0) {
    if (check_signature) {
      f = plat_fopen((wchar_t *)s, L"rb");
      if (!f)
        return 0;
      fseeko64(f, 0, SEEK_END);
      filelen = ftello64(f);
      fseeko64(f, -512, SEEK_END);
      if (filelen < 512)
        return 0;
      fread(&signature, 1, 8, f);
      fclose(f);
      if (signature == 0x78697463656E6F63ll)
        return 1;
      else
        return 0;
    } else
      return 1;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<image_is_vhd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1050 <wcslen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jl     11da <image_is_vhd+0x4a>
mov    -0x10(%rbp),%rax
cmpl   $0x2e,(%rax)
jne    11e6 <image_is_vhd+0x56>
movl   $0x0,-0x4(%rbp)
jmp    1307 <image_is_vhd+0x177>
lea    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%eax
sub    $0x4,%eax
shl    $0x1,%eax
cltq
add    %rax,%rsi
mov    $0x8,%edx
call   1090 <memcpy@plt>
lea    -0x50(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1080 <wcscasecmp@plt>
cmp    $0x0,%rax
jne    1300 <image_is_vhd+0x170>
cmpl   $0x0,-0x14(%rbp)
je     12f4 <image_is_vhd+0x164>
mov    -0x10(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   10b0 <plat_fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1255 <image_is_vhd+0xc5>
movl   $0x0,-0x4(%rbp)
jmp    1307 <image_is_vhd+0x177>
mov    -0x20(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseeko64@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftello64@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffe00,%esi
call   10a0 <fseeko64@plt>
cmpl   $0x200,-0x24(%rbp)
jge    12a5 <image_is_vhd+0x115>
movl   $0x0,-0x4(%rbp)
jmp    1307 <image_is_vhd+0x177>
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movslq -0x28(%rbp),%rax
movabs $0x78697463656e6f63,%rcx
74 69 78
cmp    %rcx,%rax
jne    12e8 <image_is_vhd+0x158>
movl   $0x1,-0x4(%rbp)
jmp    1307 <image_is_vhd+0x177>
movl   $0x0,-0x4(%rbp)
jmp    1307 <image_is_vhd+0x177>
movl   $0x1,-0x4(%rbp)
jmp    1307 <image_is_vhd+0x177>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_is_vhd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
call   1050 <wcslen@plt>
xor    %ebp,%ebp
cmp    $0x4,%eax
jl     11e2 <image_is_vhd+0x62>
cmpl   $0x2e,(%rbx)
je     11e2 <image_is_vhd+0x62>
add    %eax,%eax
add    $0xfffffff8,%eax
movslq %eax,%rsi
add    %rbx,%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x8,%edx
call   1080 <memcpy@plt>
lea    0xe2b(%rip),%rsi
mov    %r15,%rdi
call   1070 <wcscasecmp@plt>
test   %rax,%rax
je     11ef <image_is_vhd+0x6f>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14d,%r14d
je     1269 <image_is_vhd+0xe9>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <plat_fopen@plt>
test   %rax,%rax
je     11e2 <image_is_vhd+0x62>
mov    %rax,%rbx
mov    0x2dd6(%rip),%r15
mov    (%r15),%edx
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
call   1090 <fseeko64@plt>
mov    %rbx,%rdi
call   1060 <ftello64@plt>
mov    %eax,%r14d
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0xfffffe00,%esi
call   1090 <fseeko64@plt>
cmp    $0x200,%r14d
jl     11e2 <image_is_vhd+0x62>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11e2 <image_is_vhd+0x62>
mov    $0x1,%ebp
jmp    11e2 <image_is_vhd+0x62>

LLVM IR (-O0):
@.str = [5 x i32] [i32 46, i32 86, i32 72, i32 68, i32 0]
@.str.1 = [3 x i32] [i32 114, i32 98, i32 0]
@SEEK_END = global i32 0
define i32 @image_is_vhd(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [5 x i32]
store i32* %0, i32** %4
store i32 %1, i32* %5
%12 = load i32*, i32** %4
%13 = bitcast i32* %12 to i8*
store i8* %13, i8** %10
%14 = bitcast [5 x i32]* %11 to i8*
%15 = load i32*, i32** %4
%16 = call i32 @wcslen(i32* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 4
br i1 %18, label %24, label %19
19: ; preds = %2
%20 = load i32*, i32** %4
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, 46
br i1 %23, label %24, label %25
24: ; preds = %19, %2
store i32 0, i32* %3
br label %70
25: ; preds = %19
%26 = getelementptr inbounds [5 x i32], [5 x i32]* %11, i64 0, i64 0
%27 = load i8*, i8** %10
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 4
%30 = shl i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %27, i64 %31
%33 = call i32 @memcpy(i32* %26, i8* %32, i32 8)
%34 = getelementptr inbounds [5 x i32], [5 x i32]* %11, i64 0, i64 0
%35 = call i64 @wcscasecmp(i32* %34, i8* bitcast ([5 x i32]* @.str to i8*))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %69
37: ; preds = %25
%38 = load i32, i32* %5
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %68
40: ; preds = %37
%41 = load i32*, i32** %4
%42 = call i32* @plat_fopen(i32* %41, i8* bitcast ([3 x i32]* @.str.1 to i8*))
store i32* %42, i32** %7
%43 = load i32*, i32** %7
%44 = icmp ne i32* %43, null
br i1 %44, label %46, label %45
45: ; preds = %40
store i32 0, i32* %3
br label %70
46: ; preds = %40
%47 = load i32*, i32** %7
%48 = load i32, i32* @SEEK_END
%49 = call i32 @fseeko64(i32* %47, i32 0, i32 %48)
%50 = load i32*, i32** %7
%51 = call i32 @ftello64(i32* %50)
store i32 %51, i32* %8
%52 = load i32*, i32** %7
%53 = load i32, i32* @SEEK_END
%54 = call i32 @fseeko64(i32* %52, i32 -512, i32 %53)
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 512
br i1 %56, label %57, label %58
57: ; preds = %46
store i32 0, i32* %3
br label %70
58: ; preds = %46
%59 = load i32*, i32** %7
%60 = call i32 @fread(i32* %9, i32 1, i32 8, i32* %59)
%61 = load i32*, i32** %7
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = icmp eq i64 %64, 8676594127358291811
br i1 %65, label %66, label %67
66: ; preds = %58
store i32 1, i32* %3
br label %70
67: ; preds = %58
store i32 0, i32* %3
br label %70
68: ; preds = %37
store i32 1, i32* %3
br label %70
69: ; preds = %25
store i32 0, i32* %3
br label %70
70: ; preds = %69, %68, %67, %66, %57, %45, %24
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [5 x i32] [i32 46, i32 86, i32 72, i32 68, i32 0]
@.str.1 = [3 x i32] [i32 114, i32 98, i32 0]
@SEEK_END = global i32 0
define i32 @image_is_vhd(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [5 x i32]
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %0 to i8*
%7 = bitcast [5 x i32]* %4 to i8*
%8 = tail call i32 @wcslen(i32* %0)
%9 = icmp slt i32 %8, 4
br i1 %9, label %37, label %10
10: ; preds = %2
%11 = load i32, i32* %0
%12 = icmp eq i32 %11, 46
br i1 %12, label %37, label %13
13: ; preds = %10
%14 = getelementptr inbounds [5 x i32], [5 x i32]* %4, i64 0, i64 0
%15 = shl nuw i32 %8, 1
%16 = add i32 %15, -8
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = call i32 @memcpy(i32* %14, i8* %18, i32 8)
%20 = call i64 @wcscasecmp(i32* %14, i8* bitcast ([5 x i32]* @.str to i8*))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %37
22: ; preds = %13
%23 = icmp eq i32 %1, 0
br i1 %23, label %37, label %24
24: ; preds = %22
%25 = call i32* @plat_fopen(i32* %0, i8* bitcast ([3 x i32]* @.str.1 to i8*))
%26 = icmp eq i32* %25, null
br i1 %26, label %37, label %27
27: ; preds = %24
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseeko64(i32* %25, i32 0, i32 %28)
%30 = call i32 @ftello64(i32* %25)
%31 = load i32, i32* @SEEK_END
%32 = call i32 @fseeko64(i32* %25, i32 -512, i32 %31)
%33 = icmp slt i32 %30, 512
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = call i32 @fread(i32* %3, i32 1, i32 8, i32* %25)
%36 = call i32 @fclose(i32* %25)
br label %37
37: ; preds = %13, %22, %27, %24, %2, %10, %34
%38 = phi i32 [ 0, %34 ], [ 0, %10 ], [ 0, %2 ], [ 0, %24 ], [ 0, %27 ], [ 1, %22 ], [ 0, %13 ]
ret i32 %38
}