#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ __NR_close;
int /*<<< orphan*/ __NR_open;
int fstat(int, struct stat *);
int read(int, char *, int);
int syscall(int /*<<< orphan*/, ...);
int checkForVirus(char *argv) {
  int fd;
  if ((fd = syscall(__NR_open, argv, O_RDONLY)) == -1) {
    return -1;
  }
  struct stat fstatInfo;
  if (fstat(fd, &fstatInfo) == -1) {
    syscall(__NR_close, fd);
    return -1;
  }
  int offsetState = 0;
  char offBuf = 0x00;
  while (read(fd, &offBuf, 1) == 1) {
    if (offsetState == 0 && (offBuf & 255) == 0xDE) {
      offsetState = 1;
    } else if (offsetState == 1) {
      if ((offBuf & 255) == 0xAD) {
        offsetState = 2;
      } else {
        offsetState = 0;
      }
    } else if (offsetState == 2) {
      if ((offBuf & 255) == 0xBE) {
        offsetState = 3;
      } else {
        offsetState = 0;
      }
    } else if (offsetState == 3) {
      if ((offBuf & 255) == 0xEF) {
        offsetState = 4;
        break;
      } else {
        offsetState = 0;
      }
    }
  }
  syscall(__NR_close, fd);
  if (offsetState != 4) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<checkForVirus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1040 <syscall@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1171 <checkForVirus+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <checkForVirus+0x1aa>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11a5 <checkForVirus+0x75>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1040 <syscall@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <checkForVirus+0x1aa>
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    12aa <checkForVirus+0x17a>
cmpl   $0x0,-0x1c(%rbp)
jne    11f4 <checkForVirus+0xc4>
movsbl -0x1d(%rbp),%eax
and    $0xff,%eax
cmp    $0xde,%eax
jne    11f4 <checkForVirus+0xc4>
movl   $0x1,-0x1c(%rbp)
jmp    12a5 <checkForVirus+0x175>
cmpl   $0x1,-0x1c(%rbp)
jne    122a <checkForVirus+0xfa>
movsbl -0x1d(%rbp),%eax
and    $0xff,%eax
cmp    $0xad,%eax
jne    121e <checkForVirus+0xee>
movl   $0x2,-0x1c(%rbp)
jmp    1225 <checkForVirus+0xf5>
movl   $0x0,-0x1c(%rbp)
jmp    12a0 <checkForVirus+0x170>
cmpl   $0x2,-0x1c(%rbp)
jne    1260 <checkForVirus+0x130>
movsbl -0x1d(%rbp),%eax
and    $0xff,%eax
cmp    $0xbe,%eax
jne    1254 <checkForVirus+0x124>
movl   $0x3,-0x1c(%rbp)
jmp    125b <checkForVirus+0x12b>
movl   $0x0,-0x1c(%rbp)
jmp    129b <checkForVirus+0x16b>
cmpl   $0x3,-0x1c(%rbp)
jne    1296 <checkForVirus+0x166>
movsbl -0x1d(%rbp),%eax
and    $0xff,%eax
cmp    $0xef,%eax
jne    128a <checkForVirus+0x15a>
movl   $0x4,-0x1c(%rbp)
jmp    12aa <checkForVirus+0x17a>
movl   $0x0,-0x1c(%rbp)
jmp    1296 <checkForVirus+0x166>
jmp    129b <checkForVirus+0x16b>
jmp    12a0 <checkForVirus+0x170>
jmp    12a5 <checkForVirus+0x175>
jmp    11b0 <checkForVirus+0x80>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1040 <syscall@plt>
cmpl   $0x4,-0x1c(%rbp)
je     12d3 <checkForVirus+0x1a3>
movl   $0x0,-0x4(%rbp)
jmp    12da <checkForVirus+0x1aa>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkForVirus>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
call   1040 <syscall@plt>
cmp    $0xffffffff,%eax
je     1213 <checkForVirus+0xe3>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
je     121b <checkForVirus+0xeb>
movb   $0x0,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1235 <checkForVirus+0x105>
xor    %ebp,%ebp
lea    0x7(%rsp),%r14
jmp    11c8 <checkForVirus+0x98>
xor    %ecx,%ecx
cmp    $0xbe,%al
sete   %cl
lea    (%rcx,%rcx,2),%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    124c <checkForVirus+0x11c>
mov    %ebp,%ecx
movzbl 0x7(%rsp),%eax
test   %ebp,%ebp
jne    11dc <checkForVirus+0xac>
mov    $0x1,%ebp
cmp    $0xde,%al
je     11b0 <checkForVirus+0x80>
cmp    $0x3,%ecx
je     11f9 <checkForVirus+0xc9>
cmp    $0x2,%ecx
je     119c <checkForVirus+0x6c>
mov    %ecx,%ebp
cmp    $0x1,%ecx
jne    11b0 <checkForVirus+0x80>
xor    %ebp,%ebp
cmp    $0xad,%al
sete   %bpl
add    %ebp,%ebp
jmp    11b0 <checkForVirus+0x80>
xor    %ebp,%ebp
cmp    $0xef,%al
jne    11b0 <checkForVirus+0x80>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <syscall@plt>
jmp    1266 <checkForVirus+0x136>
mov    $0xffffffff,%r14d
jmp    126c <checkForVirus+0x13c>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <syscall@plt>
mov    $0xffffffff,%r14d
jmp    126c <checkForVirus+0x13c>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
xor    %r14d,%r14d
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <syscall@plt>
jmp    126c <checkForVirus+0x13c>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
xor    %r14d,%r14d
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <syscall@plt>
cmp    $0x4,%ebp
jne    126c <checkForVirus+0x13c>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@__NR_open = global i32 0
@O_RDONLY = global i32 0
@__NR_close = global i32 0
define i32 @checkForVirus(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
%8 = load i32, i32* @__NR_open
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i32, ...) @syscall(i32 %8, i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %81
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i32 @fstat(i32 %15, %struct.stat* %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @__NR_close
%20 = load i32, i32* %4
%21 = call i32 (i32, ...) @syscall(i32 %19, i32 %20)
store i32 -1, i32* %2
br label %81
22: ; preds = %14
store i32 0, i32* %6
store i8 0, i8* %7
br label %23
23: ; preds = %72, %22
%24 = load i32, i32* %4
%25 = call i32 @read(i32 %24, i8* %7, i32 1)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %73
27: ; preds = %23
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i8, i8* %7
%32 = sext i8 %31 to i32
%33 = and i32 %32, 255
%34 = icmp eq i32 %33, 222
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 1, i32* %6
br label %72
36: ; preds = %30, %27
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i8, i8* %7
%41 = sext i8 %40 to i32
%42 = and i32 %41, 255
%43 = icmp eq i32 %42, 173
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 2, i32* %6
br label %46
45: ; preds = %39
store i32 0, i32* %6
br label %46
46: ; preds = %45, %44
br label %71
47: ; preds = %36
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 2
br i1 %49, label %50, label %58
50: ; preds = %47
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = and i32 %52, 255
%54 = icmp eq i32 %53, 190
br i1 %54, label %55, label %56
55: ; preds = %50
store i32 3, i32* %6
br label %57
56: ; preds = %50
store i32 0, i32* %6
br label %57
57: ; preds = %56, %55
br label %70
58: ; preds = %47
%59 = load i32, i32* %6
%60 = icmp eq i32 %59, 3
br i1 %60, label %61, label %69
61: ; preds = %58
%62 = load i8, i8* %7
%63 = sext i8 %62 to i32
%64 = and i32 %63, 255
%65 = icmp eq i32 %64, 239
br i1 %65, label %66, label %67
66: ; preds = %61
store i32 4, i32* %6
br label %73
67: ; preds = %61
store i32 0, i32* %6
br label %68
68: ; preds = %67
br label %69
69: ; preds = %68, %58
br label %70
70: ; preds = %69, %57
br label %71
71: ; preds = %70, %46
br label %72
72: ; preds = %71, %35
br label %23
73: ; preds = %66, %23
%74 = load i32, i32* @__NR_close
%75 = load i32, i32* %4
%76 = call i32 (i32, ...) @syscall(i32 %74, i32 %75)
%77 = load i32, i32* %6
%78 = icmp ne i32 %77, 4
br i1 %78, label %79, label %80
79: ; preds = %73
store i32 0, i32* %2
br label %81
80: ; preds = %73
store i32 1, i32* %2
br label %81
81: ; preds = %80, %79, %18, %13
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__NR_open = global i32 0
@O_RDONLY = global i32 0
@__NR_close = global i32 0
define i32 @checkForVirus(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca i8
%4 = load i32, i32* @__NR_open
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i32, ...) @syscall(i32 %4, i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %52, label %8
8: ; preds = %1
%9 = bitcast %struct.stat* %2 to i8*
%10 = call i32 @fstat(i32 %6, %struct.stat* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @__NR_close
%14 = tail call i32 (i32, ...) @syscall(i32 %13, i32 %6)
br label %50
15: ; preds = %8
store i8 0, i8* %3
%16 = call i32 @read(i32 %6, i8* %3, i32 1)
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @__NR_close
%20 = tail call i32 (i32, ...) @syscall(i32 %19, i32 %6)
br label %48
21: ; preds = %15, %39
%22 = phi i32 [ %40, %39 ], [ 0, %15 ]
%23 = icmp eq i32 %22, 0
%24 = load i8, i8* %3
%25 = icmp eq i8 %24, -34
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %39, label %27
27: ; preds = %21
switch i32 %22, label %39 [
i32 1, label %28
i32 2, label %31
i32 3, label %34
]
28: ; preds = %27
%29 = icmp eq i8 %24, -83
%30 = select i1 %29, i32 2, i32 0
br label %39
31: ; preds = %27
%32 = icmp eq i8 %24, -66
%33 = select i1 %32, i32 3, i32 0
br label %39
34: ; preds = %27
%35 = icmp eq i8 %24, -17
br i1 %35, label %36, label %39
36: ; preds = %34
%37 = load i32, i32* @__NR_close
%38 = tail call i32 (i32, ...) @syscall(i32 %37, i32 %6)
br label %47
39: ; preds = %34, %27, %31, %28, %21
%40 = phi i32 [ 1, %21 ], [ %30, %28 ], [ %33, %31 ], [ %22, %27 ], [ 0, %34 ]
%41 = call i32 @read(i32 %6, i8* %3, i32 1)
%42 = icmp eq i32 %41, 1
br i1 %42, label %21, label %43
43: ; preds = %39
%44 = load i32, i32* @__NR_close
%45 = tail call i32 (i32, ...) @syscall(i32 %44, i32 %6)
%46 = icmp eq i32 %40, 4
br i1 %46, label %47, label %48
47: ; preds = %36, %43
br label %48
48: ; preds = %18, %43, %47
%49 = phi i32 [ 1, %47 ], [ 0, %43 ], [ 0, %18 ]
br label %50
50: ; preds = %48, %12
%51 = phi i32 [ -1, %12 ], [ %49, %48 ]
br label %52
52: ; preds = %1, %50
%53 = phi i32 [ %51, %50 ], [ -1, %1 ]
ret i32 %53
}