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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ driver_set_operation(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ fsync(int);
int ftell(int /*<<< orphan*/ *);
int handle_error_bad_file(char *);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int uvz_to_driver() {
  int sim_fd = open("/sys/devices/platform/brtablet/data", O_RDWR);
  if (sim_fd < 0) {
    return -1;
  }
  char buff[500];
  memset(buff, ' ', sizeof(buff));
  FILE *fp = fopen("/etc/brtablet/uvz", "r");
  if (fp == NULL) {
    return -1;
  }
  fseek(fp, 0L, SEEK_END);
  int fim = ftell(fp);
  fseek(fp, 0L, SEEK_SET);
  if (fread(buff, 1, fim, fp) < fim) {
    return handle_error_bad_file("/sys/devices/platform/brtablet/data");
  }
  buff[fim] = 0;
  driver_set_operation("/sys/devices/platform/brtablet/operation", 2, 0);
  write(sim_fd, buff, fim);
  fsync(sim_fd);
  fclose(fp);
  close(sim_fd);
  return (0);
}


x86 assembly (clang, -O0):
<uvz_to_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f9 <uvz_to_driver+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <uvz_to_driver+0x15f>
lea    -0x200(%rbp),%rdi
mov    $0x20,%esi
mov    $0x1f4,%edx
call   1080 <memset@plt>
lea    0xe0e(%rip),%rdi
lea    0xe19(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x208(%rbp)
cmpq   $0x0,-0x208(%rbp)
jne    1243 <uvz_to_driver+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <uvz_to_driver+0x15f>
mov    -0x208(%rbp),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x208(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x20c(%rbp)
mov    -0x208(%rbp),%rdi
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
lea    -0x200(%rbp),%rdi
mov    -0x20c(%rbp),%edx
mov    -0x208(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
cmp    -0x20c(%rbp),%eax
jge    12c5 <uvz_to_driver+0x105>
lea    0xd48(%rip),%rdi
call   1040 <handle_error_bad_file@plt>
mov    %eax,-0x4(%rbp)
jmp    131f <uvz_to_driver+0x15f>
movslq -0x20c(%rbp),%rax
movb   $0x0,-0x200(%rbp,%rax,1)
lea    0xd5d(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1030 <driver_set_operation@plt>
mov    -0x8(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    -0x20c(%rbp),%edx
call   1060 <write@plt>
mov    -0x8(%rbp),%edi
call   10c0 <fsync@plt>
mov    -0x208(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uvz_to_driver>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1f8,%rsp
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
call   10d0 <open@plt>
test   %eax,%eax
js     1274 <uvz_to_driver+0xb4>
mov    %eax,%r15d
mov    %rsp,%rdi
mov    $0x20,%esi
mov    $0x1f4,%edx
call   1080 <memset@plt>
lea    0xe1e(%rip),%rdi
lea    0xe29(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1274 <uvz_to_driver+0xb4>
mov    %rax,%rbx
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %eax,%ebp
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %ebp,%eax
jge    127c <uvz_to_driver+0xbc>
lea    0xd96(%rip),%rdi
call   1040 <handle_error_bad_file@plt>
mov    %eax,%r14d
jmp    12bb <uvz_to_driver+0xfb>
mov    $0xffffffff,%r14d
jmp    12bb <uvz_to_driver+0xfb>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
lea    0xdae(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1030 <driver_set_operation@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1060 <write@plt>
mov    %r15d,%edi
call   10c0 <fsync@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    %r14d,%eax
add    $0x1f8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/devices/platform/brtablet/data\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"/etc/brtablet/uvz\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [41 x i8] c"/sys/devices/platform/brtablet/operation\00"
define i32 @uvz_to_driver() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [500 x i8]
%4 = alloca i32*
%5 = alloca i32
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %50
11: ; preds = %0
%12 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i8 signext 32, i32 500)
%14 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 -1, i32* %1
br label %50
18: ; preds = %11
%19 = load i32*, i32** %4
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i64 0, i32 %20)
%22 = load i32*, i32** %4
%23 = call i32 @ftell(i32* %22)
store i32 %23, i32* %5
%24 = load i32*, i32** %4
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %24, i64 0, i32 %25)
%27 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* %5
%29 = load i32*, i32** %4
%30 = call i32 @fread(i8* %27, i32 1, i32 %28, i32* %29)
%31 = load i32, i32* %5
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %18
%34 = call i32 @handle_error_bad_file(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 %34, i32* %1
br label %50
35: ; preds = %18
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i32 @driver_set_operation(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 2, i32 0)
%40 = load i32, i32* %2
%41 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%42 = load i32, i32* %5
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* %2
%45 = call i32 @fsync(i32 %44)
%46 = load i32*, i32** %4
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
store i32 0, i32* %1
br label %50
50: ; preds = %35, %33, %17, %10
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/devices/platform/brtablet/data\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"/etc/brtablet/uvz\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [41 x i8] c"/sys/devices/platform/brtablet/operation\00"
define i32 @uvz_to_driver() {
%1 = alloca [500 x i8]
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %30, label %5
5: ; preds = %0
%6 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i8 signext 32, i32 500)
%8 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %28, label %10
10: ; preds = %5
%11 = load i32, i32* @SEEK_END
%12 = call i32 @fseek(i32* %8, i64 0, i32 %11)
%13 = call i32 @ftell(i32* %8)
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @fseek(i32* %8, i64 0, i32 %14)
%16 = call i32 @fread(i8* %6, i32 1, i32 %13, i32* %8)
%17 = icmp slt i32 %16, %13
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @handle_error_bad_file(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %28
20: ; preds = %10
%21 = sext i32 %13 to i64
%22 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i32 @driver_set_operation(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 2, i32 0)
%24 = call i32 @write(i32 %3, i8* %6, i32 %13)
%25 = call i32 @fsync(i32 %3)
%26 = call i32 @fclose(i32* %8)
%27 = call i32 @close(i32 %3)
br label %28
28: ; preds = %18, %20, %5
%29 = phi i32 [ -1, %5 ], [ %19, %18 ], [ 0, %20 ]
br label %30
30: ; preds = %0, %28
%31 = phi i32 [ %29, %28 ], [ -1, %0 ]
ret i32 %31
}