#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*/ sysfs_name;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int strlen(char *);
void v4lcontrol_get_dmi_string(const char *sysfs_prefix, const char *string,
                               char *buf, int size) {
  char sysfs_name[512];
  snprintf(sysfs_name, sizeof(sysfs_name), "%s/sys/class/dmi/id/%s",
           sysfs_prefix, string);
  FILE *f = fopen(sysfs_name, "r");
  if (!f) {
    snprintf(sysfs_name, sizeof(sysfs_name), "%s/sys/devices/virtual/dmi/id/%s",
             sysfs_prefix, string);
    f = fopen(sysfs_name, "r");
    if (!f) {
      buf[0] = 0;
      return;
    }
  }
  char *s = fgets(buf, size, f);
  if (s)
    s[strlen(s) - 1] = 0;
  fclose(f);
}


x86 assembly (clang, -O0):
<v4lcontrol_get_dmi_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xe7b(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    120b <v4lcontrol_get_dmi_string+0xbb>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xe4c(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    1206 <v4lcontrol_get_dmi_string+0xb6>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    126c <v4lcontrol_get_dmi_string+0x11c>
jmp    120b <v4lcontrol_get_dmi_string+0xbb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x228(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
je     1260 <v4lcontrol_get_dmi_string+0x110>
mov    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x238(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x228(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<v4lcontrol_get_dmi_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xe8e(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rcx
mov    %r12,%r8
call   1050 <snprintf@plt>
lea    0xe88(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11d3 <v4lcontrol_get_dmi_string+0x83>
lea    0xe73(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rcx
mov    %r12,%r8
call   1050 <snprintf@plt>
lea    0xe54(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1210 <v4lcontrol_get_dmi_string+0xc0>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f8 <v4lcontrol_get_dmi_string+0xa8>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbp,%rdi
call   1030 <fclose@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%r15)
jmp    1200 <v4lcontrol_get_dmi_string+0xb0>

LLVM IR (-O0):
@.str = [23 x i8] c"%s/sys/class/dmi/id/%s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"%s/sys/devices/virtual/dmi/id/%s\00"
define void @v4lcontrol_get_dmi_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca i32*
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = load i8*, i8** %6
%15 = call i32 @snprintf(i8* %12, i32 512, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp ne i32* %18, null
br i1 %19, label %33, label %20
20: ; preds = %4
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = call i32 @snprintf(i8* %21, i32 512, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* %23)
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %10
%27 = load i32*, i32** %10
%28 = icmp ne i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %20
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 0
store i8 0, i8* %31
br label %50
32: ; preds = %20
br label %33
33: ; preds = %32, %4
%34 = load i8*, i8** %7
%35 = load i32, i32* %8
%36 = load i32*, i32** %10
%37 = call i8* @fgets(i8* %34, i32 %35, i32* %36)
store i8* %37, i8** %11
%38 = load i8*, i8** %11
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %47
40: ; preds = %33
%41 = load i8*, i8** %11
%42 = load i8*, i8** %11
%43 = call i32 @strlen(i8* %42)
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %40, %33
%48 = load i32*, i32** %10
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %47, %29
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"%s/sys/class/dmi/id/%s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"%s/sys/devices/virtual/dmi/id/%s\00"
define void @v4lcontrol_get_dmi_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%12 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %10
store i8 0, i8* %2
br label %26
15: ; preds = %10, %4
%16 = phi i32* [ %8, %4 ], [ %12, %10 ]
%17 = call i8* @fgets(i8* %2, i32 %3, i32* %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = call i32 @strlen(i8* %17)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %17, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %15
%25 = call i32 @fclose(i32* %16)
br label %26
26: ; preds = %24, %14
ret void
}