#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
char *strstr(char *, char *);
int DetectLinuxVersion(void) {
  int fd, v;
  char buf[64], *p;
  if ((fd = open("/proc/version", O_RDONLY)) < 0) {
    printf("Fail to open version file\n");
    return -1;
  }
  memset(buf, 0, 64);
  read(fd, buf, 63);
  close(fd);
  p = strstr(buf, "Linux version");
  v = p[16] - '0';
  if (v == 5)
    v = 6;
  return v;
}


x86 assembly (clang, -O0):
<DetectLinuxVersion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a1 <DetectLinuxVersion+0x41>
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <DetectLinuxVersion+0xa3>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    -0x50(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
movsbl 0x10(%rax),%eax
sub    $0x30,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jne    11fd <DetectLinuxVersion+0x9d>
movl   $0x6,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DetectLinuxVersion>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11cd <DetectLinuxVersion+0x6d>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe5d(%rip),%rsi
mov    %r14,%rdi
call   1080 <strstr@plt>
movsbl 0x10(%rax),%ecx
add    $0xffffffd0,%ecx
cmp    $0x5,%ecx
mov    $0x6,%eax
cmovne %ecx,%eax
jmp    11de <DetectLinuxVersion+0x7e>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/version\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"Fail to open version file\0A\00"
@.str.2 = [14 x i8] c"Linux version\00"
define i32 @DetectLinuxVersion() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca i8*
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %31
11: ; preds = %0
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 64)
%14 = load i32, i32* %2
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 63)
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%20 = call i8* @strstr(i8* %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 16
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = sub nsw i32 %24, 48
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 5
br i1 %27, label %28, label %29
28: ; preds = %11
store i32 6, i32* %3
br label %29
29: ; preds = %28, %11
%30 = load i32, i32* %3
store i32 %30, i32* %1
br label %31
31: ; preds = %29, %9
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/version\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"Linux version\00"
@str = [26 x i8] c"Fail to open version file\00"
define i32 @DetectLinuxVersion() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %19
8: ; preds = %0
%9 = call i32 @memset(i8* %2, i32 0, i32 64)
%10 = call i32 @read(i32 %4, i8* %2, i32 63)
%11 = call i32 @close(i32 %4)
%12 = call i8* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%13 = getelementptr inbounds i8, i8* %12, i64 16
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = add nsw i32 %15, -48
%17 = icmp eq i32 %16, 5
%18 = select i1 %17, i32 6, i32 %16
br label %19
19: ; preds = %8, %6
%20 = phi i32 [ -1, %6 ], [ %18, %8 ]
ret i32 %20
}