#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*/ FILE;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
long strtol(char *, int /*<<< orphan*/ *, int);
int turris_detect_version() {
  char snc[9];
  FILE *atshacmd = popen("atsha204cmd serial-number", "r");
  char *ret = fgets(snc, 9, atshacmd);
  pclose(atshacmd);
  if (ret == NULL)
    return 0;
  long int sn = strtol(snc, NULL, 16);
  if (sn == 0)
    return 0;
  if (sn > 0x00000005)
    return 11;
  return 10;
}


x86 assembly (clang, -O0):
<turris_detect_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
lea    -0xd(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x9,%esi
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1195 <turris_detect_version+0x55>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <turris_detect_version+0xa0>
lea    -0xd(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c2 <turris_detect_version+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <turris_detect_version+0xa0>
cmpq   $0x5,-0x28(%rbp)
jle    11d9 <turris_detect_version+0x99>
movl   $0xb,-0x4(%rbp)
jmp    11e0 <turris_detect_version+0xa0>
movl   $0xa,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<turris_detect_version>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xeb2(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x9,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %r14,%r14
je     11a9 <turris_detect_version+0x69>
xor    %ebx,%ebx
lea    0xf(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x6,%rcx
setge  %al
or     $0xa,%eax
test   %rcx,%rcx
cmove  %ebx,%eax
jmp    11ab <turris_detect_version+0x6b>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"atsha204cmd serial-number\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @turris_detect_version() {
%1 = alloca i32
%2 = alloca [9 x i8]
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = call i32* @popen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%8 = load i32*, i32** %3
%9 = call i8* @fgets(i8* %7, i32 9, i32* %8)
store i8* %9, i8** %4
%10 = load i32*, i32** %3
%11 = call i32 @pclose(i32* %10)
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %0
store i32 0, i32* %1
br label %26
15: ; preds = %0
%16 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%17 = call i64 @strtol(i8* %16, i32* null, i32 16)
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %1
br label %26
21: ; preds = %15
%22 = load i64, i64* %5
%23 = icmp sgt i64 %22, 5
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 11, i32* %1
br label %26
25: ; preds = %21
store i32 10, i32* %1
br label %26
26: ; preds = %25, %24, %20, %14
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [26 x i8] c"atsha204cmd serial-number\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @turris_detect_version() {
%1 = alloca [9 x i8]
%2 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = call i8* @fgets(i8* %2, i32 9, i32* %3)
%5 = call i32 @pclose(i32* %3)
%6 = icmp eq i8* %4, null
br i1 %6, label %13, label %7
7: ; preds = %0
%8 = call i64 @strtol(i8* nocapture %2, i32* null, i32 16)
%9 = icmp eq i64 %8, 0
%10 = icmp sgt i64 %8, 5
%11 = select i1 %10, i32 11, i32 10
%12 = select i1 %9, i32 0, i32 %11
br label %13
13: ; preds = %0, %7
%14 = phi i32 [ %12, %7 ], [ 0, %0 ]
ret i32 %14
}