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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ replace_char(char *, char, char);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strstr(char *, char *);
char *strstrip(char *);
int check_version(char *command, int required) {
  int result = 0;
  char full_command[151];
  char *version_command = " -version 2>&1";
  memset(full_command, 0, 151);
  snprintf(full_command, 150, "%s %s", command, version_command);
  char output[1024];
  FILE *fp;
  fp = popen(full_command, "r");
  if (fp != NULL) {
    if (fgets(output, sizeof(output), fp) != NULL) {
      char *version = "version";
      char *p = strstr(output, version);
      if (p != NULL) {
        p = p + 7;
        replace_char(p, '"', ' ');
        p = strstrip(p);
        int v = atoi(p + 2);
        result = v >= required;
      } else {
        puts("Version not found");
      }
    } else {
      puts("Cannot get output command\n");
    }
    pclose(fp);
  } else {
    perror("Error while executing command");
  }
  return result;
}


x86 assembly (clang, -O0):
<check_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe30(%rip),%rax
mov    %rax,-0xb8(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x97,%edx
call   1060 <memset@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0xb8(%rbp),%r8
mov    $0x96,%esi
lea    0xe07(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x4c8(%rbp)
cmpq   $0x0,-0x4c8(%rbp)
je     1325 <check_version+0x175>
lea    -0x4c0(%rbp),%rdi
mov    -0x4c8(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1308 <check_version+0x158>
lea    0xdb9(%rip),%rax
mov    %rax,-0x4d0(%rbp)
lea    -0x4c0(%rbp),%rdi
mov    -0x4d0(%rbp),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x4d8(%rbp)
cmpq   $0x0,-0x4d8(%rbp)
je     12f7 <check_version+0x147>
mov    -0x4d8(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x4d8(%rbp)
mov    -0x4d8(%rbp),%rdi
mov    $0x22,%esi
mov    $0x20,%edx
call   1090 <replace_char@plt>
mov    -0x4d8(%rbp),%rdi
call   1080 <strstrip@plt>
mov    %rax,-0x4d8(%rbp)
mov    -0x4d8(%rbp),%rdi
add    $0x2,%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x4dc(%rbp)
mov    -0x4dc(%rbp),%eax
cmp    -0xc(%rbp),%eax
setge  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
jmp    1303 <check_version+0x153>
lea    0xd21(%rip),%rdi
call   1030 <puts@plt>
jmp    1314 <check_version+0x164>
lea    0xd22(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4c8(%rbp),%rdi
call   1040 <pclose@plt>
jmp    1331 <check_version+0x181>
lea    0xd20(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%eax
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_version>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4a8,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %rsp,%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x97,%edx
call   1060 <memset@plt>
lea    0xe31(%rip),%rdx
lea    0xe1b(%rip),%r8
mov    %r15,%rdi
mov    $0x96,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
lea    0xe19(%rip),%rsi
mov    %r15,%rdi
call   10a0 <popen@plt>
test   %rax,%rax
je     12a7 <check_version+0xf7>
mov    %rax,%rbx
lea    0xa0(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1278 <check_version+0xc8>
lea    0xde6(%rip),%rsi
lea    0xa0(%rsp),%rdi
call   10d0 <strstr@plt>
test   %rax,%rax
je     1281 <check_version+0xd1>
mov    %rax,%rbp
add    $0x7,%rbp
mov    %rbp,%rdi
mov    $0x22,%esi
mov    $0x20,%edx
call   1090 <replace_char@plt>
mov    %rbp,%rdi
call   1080 <strstrip@plt>
lea    0x2(%rax),%rdi
call   10c0 <atoi@plt>
xor    %ebp,%ebp
cmp    %r14d,%eax
setge  %bpl
jmp    128f <check_version+0xdf>
lea    0xdb2(%rip),%rdi
jmp    1288 <check_version+0xd8>
lea    0xd97(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    %ebp,%eax
add    $0x4a8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   10b0 <perror@plt>
jmp    1297 <check_version+0xe7>

LLVM IR (-O0):
@.str = [15 x i8] c" -version 2>&1\00"
@.str.1 = [6 x i8] c"%s %s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"version\00"
@.str.4 = [18 x i8] c"Version not found\00"
@.str.5 = [27 x i8] c"Cannot get output command\0A\00"
@.str.6 = [30 x i8] c"Error while executing command\00"
define i32 @check_version(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [151 x i8]
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %7
%13 = getelementptr inbounds [151 x i8], [151 x i8]* %6, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 151)
%15 = getelementptr inbounds [151 x i8], [151 x i8]* %6, i64 0, i64 0
%16 = load i8*, i8** %3
%17 = load i8*, i8** %7
%18 = call i32 @snprintf(i8* %15, i32 150, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %17)
%19 = getelementptr inbounds [151 x i8], [151 x i8]* %6, i64 0, i64 0
%20 = call i32* @popen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %56
23: ; preds = %2
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%25 = load i32*, i32** %9
%26 = call i32* @fgets(i8* %24, i32 1024, i32* %25)
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %51
28: ; preds = %23
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %10
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = load i8*, i8** %10
%31 = call i8* @strstr(i8* %29, i8* %30)
store i8* %31, i8** %11
%32 = load i8*, i8** %11
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %48
34: ; preds = %28
%35 = load i8*, i8** %11
%36 = getelementptr inbounds i8, i8* %35, i64 7
store i8* %36, i8** %11
%37 = load i8*, i8** %11
%38 = call i32 @replace_char(i8* %37, i8 signext 34, i8 signext 32)
%39 = load i8*, i8** %11
%40 = call i8* @strstrip(i8* %39)
store i8* %40, i8** %11
%41 = load i8*, i8** %11
%42 = getelementptr inbounds i8, i8* %41, i64 2
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = load i32, i32* %4
%46 = icmp sge i32 %44, %45
%47 = zext i1 %46 to i32
store i32 %47, i32* %5
br label %50
48: ; preds = %28
%49 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %34
br label %53
51: ; preds = %23
%52 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %50
%54 = load i32*, i32** %9
%55 = call i32 @pclose(i32* %54)
br label %58
56: ; preds = %2
%57 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
@.str = [15 x i8] c" -version 2>&1\00"
@.str.1 = [6 x i8] c"%s %s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"version\00"
@.str.4 = [18 x i8] c"Version not found\00"
@.str.5 = [27 x i8] c"Cannot get output command\0A\00"
@.str.6 = [30 x i8] c"Error while executing command\00"
define i32 @check_version(i8* %0, i32 %1) {
%3 = alloca [151 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [151 x i8], [151 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 151)
%7 = call i32 @snprintf(i8* %5, i32 150, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %32, label %11
11: ; preds = %2
%12 = call i32* @fgets(i8* %8, i32 1024, i32* %9)
%13 = icmp eq i32* %12, null
br i1 %13, label %27, label %14
14: ; preds = %11
%15 = call i8* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %15, i64 7
%19 = call i32 @replace_char(i8* %18, i8 signext 34, i8 signext 32)
%20 = call i8* @strstrip(i8* %18)
%21 = getelementptr inbounds i8, i8* %20, i64 2
%22 = call i32 @atoi(i8* %21)
%23 = icmp sge i32 %22, %1
%24 = zext i1 %23 to i32
br label %29
25: ; preds = %14
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %29
27: ; preds = %11
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %17, %25, %27
%30 = phi i32 [ 0, %27 ], [ %24, %17 ], [ 0, %25 ]
%31 = call i32 @pclose(i32* %9)
br label %34
32: ; preds = %2
%33 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = phi i32 [ %30, %29 ], [ 0, %32 ]
ret i32 %35
}