#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = 0, ret = 0;
  off_t offset = 32;
  char buf[32] = {0};
  int i = 0;
  int *timeout_p = (int *)buf;
  *timeout_p = 5;
  fd = open("/dev/dpsctl0", O_RDWR);
  if (fd < 0) {
    perror("Opening /dev/dpsctl");
    return -1;
  }
  while (i < 100) {
    ret = read(fd, buf, 32);
    if (ret < 0) {
      perror("Read error");
    }
    printf("Reading\n");
    i++;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x20,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0x0,-0x44(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
movl   $0x5,(%rax)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ee <main+0x8e>
lea    0xe30(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <main+0xf0>
jmp    11f3 <main+0x93>
cmpl   $0x64,-0x44(%rbp)
jge    1241 <main+0xe1>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1227 <main+0xc7>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
lea    0xdfe(%rip),%rdi
call   1030 <printf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11f3 <main+0x93>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x5,(%rsp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f0 <main+0xa0>
mov    %eax,%ebx
mov    $0x64,%ebp
mov    %rsp,%r14
lea    0xe88(%rip),%r15
lea    0xe8c(%rip),%r12
jmp    11bd <main+0x6d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%ebp
je     11da <main+0x8a>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x20,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11b0 <main+0x60>
mov    %r15,%rdi
call   1070 <perror@plt>
jmp    11b0 <main+0x60>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e3 <main+0x93>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/dpsctl0\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Opening /dev/dpsctl\00"
@.str.2 = [11 x i8] c"Read error\00"
@.str.3 = [9 x i8] c"Reading\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32 x i8]
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 32, i32* %8
%12 = bitcast [32 x i8]* %9 to i8*
store i32 0, i32* %10
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %11
%15 = load i32*, i32** %11
store i32 5, i32* %15
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
22: ; preds = %2
br label %23
23: ; preds = %34, %22
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 100
br i1 %25, label %26, label %38
26: ; preds = %23
%27 = load i32, i32* %6
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 32)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %26
%35 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
br label %23
38: ; preds = %23
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %20
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/dpsctl0\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Opening /dev/dpsctl\00"
@.str.2 = [11 x i8] c"Read error\00"
@str = [8 x i8] c"Reading\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = bitcast [32 x i8]* %3 to i32*
store i32 5, i32* %5
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %23
11: ; preds = %2, %17
%12 = phi i32 [ %19, %17 ], [ 0, %2 ]
%13 = call i32 @read(i32 %7, i8* %4, i32 32)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%19 = add nuw nsw i32 %12, 1
%20 = icmp eq i32 %19, 100
br i1 %20, label %21, label %11
21: ; preds = %17
%22 = tail call i32 @close(i32 %7)
br label %23
23: ; preds = %21, %9
%24 = phi i32 [ -1, %9 ], [ 0, %21 ]
ret i32 %24
}