#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*/ uint64_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strtoull(char *, int /*<<< orphan*/, int /*<<< orphan*/);
uint64_t i915_type_id(void) {
  char buf[1024];
  int fd, n;
  fd = open("/sys/bus/event_source/devices/i915/type", 0);
  if (fd < 0) {
    n = -1;
  } else {
    n = read(fd, buf, sizeof(buf) - 1);
    close(fd);
  }
  if (n < 0)
    return 0;
  buf[n] = '\0';
  return strtoull(buf, 0, 0);
}


x86 assembly (clang, -O0):
<i915_type_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xeae(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    117b <i915_type_id+0x3b>
movl   $0xffffffff,-0x418(%rbp)
ff ff ff
jmp    11a3 <i915_type_id+0x63>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x418(%rbp)
mov    -0x414(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x418(%rbp)
jge    11bc <i915_type_id+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    11de <i915_type_id+0x9e>
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <strtoull@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i915_type_id>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xeae(%rip),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     1193 <i915_type_id+0x53>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
test   %ebx,%ebx
js     1193 <i915_type_id+0x53>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"/sys/bus/event_source/devices/i915/type\00"
define i32 @i915_type_id() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 -1, i32* %4
br label %15
9: ; preds = %0
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 1023)
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = call i32 @close(i32 %13)
br label %15
15: ; preds = %9, %8
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 0, i32* %1
br label %25
19: ; preds = %15
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%24 = call i32 @strtoull(i8* %23, i32 0, i32 0)
store i32 %24, i32* %1
br label %25
25: ; preds = %19, %18
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/bus/event_source/devices/i915/type\00"
define i32 @i915_type_id() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %0
%6 = call i32 @read(i32 %3, i8* %2, i32 1023)
%7 = tail call i32 @close(i32 %3)
%8 = icmp slt i32 %6, 0
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = zext i32 %6 to i64
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 @strtoull(i8* %2, i32 0, i32 0)
br label %13
13: ; preds = %0, %5, %9
%14 = phi i32 [ %12, %9 ], [ 0, %5 ], [ 0, %0 ]
ret i32 %14
}