#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;
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ stderr;
int read_position(int *x, int *y) {
  char buf[32];
  int fd, ret;
  fd = open("/sys/devices/platform/hdaps/position", O_RDONLY);
  if (fd < 0) {
    perror("open");
    return fd;
  }
  ret = read(fd, buf, 32);
  if (ret < 0) {
    perror("read");
    goto out;
  } else if (ret == 0) {
    fprintf(stderr, "error: unexpectedly read zero!\n");
    ret = 1;
    goto out;
  }
  ret = 0;
  if (sscanf(buf, "(%d,%d)\n", x, y) != 2)
    ret = 1;
out:
  if (close(fd))
    perror("close");
  return ret;
}


x86 assembly (clang, -O0):
<read_position>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11a9 <read_position+0x49>
lea    0xe8c(%rip),%rdi
call   1080 <perror@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1260 <read_position+0x100>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    11d8 <read_position+0x78>
lea    0xe5c(%rip),%rdi
call   1080 <perror@plt>
jmp    123c <read_position+0xdc>
cmpl   $0x0,-0x48(%rbp)
jne    1203 <read_position+0xa3>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x48(%rbp)
jmp    123c <read_position+0xdc>
jmp    1208 <read_position+0xa8>
movl   $0x0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe2d(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x2,%eax
je     1237 <read_position+0xd7>
movl   $0x1,-0x48(%rbp)
jmp    123c <read_position+0xdc>
mov    -0x44(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     125a <read_position+0xfa>
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_position>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11f3 <read_position+0x93>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x20,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11fe <read_position+0x9e>
je     11c0 <read_position+0x60>
lea    0xea8(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <sscanf@plt>
xor    %ebp,%ebp
cmp    $0x2,%eax
setne  %bpl
jmp    11da <read_position+0x7a>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %rax,%rax
jne    120e <read_position+0xae>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2b(%rip),%rdi
mov    %ebx,%ebp
jmp    1215 <read_position+0xb5>
mov    %eax,%ebp
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
jmp    11da <read_position+0x7a>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
jmp    11e6 <read_position+0x86>

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/devices/platform/hdaps/position\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"error: unexpectedly read zero!\0A\00"
@.str.4 = [9 x i8] c"(%d,%d)\0A\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @read_position(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [32 x i8]
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %7
store i32 %15, i32* %3
br label %47
16: ; preds = %2
%17 = load i32, i32* %7
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 32)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %39
24: ; preds = %16
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %8
br label %39
30: ; preds = %24
br label %31
31: ; preds = %30
store i32 0, i32* %8
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%33 = load i32*, i32** %4
%34 = load i32*, i32** %5
%35 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32* %33, i32* %34)
%36 = icmp ne i32 %35, 2
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 1, i32* %8
br label %38
38: ; preds = %37, %31
br label %39
39: ; preds = %38, %27, %22
%40 = load i32, i32* %7
%41 = call i64 @close(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = load i32, i32* %8
store i32 %46, i32* %3
br label %47
47: ; preds = %45, %13
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/devices/platform/hdaps/position\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"error: unexpectedly read zero!\0A\00"
@.str.4 = [9 x i8] c"(%d,%d)\0A\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @read_position(i32* %0, i32* %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%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 slt i32 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = call i32 @read(i32 %6, i8* %4, i32 32)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %22
13: ; preds = %8
%14 = icmp eq i32 %9, 0
br i1 %14, label %15, label %18
15: ; preds = %13
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %22
18: ; preds = %13
%19 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32* %0, i32* %1)
%20 = icmp ne i32 %19, 2
%21 = zext i1 %20 to i32
br label %22
22: ; preds = %18, %15, %11
%23 = phi i32 [ %9, %11 ], [ 1, %15 ], [ %21, %18 ]
%24 = tail call i64 @close(i32 %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22, %2
%27 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %22 ]
%28 = phi i32 [ %6, %2 ], [ %23, %22 ]
%29 = tail call i32 @perror(i8* %27)
br label %30
30: ; preds = %26, %22
%31 = phi i32 [ %23, %22 ], [ %28, %26 ]
ret i32 %31
}