#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;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main() {
  int fd1, fd2, fd3, ret;
  char buf[10];
  fd1 = open("/sys/kernel/kset_device_paul/dev0/dev_param1", O_RDONLY);
  if (fd1 < 0) {
    perror("Error opening foo attribute\n");
    exit(1);
  }
  while (1) {
    ret = read(fd1, buf, 10);
    if (ret == 0)
      break;
    printf("foo : # of chars returned is %d\n", ret);
    if (ret > 0) {
      write(STDOUT_FILENO, buf, ret);
    }
  }
  lseek(fd1, 0, SEEK_SET);
  while (1) {
    ret = read(fd1, buf, 10);
    if (ret == 0)
      break;
    printf("foo : # of chars returned is %d\n", ret);
    if (ret > 0) {
      write(STDOUT_FILENO, buf, ret);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b7 <main+0x47>
lea    0xe85(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11bc <main+0x4c>
mov    -0x8(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11df <main+0x6f>
jmp    1212 <main+0xa2>
mov    -0x14(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    120d <main+0x9d>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    -0x1e(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
jmp    11bc <main+0x4c>
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1248 <main+0xd8>
jmp    127b <main+0x10b>
mov    -0x14(%rbp),%esi
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1276 <main+0x106>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    -0x1e(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
jmp    1225 <main+0xb5>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11ab <main+0x3b>
lea    0xe91(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebx
lea    0x6(%rsp),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11f0 <main+0x80>
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    1243 <main+0xd3>
xor    %edi,%edi
call   1090 <exit@plt>
mov    %eax,%ebp
lea    0xe51(%rip),%r14
mov    0x2df0(%rip),%r12
lea    0x6(%rsp),%r15
jmp    1225 <main+0xb5>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11c2 <main+0x52>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
jle    1210 <main+0xa0>
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
jmp    1210 <main+0xa0>
mov    %eax,%ebp
lea    0xdfe(%rip),%r14
mov    0x2d9d(%rip),%r12
lea    0x6(%rsp),%r15
jmp    1279 <main+0x109>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11e9 <main+0x79>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
jle    1260 <main+0xf0>
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
jmp    1260 <main+0xf0>

LLVM IR (-O0):
@.str = [45 x i8] c"/sys/kernel/kset_device_paul/dev0/dev_param1\00"
@O_RDONLY = global i32 0
@.str.1 = [29 x i8] c"Error opening foo attribute\0A\00"
@.str.2 = [33 x i8] c"foo : # of chars returned is %d\0A\00"
@STDOUT_FILENO = global i32 0
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [10 x i8]
store i32 0, i32* %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %14, %32
%16 = load i32, i32* %2
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 10)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %33
22: ; preds = %15
%23 = load i32, i32* %5
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %5
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @STDOUT_FILENO
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @write(i32 %28, i8* %29, i32 %30)
br label %32
32: ; preds = %27, %22
br label %15
33: ; preds = %21
%34 = load i32, i32* %2
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 0, i32 %35)
br label %37
37: ; preds = %33, %54
%38 = load i32, i32* %2
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 10)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
br label %55
44: ; preds = %37
%45 = load i32, i32* %5
%46 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %5
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i32, i32* @STDOUT_FILENO
%51 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%52 = load i32, i32* %5
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
br label %54
54: ; preds = %49, %44
br label %37
55: ; preds = %43
%56 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [45 x i8] c"/sys/kernel/kset_device_paul/dev0/dev_param1\00"
@O_RDONLY = global i32 0
@.str.1 = [29 x i8] c"Error opening foo attribute\0A\00"
@.str.2 = [33 x i8] c"foo : # of chars returned is %d\0A\00"
@STDOUT_FILENO = global i32 0
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = call i32 @read(i32 %4, i8* %2, i32 10)
%8 = icmp eq i32 %7, 0
br i1 %8, label %22, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6, %19
%13 = phi i32 [ %20, %19 ], [ %7, %6 ]
%14 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp sgt i32 %13, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @write(i32 %17, i8* %2, i32 %13)
br label %19
19: ; preds = %16, %12
%20 = call i32 @read(i32 %4, i8* %2, i32 10)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %19, %6
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @lseek(i32 %4, i32 0, i32 %23)
%25 = call i32 @read(i32 %4, i8* %2, i32 10)
%26 = icmp eq i32 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %22, %34
%28 = phi i32 [ %35, %34 ], [ %25, %22 ]
%29 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp sgt i32 %28, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @write(i32 %32, i8* %2, i32 %28)
br label %34
34: ; preds = %31, %27
%35 = call i32 @read(i32 %4, i8* %2, i32 10)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %27
37: ; preds = %34, %22
%38 = tail call i32 @exit(i32 0)
unreachable
}