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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long *);
unsigned long get_default_dscr(void) {
  int fd = -1, ret;
  char buf[16];
  unsigned long val;
  if (fd == -1) {
    fd = open("/sys/devices/system/cpu/dscr_default", O_RDONLY);
    if (fd == -1) {
      perror("open() failed");
      exit(1);
    }
  }
  memset(buf, 0, sizeof(buf));
  lseek(fd, 0, SEEK_SET);
  ret = read(fd, buf, sizeof(buf));
  if (ret == -1) {
    perror("read() failed");
    exit(1);
  }
  sscanf(buf, "%lx", &val);
  close(fd);
  return val;
}


x86 assembly (clang, -O0):
<get_default_dscr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d6 <get_default_dscr+0x56>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d1 <get_default_dscr+0x51>
lea    0xe63(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11d6 <get_default_dscr+0x56>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    122d <get_default_dscr+0xad>
lea    0xe15(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x20(%rbp),%rdi
lea    0xe09(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_default_dscr>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1207 <get_default_dscr+0x87>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1210 <get_default_dscr+0x90>
lea    0xe5d(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1070 <sscanf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe17(%rip),%rdi
jmp    1217 <get_default_dscr+0x97>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/devices/system/cpu/dscr_default\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@SEEK_SET = global i32 0
@.str.2 = [14 x i8] c"read() failed\00"
@.str.3 = [4 x i8] c"%lx\00"
define i64 @get_default_dscr() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16 x i8]
%4 = alloca i64
store i32 -1, i32* %1
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %16
7: ; preds = %0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %7
br label %16
16: ; preds = %15, %0
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 16)
%19 = load i32, i32* %1
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %19, i32 0, i32 %20)
%22 = load i32, i32* %1
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 16)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %16
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %4)
%33 = load i32, i32* %1
%34 = call i32 @close(i32 %33)
%35 = load i64, i64* %4
ret i64 %35
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/devices/system/cpu/dscr_default\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@SEEK_SET = global i32 0
@.str.2 = [14 x i8] c"read() failed\00"
@.str.3 = [4 x i8] c"%lx\00"
define i64 @get_default_dscr() {
%1 = alloca [16 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @memset(i8* %3, i32 0, i32 16)
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %6, i32 0, i32 %13)
%15 = call i32 @read(i32 %6, i8* %3, i32 16)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %2)
%22 = call i32 @close(i32 %6)
%23 = load i64, i64* %2
ret i64 %23
}