#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 */
scalar_t__ SDCheck();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int getSdSN(char *sn) {
  char cid[33];
  sn[0] = '\0';
  if (SDCheck() == 0) {
    FILE *fp;
    fp = fopen("/sys/block/mmcblk0/device/cid", "r");
    if (fp != NULL) {
      if (fgets(cid, 33, fp) != NULL) {
        strncpy(sn, &cid[18], 8);
        sn[8] = '\0';
      }
      fclose(fp);
      return sn[0] == '\0';
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<getSdSN>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    $0x0,%al
call   1050 <SDCheck@plt>
cmp    $0x0,%rax
jne    11f8 <getSdSN+0xa8>
lea    0xe85(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     11f3 <getSdSN+0xa3>
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    $0x21,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11d0 <getSdSN+0x80>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
add    $0x12,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x8(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <getSdSN+0xaf>
jmp    11f8 <getSdSN+0xa8>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getSdSN>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movb   $0x0,(%rdi)
xor    %eax,%eax
call   1050 <SDCheck@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11c6 <getSdSN+0x76>
lea    0xe8a(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c6 <getSdSN+0x76>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x21,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11b5 <getSdSN+0x65>
lea    0x12(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x8(%rbx)
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
cmpb   $0x0,(%rbx)
sete   %bpl
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"/sys/block/mmcblk0/device/cid\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getSdSN(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [33 x i8]
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 0
store i8 0, i8* %7
%8 = call i64 (...) @SDCheck()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %35
10: ; preds = %1
%11 = call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %34
14: ; preds = %10
%15 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%16 = load i32*, i32** %5
%17 = call i32* @fgets(i8* %15, i32 33, i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 18
%22 = call i32 @strncpy(i8* %20, i8* %21, i32 8)
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 8
store i8 0, i8* %24
br label %25
25: ; preds = %19, %14
%26 = load i32*, i32** %5
%27 = call i32 @fclose(i32* %26)
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
%33 = zext i1 %32 to i32
store i32 %33, i32* %2
br label %36
34: ; preds = %10
br label %35
35: ; preds = %34, %1
store i32 -1, i32* %2
br label %36
36: ; preds = %35, %25
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [30 x i8] c"/sys/block/mmcblk0/device/cid\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getSdSN(i8* %0) {
%2 = alloca [33 x i8]
%3 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 0
store i8 0, i8* %0
%4 = tail call i64 (...) @SDCheck()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %1
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = call i32* @fgets(i8* %3, i32 33, i32* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 18
%14 = call i32 @strncpy(i8* %0, i8* %13, i32 8)
%15 = getelementptr inbounds i8, i8* %0, i64 8
store i8 0, i8* %15
br label %16
16: ; preds = %9, %12
%17 = call i32 @fclose(i32* %7)
%18 = load i8, i8* %0
%19 = icmp eq i8 %18, 0
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %6, %1, %16
%22 = phi i32 [ %20, %16 ], [ -1, %1 ], [ -1, %6 ]
ret i32 %22
}