#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 utsname {
  int /*<<< orphan*/ release;
  int /*<<< orphan*/ sysname;
};

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int sscanf(int /*<<< orphan*/, char *, int *, int *, int *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
scalar_t__ uname(struct utsname *);
int get_linux_version(void) {
  struct utsname ub;
  int minor = 0, major = 0, rev = 0;
  if (uname(&ub) < 0) {
    fprintf(stderr, "failed: uname() error: %s\n", strerror(errno));
    return -1;
  }
  if (strcmp(ub.sysname, "Linux"))
    return 0;
  if (sscanf(ub.release, "%d.%d.%d", &major, &minor, &rev) != 3)
    return 0;
  if (major < 2 || minor < 0 || rev < 0)
    return 0;
  if (major == 2 && minor < 4)
    return 0;
  if (minor > 999)
    minor = 999;
  if (rev > 999)
    rev = 999;
  return (major * 1000 + minor) * 1000 + rev;
}


x86 assembly (clang, -O0):
<get_linux_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x10(%rbp),%rdi
call   1030 <uname@plt>
cmp    $0x0,%rax
jge    11b8 <get_linux_version+0x68>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <get_linux_version+0x145>
mov    -0xc(%rbp),%edi
lea    0xe59(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11dd <get_linux_version+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    1295 <get_linux_version+0x145>
mov    -0x10(%rbp),%edi
lea    0xe3a(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x14(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1060 <sscanf@plt>
cmp    $0x3,%eax
je     120d <get_linux_version+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    1295 <get_linux_version+0x145>
cmpl   $0x2,-0x18(%rbp)
jl     122b <get_linux_version+0xdb>
cmpl   $0x0,-0x14(%rbp)
jl     122b <get_linux_version+0xdb>
cmpl   $0x0,-0x1c(%rbp)
jge    1237 <get_linux_version+0xe7>
movl   $0x0,-0x4(%rbp)
jmp    1295 <get_linux_version+0x145>
cmpl   $0x2,-0x18(%rbp)
jne    1257 <get_linux_version+0x107>
cmpl   $0x4,-0x14(%rbp)
jge    1257 <get_linux_version+0x107>
movl   $0x0,-0x4(%rbp)
jmp    1295 <get_linux_version+0x145>
cmpl   $0x3e7,-0x14(%rbp)
jle    126b <get_linux_version+0x11b>
movl   $0x3e7,-0x14(%rbp)
cmpl   $0x3e7,-0x1c(%rbp)
jle    127f <get_linux_version+0x12f>
movl   $0x3e7,-0x1c(%rbp)
imul   $0x3e8,-0x18(%rbp),%eax
add    -0x14(%rbp),%eax
imul   $0x3e8,%eax,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_linux_version>:
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0xc(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x18(%rsp),%rdi
call   1030 <uname@plt>
test   %rax,%rax
js     121b <get_linux_version+0xcb>
mov    0x1c(%rsp),%edi
lea    0xe90(%rip),%rsi
call   1040 <strcmp@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    1248 <get_linux_version+0xf8>
mov    0x18(%rsp),%edi
lea    0xe7b(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1060 <sscanf@plt>
cmp    $0x3,%eax
jne    1248 <get_linux_version+0xf8>
mov    0x14(%rsp),%eax
cmp    $0x2,%eax
jl     1248 <get_linux_version+0xf8>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
js     1248 <get_linux_version+0xf8>
mov    0x10(%rsp),%edx
test   %edx,%edx
js     1248 <get_linux_version+0xf8>
cmp    $0x2,%eax
jne    11e6 <get_linux_version+0x96>
cmp    $0x4,%ecx
jb     1248 <get_linux_version+0xf8>
cmp    $0x3e8,%ecx
jb     11fb <get_linux_version+0xab>
movl   $0x3e7,0xc(%rsp)
mov    $0x3e7,%ecx
cmp    $0x3e7,%edx
mov    $0x3e7,%esi
cmovb  %edx,%esi
imul   $0x3e8,%eax,%eax
add    %ecx,%eax
imul   $0x3e8,%eax,%ebx
add    %esi,%ebx
jmp    1248 <get_linux_version+0xf8>
mov    0x2dbe(%rip),%rax
mov    (%rax),%ebx
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdc7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.utsname = type { i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"failed: uname() error: %s\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"Linux\00"
@.str.2 = [9 x i8] c"%d.%d.%d\00"
define i32 @get_linux_version() {
%1 = alloca i32
%2 = alloca %struct.utsname
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%6 = call i64 @uname(%struct.utsname* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 -1, i32* %1
br label %58
13: ; preds = %0
%14 = getelementptr inbounds %struct.utsname, %struct.utsname* %2, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i64 @strcmp(i32 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 0, i32* %1
br label %58
19: ; preds = %13
%20 = getelementptr inbounds %struct.utsname, %struct.utsname* %2, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @sscanf(i32 %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %3, i32* %5)
%23 = icmp ne i32 %22, 3
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 0, i32* %1
br label %58
25: ; preds = %19
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 2
br i1 %27, label %34, label %28
28: ; preds = %25
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31, %28, %25
store i32 0, i32* %1
br label %58
35: ; preds = %31
%36 = load i32, i32* %4
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* %3
%40 = icmp slt i32 %39, 4
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 0, i32* %1
br label %58
42: ; preds = %38, %35
%43 = load i32, i32* %3
%44 = icmp sgt i32 %43, 999
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 999, i32* %3
br label %46
46: ; preds = %45, %42
%47 = load i32, i32* %5
%48 = icmp sgt i32 %47, 999
br i1 %48, label %49, label %50
49: ; preds = %46
store i32 999, i32* %5
br label %50
50: ; preds = %49, %46
%51 = load i32, i32* %4
%52 = mul nsw i32 %51, 1000
%53 = load i32, i32* %3
%54 = add nsw i32 %52, %53
%55 = mul nsw i32 %54, 1000
%56 = load i32, i32* %5
%57 = add nsw i32 %55, %56
store i32 %57, i32* %1
br label %58
58: ; preds = %50, %41, %34, %24, %18, %8
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
%struct.utsname = type { i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"failed: uname() error: %s\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"Linux\00"
@.str.2 = [9 x i8] c"%d.%d.%d\00"
define i32 @get_linux_version() {
%1 = alloca %struct.utsname
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.utsname* %1 to i8*
%6 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = call i64 @uname(%struct.utsname* %1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %50
16: ; preds = %0
%17 = getelementptr inbounds %struct.utsname, %struct.utsname* %1, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i64 @strcmp(i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %50
21: ; preds = %16
%22 = getelementptr inbounds %struct.utsname, %struct.utsname* %1, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @sscanf(i32 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %2, i32* %4)
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %50
26: ; preds = %21
%27 = load i32, i32* %3
%28 = icmp slt i32 %27, 2
%29 = load i32, i32* %2
%30 = icmp slt i32 %29, 0
%31 = select i1 %28, i1 true, i1 %30
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
%34 = select i1 %31, i1 true, i1 %33
br i1 %34, label %50, label %35
35: ; preds = %26
%36 = icmp eq i32 %27, 2
%37 = icmp ult i32 %29, 4
%38 = select i1 %36, i1 %37, i1 false
br i1 %38, label %50, label %39
39: ; preds = %35
%40 = icmp ugt i32 %29, 999
br i1 %40, label %41, label %42
41: ; preds = %39
store i32 999, i32* %2
br label %42
42: ; preds = %41, %39
%43 = phi i32 [ 999, %41 ], [ %29, %39 ]
%44 = icmp ult i32 %32, 999
%45 = select i1 %44, i32 %32, i32 999
%46 = mul nsw i32 %27, 1000
%47 = add nsw i32 %43, %46
%48 = mul nsw i32 %47, 1000
%49 = add nsw i32 %48, %45
br label %50
50: ; preds = %35, %26, %21, %16, %42, %11
%51 = phi i32 [ -1, %11 ], [ %49, %42 ], [ 0, %16 ], [ 0, %21 ], [ 0, %26 ], [ 0, %35 ]
ret i32 %51
}