#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*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int screen = 0xb8000;
  int size = 80 * 25 * 2;
  char buf[size];
  int fd = open("/dev/kmem", 0x0002);
  lseek(fd, screen, 0);
  read(fd, (char *)buf, size);
  int i;
  printf("First line:\n");
  for (i = 0; i < 160; i += 2) {
    printf("%c", buf[i]);
  }
  printf("\n");
  char *msg = "HELLO THIS IS A TEST";
  printf("Setting first line to '%s'\n", msg);
  for (i = 0; i < 2 * strlen(msg); i += 2) {
    buf[i] = msg[i / 2];
  }
  lseek(fd, screen, 0);
  write(fd, buf, 2 * strlen(msg));
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xb8000,-0x8(%rbp)
movl   $0xfa0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe36(%rip),%rdi
mov    $0x2,%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1060 <lseek@plt>
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%edx
call   1080 <read@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0xa0,-0x28(%rbp)
jge    123d <main+0xbd>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    1208 <main+0x88>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdc8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    12be <main+0x13e>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%eax
mov    $0x2,%esi
cltd
idiv   %esi
mov    %eax,%edx
mov    -0x38(%rbp),%rax
movslq %edx,%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    126f <main+0xef>
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1060 <lseek@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
shl    $0x1,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xfa8,%rsp
lea    0xe4c(%rip),%rdi
mov    $0x2,%esi
call   10b0 <open@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0xb8000,%esi
xor    %edx,%edx
call   1080 <lseek@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xfa0,%edx
call   10a0 <read@plt>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffffffffffe,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl 0x2(%rsp,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x2,%rbx
cmp    $0x9e,%rbx
jb     1200 <main+0x60>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdf7(%rip),%rdi
lea    0xddb(%rip),%r15
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    126f <main+0xcf>
xor    %ebp,%ebp
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%eax
mov    %al,(%rsp,%rbp,1)
add    $0x2,%rbp
mov    %r15,%rdi
call   1060 <strlen@plt>
add    %eax,%eax
cltq
add    $0x1,%rbx
cmp    %rax,%rbp
jl     1250 <main+0xb0>
mov    %r14d,%edi
mov    $0xb8000,%esi
xor    %edx,%edx
call   1080 <lseek@plt>
lea    0xd85(%rip),%rdi
call   1060 <strlen@plt>
lea    (%rax,%rax,1),%edx
mov    %rsp,%rsi
mov    %r14d,%edi
call   1050 <write@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/kmem\00"
@.str.1 = [13 x i8] c"First line:\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [21 x i8] c"HELLO THIS IS A TEST\00"
@.str.5 = [28 x i8] c"Setting first line to '%s'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %1
store i32 753664, i32* %2
store i32 4000, i32* %3
%9 = load i32, i32* %3
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = load i32, i32* %2
%16 = call i32 @lseek(i32 %14, i32 %15, i32 0)
%17 = load i32, i32* %6
%18 = load i32, i32* %3
%19 = call i32 @read(i32 %17, i8* %12, i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %7
br label %21
21: ; preds = %31, %0
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 160
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %12, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %24
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 2
store i32 %33, i32* %7
br label %21
34: ; preds = %21
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8** %8
%36 = load i8*, i8** %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %36)
store i32 0, i32* %7
br label %38
38: ; preds = %54, %34
%39 = load i32, i32* %7
%40 = load i8*, i8** %8
%41 = call i32 @strlen(i8* %40)
%42 = mul nsw i32 2, %41
%43 = icmp slt i32 %39, %42
br i1 %43, label %44, label %57
44: ; preds = %38
%45 = load i8*, i8** %8
%46 = load i32, i32* %7
%47 = sdiv i32 %46, 2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8, i8* %49
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %12, i64 %52
store i8 %50, i8* %53
br label %54
54: ; preds = %44
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 2
store i32 %56, i32* %7
br label %38
57: ; preds = %38
%58 = load i32, i32* %6
%59 = load i32, i32* %2
%60 = call i32 @lseek(i32 %58, i32 %59, i32 0)
%61 = load i32, i32* %6
%62 = load i8*, i8** %8
%63 = call i32 @strlen(i8* %62)
%64 = mul nsw i32 2, %63
%65 = call i32 @write(i32 %61, i8* %12, i32 %64)
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
%68 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/kmem\00"
@.str.4 = [21 x i8] c"HELLO THIS IS A TEST\00"
@.str.5 = [28 x i8] c"Setting first line to '%s'\0A\00"
@str = [12 x i8] c"First line:\00"
define i32 @main() {
%1 = alloca [4000 x i8]
%2 = getelementptr inbounds [4000 x i8], [4000 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 2)
%4 = tail call i32 @lseek(i32 %3, i32 753664, i32 0)
%5 = call i32 @read(i32 %3, i8* %2, i32 4000)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %7
7: ; preds = %0, %7
%8 = phi i64 [ 0, %0 ], [ %13, %7 ]
%9 = getelementptr inbounds [4000 x i8], [4000 x i8]* %1, i64 0, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = tail call i32 @putchar(i32 %11)
%13 = add nuw nsw i64 %8, 2
%14 = icmp ult i64 %8, 158
br i1 %14, label %7, label %15
15: ; preds = %7
%16 = tail call i32 @putchar(i32 10)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %15, %20
%21 = phi i64 [ %26, %20 ], [ 0, %15 ]
%22 = lshr exact i64 %21, 1
%23 = getelementptr inbounds [21 x i8], [21 x i8]* @.str.4, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = getelementptr inbounds [4000 x i8], [4000 x i8]* %1, i64 0, i64 %21
store i8 %24, i8* %25
%26 = add nuw nsw i64 %21, 2
%27 = tail call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%28 = shl nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %26, %29
br i1 %30, label %20, label %31
31: ; preds = %20, %15
%32 = tail call i32 @lseek(i32 %3, i32 753664, i32 0)
%33 = tail call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%34 = shl nsw i32 %33, 1
%35 = call i32 @write(i32 %3, i8* %2, i32 %34)
%36 = tail call i32 @close(i32 %3)
%37 = tail call i32 @exit(i32 0)
unreachable
}