#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ *strstr(char *, char *);
void pi_get_cpu_serial(char *buf) {
  FILE *cpuSerialFile;
  char serial[17];
  cpuSerialFile = fopen("/proc/cpuinfo", "r");
  char buffer[64];
  while (fgets(buffer, 64, (FILE *)cpuSerialFile) != NULL) {
    if (strstr(buffer, "Serial") != NULL) {
      memcpy(serial, &buffer[10], 16);
      serial[16] = '\0';
    }
  }
  fclose(cpuSerialFile);
  strncpy(buf, serial, 17);
}


x86 assembly (clang, -O0):
<pi_get_cpu_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11d8 <pi_get_cpu_serial+0x78>
lea    -0x70(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     11d3 <pi_get_cpu_serial+0x73>
lea    -0x30(%rbp),%rdi
lea    -0x70(%rbp),%rsi
add    $0xa,%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
movb   $0x0,-0x20(%rbp)
jmp    1183 <pi_get_cpu_serial+0x23>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x11,%edx
call   1030 <strncpy@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pi_get_cpu_serial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
lea    0xe88(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
lea    0x20(%rsp),%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11fc <pi_get_cpu_serial+0x9c>
lea    0x2a(%rsp),%r15
lea    0xe66(%rip),%r12
lea    0x20(%rsp),%rbx
mov    %rsp,%rbp
jmp    11d5 <pi_get_cpu_serial+0x75>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11fc <pi_get_cpu_serial+0x9c>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11c0 <pi_get_cpu_serial+0x60>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
movb   $0x0,0x10(%rsp)
jmp    11c0 <pi_get_cpu_serial+0x60>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x11,%edx
call   1030 <strncpy@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"Serial\00"
define void @pi_get_cpu_serial(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [17 x i8]
%5 = alloca [64 x i8]
store i8* %0, i8** %2
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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
br label %7
7: ; preds = %21, %1
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%9 = load i32*, i32** %3
%10 = call i32* @fgets(i8* %8, i32 64, i32* %9)
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %22
12: ; preds = %7
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%14 = call i32* @strstr(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 10
%19 = call i32 @memcpy(i8* %17, i8* %18, i32 16)
%20 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 16
store i8 0, i8* %20
br label %21
21: ; preds = %16, %12
br label %7
22: ; preds = %7
%23 = load i32*, i32** %3
%24 = call i32 @fclose(i32* %23)
%25 = load i8*, i8** %2
%26 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%27 = call i32 @strncpy(i8* %25, i8* %26, i32 17)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"Serial\00"
define void @pi_get_cpu_serial(i8* %0) {
%2 = alloca [17 x i8]
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = call i32* @fgets(i8* %6, i32 64, i32* %5)
%8 = icmp eq i32* %7, null
br i1 %8, label %20, label %9
9: ; preds = %1
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 10
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 16
br label %12
12: ; preds = %9, %17
%13 = call i32* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @memcpy(i8* %4, i8* %10, i32 16)
store i8 0, i8* %11
br label %17
17: ; preds = %15, %12
%18 = call i32* @fgets(i8* %6, i32 64, i32* %5)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %12
20: ; preds = %17, %1
%21 = call i32 @fclose(i32* %5)
%22 = call i32 @strncpy(i8* %0, i8* %4, i32 17)
ret void
}