#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 uint32_t;
typedef int /*<<< orphan*/ info;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
size_t read(int, int *, int);
size_t write(int, int *, int);
int main(int argc, char **argv) {
  char *dev = "/dev/uio0";
  if (argc > 1) {
    dev = argv[1];
    exit(EXIT_FAILURE);
  }
  int fd = open(dev, O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  while (1) {
    uint32_t info = 1;
    size_t nb = write(fd, &info, sizeof(info));
    if (nb < sizeof(info)) {
      perror("write");
      close(fd);
      exit(EXIT_FAILURE);
    }
    nb = read(fd, &info, sizeof(info));
    if (nb == sizeof(info)) {
      printf("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/cirosantilli/linux-kernel-module-cheat/"
             "refs/heads/master/userland/kernel_modules/uio_read.c"
             " read = %u\n",
             info);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ee <main+0x7e>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    11f3 <main+0x83>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x4,-0x28(%rbp)
jae    123c <main+0xcc>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x4,-0x28(%rbp)
jne    126b <main+0xfb>
mov    -0x20(%rbp),%esi
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
jmp    11f3 <main+0x83>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jge    11d9 <main+0x69>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11a5 <main+0x35>
lea    0xe6c(%rip),%rdi
call   1080 <perror@plt>
jmp    11d9 <main+0x69>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%rax
jae    11e7 <main+0x77>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xc(%rsp),%r14
lea    0xe22(%rip),%r15
jmp    121d <main+0xad>
cs nopw 0x0(%rax,%rax,1)
nop
movl   $0x1,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%rax
jb     11c6 <main+0x56>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%rax
jne    1200 <main+0x90>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
call   1040 <printf@plt>
jmp    1200 <main+0x90>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/uio0\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [193 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/cirosantilli/linux-kernel-module-cheat/refs/heads/master/userland/kernel_modules/uio_read.c read = %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %6
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
br label %29
29: ; preds = %28, %48
store i32 1, i32* %8
%30 = load i32, i32* %7
%31 = call i64 @write(i32 %30, i32* %8, i32 4)
store i64 %31, i64* %9
%32 = load i64, i64* %9
%33 = icmp ult i64 %32, 4
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29
%41 = load i32, i32* %7
%42 = call i64 @read(i32 %41, i32* %8, i32 4)
store i64 %42, i64* %9
%43 = load i64, i64* %9
%44 = icmp eq i64 %43, 4
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32, i32* %8
%47 = call i32 @printf(i8* getelementptr inbounds ([193 x i8], [193 x i8]* @.str.3, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %45, %40
br label %29
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/uio0\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [193 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/cirosantilli/linux-kernel-module-cheat/refs/heads/master/userland/kernel_modules/uio_read.c read = %u\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%14 = call i64 @write(i32 %10, i32* %3, i32 4)
%15 = icmp ult i64 %14, 4
br i1 %15, label %20, label %25
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %31, %12
%21 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @close(i32 %10)
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %12, %31
%26 = call i64 @read(i32 %10, i32* %3, i32 4)
%27 = icmp eq i64 %26, 4
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %3
%30 = tail call i32 @printf(i8* getelementptr inbounds ([193 x i8], [193 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %28, %25
store i32 1, i32* %3
%32 = call i64 @write(i32 %10, i32* %3, i32 4)
%33 = icmp ult i64 %32, 4
br i1 %33, label %20, label %25
}