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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *strchr(char *, char);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int get_system_vpd(char *machine_serial, char *machine_type,
                   char **machine_model) {
  int device_fd;
  int rc;
  int start_index = 0;
  char serial[16] = {0};
  char model[16] = {0};
  char *temp;
  device_fd = open("/proc/device-tree/"
                   "system-id",
                   O_RDONLY);
  if (device_fd < 0)
    return -1;
  rc = read(device_fd, serial, 16);
  close(device_fd);
  if (rc <= 0)
    return -1;
  if (strlen(serial) > 5)
    start_index = strlen(serial) - 5;
  strncpy(machine_serial, serial + start_index, 8);
  device_fd = open("/proc/device-tree/"
                   "model",
                   O_RDONLY);
  if (device_fd < 0)
    return -1;
  rc = read(device_fd, model, 16);
  close(device_fd);
  if (rc <= 0)
    return -1;
  temp = model;
  if (strchr(model, ',') != NULL)
    temp = strchr(model, ',') + 1;
  strncpy(machine_type, temp, 8 + 1);
  *machine_model = strchr(machine_type, '-');
  if (*machine_model == NULL)
    return -1;
  **machine_model = '\0';
  (*machine_model)++;
  return 0;
}


x86 assembly (clang, -O0):
<get_system_vpd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d9 <get_system_vpd+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <get_system_vpd+0x1ce>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1080 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jg     120b <get_system_vpd+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <get_system_vpd+0x1ce>
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x5,%eax
jle    122c <get_system_vpd+0xbc>
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x5,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
lea    0xdc7(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1273 <get_system_vpd+0x103>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <get_system_vpd+0x1ce>
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
call   1080 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jg     12a5 <get_system_vpd+0x135>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <get_system_vpd+0x1ce>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x2c,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     12db <get_system_vpd+0x16b>
lea    -0x50(%rbp),%rdi
mov    $0x2c,%esi
call   1050 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    131f <get_system_vpd+0x1af>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <get_system_vpd+0x1ce>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_system_vpd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r13
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x2e5e(%rip),%r14
mov    (%r14),%esi
lea    0xe6c(%rip),%rdi
call   1080 <open@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     127c <get_system_vpd+0x11c>
mov    %eax,%ebp
mov    %rbx,0x18(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
jle    127c <get_system_vpd+0x11c>
mov    %rsp,%rdi
call   1040 <strlen@plt>
cmp    $0x6,%eax
jl     11ea <get_system_vpd+0x8a>
mov    %rsp,%rdi
call   1040 <strlen@plt>
add    $0xfffffffb,%eax
cltq
jmp    11ec <get_system_vpd+0x8c>
xor    %eax,%eax
lea    (%rsp,%rax,1),%rsi
mov    %r13,%rdi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    (%r14),%esi
lea    0xe15(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     127c <get_system_vpd+0x11c>
mov    %eax,%ebp
lea    0x20(%rsp),%r13
mov    %eax,%edi
mov    %r13,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
jle    127c <get_system_vpd+0x11c>
mov    %r13,%rdi
mov    $0x2c,%esi
call   1050 <strchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %r13,%rsi
mov    %r15,%rdi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
mov    0x18(%rsp),%rcx
mov    %rax,(%rcx)
test   %rax,%rax
je     127c <get_system_vpd+0x11c>
movb   $0x0,(%rax)
addq   $0x1,(%rcx)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"/proc/device-tree/system-id\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"/proc/device-tree/model\00"
define i32 @get_system_vpd(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [16 x i8]
%12 = alloca [16 x i8]
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i32 0, i32* %10
%14 = bitcast [16 x i8]* %11 to i8*
%15 = bitcast [16 x i8]* %12 to i8*
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %85
21: ; preds = %3
%22 = load i32, i32* %8
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 16)
store i32 %24, i32* %9
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %9
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 -1, i32* %4
br label %85
30: ; preds = %21
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = icmp sgt i32 %32, 5
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = sub nsw i32 %36, 5
store i32 %37, i32* %10
br label %38
38: ; preds = %34, %30
%39 = load i8*, i8** %5
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = call i32 @strncpy(i8* %39, i8* %43, i32 8)
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %45)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %38
store i32 -1, i32* %4
br label %85
50: ; preds = %38
%51 = load i32, i32* %8
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 16)
store i32 %53, i32* %9
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %9
%57 = icmp sle i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %50
store i32 -1, i32* %4
br label %85
59: ; preds = %50
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
store i8* %60, i8** %13
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%62 = call i8* @strchr(i8* %61, i8 signext 44)
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %68
64: ; preds = %59
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%66 = call i8* @strchr(i8* %65, i8 signext 44)
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8* %67, i8** %13
br label %68
68: ; preds = %64, %59
%69 = load i8*, i8** %6
%70 = load i8*, i8** %13
%71 = call i32 @strncpy(i8* %69, i8* %70, i32 9)
%72 = load i8*, i8** %6
%73 = call i8* @strchr(i8* %72, i8 signext 45)
%74 = load i8**, i8*** %7
store i8* %73, i8** %74
%75 = load i8**, i8*** %7
%76 = load i8*, i8** %75
%77 = icmp eq i8* %76, null
br i1 %77, label %78, label %79
78: ; preds = %68
store i32 -1, i32* %4
br label %85
79: ; preds = %68
%80 = load i8**, i8*** %7
%81 = load i8*, i8** %80
store i8 0, i8* %81
%82 = load i8**, i8*** %7
%83 = load i8*, i8** %82
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %82
store i32 0, i32* %4
br label %85
85: ; preds = %79, %78, %58, %49, %29, %20
%86 = load i32, i32* %4
ret i32 %86
}

LLVM IR (-O2):
@.str = [28 x i8] c"/proc/device-tree/system-id\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"/proc/device-tree/model\00"
define i32 @get_system_vpd(i8* %0, i8* %1, i8** nocapture %2) {
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %44, label %11
11: ; preds = %3
%12 = call i32 @read(i32 %9, i8* %6, i32 16)
%13 = tail call i32 @close(i32 %9)
%14 = icmp slt i32 %12, 1
br i1 %14, label %44, label %15
15: ; preds = %11
%16 = call i32 @strlen(i8* %6)
%17 = icmp sgt i32 %16, 5
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i32 @strlen(i8* %6)
%20 = add nsw i32 %19, -5
%21 = sext i32 %20 to i64
br label %22
22: ; preds = %18, %15
%23 = phi i64 [ %21, %18 ], [ 0, %15 ]
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %23
%25 = call i32 @strncpy(i8* %0, i8* %24, i32 8)
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %44, label %29
29: ; preds = %22
%30 = call i32 @read(i32 %27, i8* %7, i32 16)
%31 = call i32 @close(i32 %27)
%32 = icmp slt i32 %30, 1
br i1 %32, label %44, label %33
33: ; preds = %29
%34 = call i8* @strchr(i8* %7, i8 signext 44)
%35 = icmp eq i8* %34, null
%36 = getelementptr inbounds i8, i8* %34, i64 1
%37 = select i1 %35, i8* %7, i8* %36
%38 = call i32 @strncpy(i8* %1, i8* %37, i32 9)
%39 = call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 45)
store i8* %39, i8** %2
%40 = icmp eq i8* %39, null
br i1 %40, label %44, label %41
41: ; preds = %33
store i8 0, i8* %39
%42 = load i8*, i8** %2
%43 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %43, i8** %2
br label %44
44: ; preds = %33, %29, %22, %11, %3, %41
%45 = phi i32 [ 0, %41 ], [ -1, %3 ], [ -1, %11 ], [ -1, %22 ], [ -1, %29 ], [ -1, %33 ]
ret i32 %45
}