#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ in;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ isspace(char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int test_kernel_bit_width(void) {
  char in[512], *ptr;
  int num = 0, fd;
  ssize_t ret;
  fd = open("/proc/kallsyms", O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  ret = read(fd, in, sizeof(in));
  if (ret <= 0) {
    perror("read");
    exit(1);
  }
  close(fd);
  ptr = in;
  while (!isspace(*ptr)) {
    num++;
    ptr++;
  }
  return num * 4;
}


x86 assembly (clang, -O0):
<test_kernel_bit_width>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x20c(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x210(%rbp)
cmpl   $0xffffffff,-0x210(%rbp)
jne    11b3 <test_kernel_bit_width+0x53>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x210(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jg     11f5 <test_kernel_bit_width+0x95>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x210(%rbp),%edi
call   1030 <close@plt>
lea    -0x200(%rbp),%rax
mov    %rax,-0x208(%rbp)
mov    -0x208(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1232 <test_kernel_bit_width+0xd2>
jmp    1258 <test_kernel_bit_width+0xf8>
mov    -0x20c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20c(%rbp)
mov    -0x208(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x208(%rbp)
jmp    120e <test_kernel_bit_width+0xae>
mov    -0x20c(%rbp),%eax
shl    $0x2,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_kernel_bit_width>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11df <test_kernel_bit_width+0x7f>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11e8 <test_kernel_bit_width+0x88>
mov    %ebx,%edi
call   1030 <close@plt>
movsbl (%rsp),%edi
call   1040 <isspace@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11d3 <test_kernel_bit_width+0x73>
lea    0x1(%rsp),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1040 <isspace@plt>
add    $0x1,%rbx
add    $0x4,%ebp
test   %eax,%eax
je     11c0 <test_kernel_bit_width+0x60>
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe29(%rip),%rdi
jmp    11ef <test_kernel_bit_width+0x8f>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/kallsyms\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
define i32 @test_kernel_bit_width() {
%1 = alloca [512 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 0, i32* %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %4
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%16 = call i64 @read(i32 %14, i8* %15, i32 512)
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = icmp sle i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
store i8* %25, i8** %2
br label %26
26: ; preds = %32, %22
%27 = load i8*, i8** %2
%28 = load i8, i8* %27
%29 = call i32 @isspace(i8 signext %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
%35 = load i8*, i8** %2
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %2
br label %26
37: ; preds = %26
%38 = load i32, i32* %3
%39 = mul nsw i32 %38, 4
ret i32 %39
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/kallsyms\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
define i32 @test_kernel_bit_width() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i64 @read(i32 %4, i8* %2, i32 512)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = tail call i32 @close(i32 %4)
%17 = load i8, i8* %2
%18 = tail call i32 @isspace(i8 signext %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %15, %20
%21 = phi i8* [ %24, %20 ], [ %2, %15 ]
%22 = phi i32 [ %23, %20 ], [ 0, %15 ]
%23 = add nuw nsw i32 %22, 1
%24 = getelementptr inbounds i8, i8* %21, i64 1
%25 = load i8, i8* %24
%26 = tail call i32 @isspace(i8 signext %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %20, label %28
28: ; preds = %20
%29 = shl i32 %23, 2
br label %30
30: ; preds = %28, %15
%31 = phi i32 [ 0, %15 ], [ %29, %28 ]
ret i32 %31
}