#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
unsigned long lseek(int, unsigned long, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned char, unsigned long);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int do_read_devport(unsigned long addr, unsigned long nports) {
  unsigned char zero = 0, readout = 0;
  int fd;
  if ((fd = open("/dev/port", O_RDWR)) < 0)
    return EXIT_FAILURE;
  if (addr != lseek(fd, addr, SEEK_SET))
    return EXIT_FAILURE;
  printf("Writing: %6d  to  %lx\n", zero, addr);
  write(fd, &zero, 1);
  usleep(1000);
  read(fd, &readout, 1);
  printf("Reading: %6d from %lx\n", readout, addr + 1);
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<do_read_devport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x0,-0x1a(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11ba <do_read_devport+0x4a>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <do_read_devport+0xf8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     11f8 <do_read_devport+0x88>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <do_read_devport+0xf8>
mov    -0x19(%rbp),%al
mov    -0x10(%rbp),%rdx
lea    0xe04(%rip),%rdi
movzbl %al,%esi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x3e8,%edi
call   1090 <usleep@plt>
mov    -0x20(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x1a(%rbp),%al
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
lea    0xdd5(%rip),%rdi
movzbl %al,%esi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read_devport>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movb   $0x0,0xf(%rsp)
movb   $0x0,0xe(%rsp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1080 <open@plt>
mov    0x2e2f(%rip),%rbx
test   %eax,%eax
js     1222 <do_read_devport+0xb2>
mov    %eax,%ebp
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <lseek@plt>
cmp    %r14,%rax
jne    1222 <do_read_devport+0xb2>
lea    0xe44(%rip),%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <printf@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x3e8,%edi
call   1090 <usleep@plt>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1070 <read@plt>
movzbl 0xe(%rsp),%esi
add    $0x1,%r14
lea    0xe15(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2da6(%rip),%rbx
mov    (%rbx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/port\00"
@O_RDWR = global i32 0
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [23 x i8] c"Writing: %6d to %lx\0A\00"
@.str.2 = [23 x i8] c"Reading: %6d from %lx\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @do_read_devport(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
store i8 0, i8* %6
store i8 0, i8* %7
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %3
br label %39
14: ; preds = %2
%15 = load i64, i64* %4
%16 = load i32, i32* %8
%17 = load i64, i64* %4
%18 = load i32, i32* @SEEK_SET
%19 = call i64 @lseek(i32 %16, i64 %17, i32 %18)
%20 = icmp ne i64 %15, %19
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* @EXIT_FAILURE
store i32 %22, i32* %3
br label %39
23: ; preds = %14
%24 = load i8, i8* %6
%25 = load i64, i64* %4
%26 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %24, i64 %25)
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %27, i8* %6, i32 1)
%29 = call i32 @usleep(i32 1000)
%30 = load i32, i32* %8
%31 = call i32 @read(i32 %30, i8* %7, i32 1)
%32 = load i8, i8* %7
%33 = load i64, i64* %4
%34 = add i64 %33, 1
%35 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %32, i64 %34)
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
store i32 %38, i32* %3
br label %39
39: ; preds = %23, %21, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/port\00"
@O_RDWR = global i32 0
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [23 x i8] c"Writing: %6d to %lx\0A\00"
@.str.2 = [23 x i8] c"Reading: %6d from %lx\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @do_read_devport(i64 %0, i64 %1) {
%3 = alloca i8
%4 = alloca i8
store i8 0, i8* %3
store i8 0, i8* %4
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = load i32, i32* @SEEK_SET
%10 = tail call i64 @lseek(i32 %6, i64 %0, i32 %9)
%11 = icmp eq i64 %10, %0
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8 zeroext 0, i64 %0)
%14 = call i32 @write(i32 %6, i8* %3, i32 1)
%15 = tail call i32 @usleep(i32 1000)
%16 = call i32 @read(i32 %6, i8* %4, i32 1)
%17 = load i8, i8* %4
%18 = add i64 %0, 1
%19 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %17, i64 %18)
%20 = tail call i32 @close(i32 %6)
br label %21
21: ; preds = %8, %2, %12
%22 = phi i32* [ @EXIT_SUCCESS, %12 ], [ @EXIT_FAILURE, %2 ], [ @EXIT_FAILURE, %8 ]
%23 = load i32, i32* %22
ret i32 %23
}