#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ system(char *);
int /*<<< orphan*/ test_sector(int, size_t);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  int fd;
  size_t i;
  int back_errno;
  printf("insmod ../kernel/"
         "ram-disk"
         ".ko\n");
  if (system("insmod ../kernel/"
             "ram-disk"
             ".ko\n")) {
    fprintf(stderr, "insmod failed\n");
    exit(EXIT_FAILURE);
  }
  sleep(1);
  printf("mknod "
         "/dev/myblock"
         " b "
         "240"
         " "
         "0"
         "\n");
  system("mknod "
         "/dev/myblock"
         " b "
         "240"
         " "
         "0"
         "\n");
  sleep(1);
  fd = open("/dev/myblock", O_RDWR);
  if (fd < 0) {
    back_errno = errno;
    perror("open");
    fprintf(stderr, "errno is %d\n", back_errno);
    exit(EXIT_FAILURE);
  }
  srand(time(NULL));
  for (i = 0; i < 128; i++)
    test_sector(fd, i);
  close(fd);
  sleep(1);
  printf("rmmod "
         "ram-disk"
         "\n");
  system("rmmod "
         "ram-disk"
         "\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe2e(%rip),%rdi
call   1030 <system@plt>
cmp    $0x0,%rax
je     1206 <main+0x56>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
lea    0xe0a(%rip),%rdi
call   1030 <system@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1294 <main+0xe4>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xdef(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x80,-0x10(%rbp)
jae    12d7 <main+0x127>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <test_sector@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12ac <main+0xfc>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xd78(%rip),%rdi
call   1040 <printf@plt>
lea    0xd6c(%rip),%rdi
call   1030 <system@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3a(%rip),%rdi
call   1040 <system@plt>
test   %rax,%rax
jne    1279 <main+0xc9>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3c(%rip),%rdi
call   1040 <system@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1292 <main+0xe2>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <test_sector@plt>
add    $0x1,%rbx
cmp    $0x80,%rbx
jne    1230 <main+0x80>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
lea    0xdfd(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12c0 <main+0x110>
mov    0x2d37(%rip),%rax
mov    (%rax),%ebx
lea    0xdb4(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"insmod ../kernel/ram-disk.ko\0A\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"insmod failed\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"mknod /dev/myblock b 240 0\0A\00"
@.str.3 = [13 x i8] c"/dev/myblock\00"
@O_RDWR = global i32 0
@errno = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [13 x i8] c"errno is %d\0A\00"
@.str.6 = [16 x i8] c"rmmod ram-disk\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%6 = call i64 @system(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 @sleep(i32 1)
%15 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%16 = call i64 @system(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @sleep(i32 1)
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %18)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %13
%23 = load i32, i32* @errno
store i32 %23, i32* %4
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = load i32, i32* %4
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %13
%31 = call i32 @time(i32* null)
%32 = call i32 @srand(i32 %31)
store i64 0, i64* %3
br label %33
33: ; preds = %40, %30
%34 = load i64, i64* %3
%35 = icmp ult i64 %34, 128
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* %2
%38 = load i64, i64* %3
%39 = call i32 @test_sector(i32 %37, i64 %38)
br label %40
40: ; preds = %36
%41 = load i64, i64* %3
%42 = add i64 %41, 1
store i64 %42, i64* %3
br label %33
43: ; preds = %33
%44 = load i32, i32* %2
%45 = call i32 @close(i32 %44)
%46 = call i32 @sleep(i32 1)
%47 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%48 = call i64 @system(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"insmod ../kernel/ram-disk.ko\0A\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"insmod failed\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"mknod /dev/myblock b 240 0\0A\00"
@.str.3 = [13 x i8] c"/dev/myblock\00"
@O_RDWR = global i32 0
@errno = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [13 x i8] c"errno is %d\0A\00"
@.str.6 = [16 x i8] c"rmmod ram-disk\0A\00"
@str = [29 x i8] c"insmod ../kernel/ram-disk.ko\00"
@str.7 = [27 x i8] c"mknod /dev/myblock b 240 0\00"
@str.8 = [15 x i8] c"rmmod ram-disk\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%2 = tail call i64 @system(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i64 %2, 0
br i1 %3, label %9, label %4
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%12 = tail call i64 @system(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 1)
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %9
%18 = load i32, i32* @errno
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %18)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %9
%25 = tail call i32 @time(i32* null)
%26 = tail call i32 @srand(i32 %25)
br label %27
27: ; preds = %24, %27
%28 = phi i64 [ 0, %24 ], [ %30, %27 ]
%29 = tail call i32 @test_sector(i32 %15, i64 %28)
%30 = add nuw nsw i64 %28, 1
%31 = icmp eq i64 %30, 128
br i1 %31, label %32, label %27
32: ; preds = %27
%33 = tail call i32 @close(i32 %15)
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%36 = tail call i64 @system(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}