#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_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int i;
  int *ints;
  int fd = open("/dev/ttyS1", O_RDWR);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  while (1) {
    write(STDOUT_FILENO, "Starting...\n", 12);
    sleep(2);
    write(STDOUT_FILENO, "Calling sbrk...\n", 16);
    ints = malloc(256 * sizeof(int));
    printf("New malloc ptr: %p\n", ints);
    printf("Sleeping for 10 seconds after allocation...\n");
    sleep(10);
    printf("Faulting page in...\n");
    for (i = 0; i < 256; i++) {
      printf("Prev value: %d, ", ints[i]);
      ints[i] = i;
      printf("new %d\n", ints[i]);
    }
    printf("Writing done...\n");
    printf("Argv 0: %s\n", argv[0]);
  }
  return 0;
}


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)
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x100,-0x14(%rbp)
jge    1296 <main+0x136>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123b <main+0xdb>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11b0 <main+0x50>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x2e54(%rip),%r13
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
lea    0xe7f(%rip),%r14
lea    0xe89(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%edi
lea    0xe30(%rip),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x0(%r13),%edi
lea    0xe1e(%rip),%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
lea    0xe11(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp,%r12,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,0x0(%rbp,%r12,4)
mov    %r15,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%r12
cmp    $0x100,%r12
jne    1250 <main+0xe0>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%rsi
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d0 <main+0x60>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ttyS1\00"
@O_RDWR = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [13 x i8] c"Starting...\0A\00"
@.str.2 = [17 x i8] c"Calling sbrk...\0A\00"
@.str.3 = [20 x i8] c"New malloc ptr: %p\0A\00"
@.str.4 = [45 x i8] c"Sleeping for 10 seconds after allocation...\0A\00"
@.str.5 = [21 x i8] c"Faulting page in...\0A\00"
@.str.6 = [17 x i8] c"Prev value: %d, \00"
@.str.7 = [8 x i8] c"new %d\0A\00"
@.str.8 = [17 x i8] c"Writing done...\0A\00"
@.str.9 = [12 x i8] c"Argv 0: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = load i32, i32* %8
%15 = load i32, i32* @STDERR_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
br label %17
17: ; preds = %2, %53
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 12)
%20 = call i32 @sleep(i32 2)
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 16)
%23 = call i32* @malloc(i32 1024)
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @sleep(i32 10)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %6
br label %29
29: ; preds = %50, %17
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 256
br i1 %31, label %32, label %53
32: ; preds = %29
%33 = load i32*, i32** %7
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %6
%40 = load i32*, i32** %7
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
store i32 %39, i32* %43
%44 = load i32*, i32** %7
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %32
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %29
53: ; preds = %29
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 0
%57 = load i8*, i8** %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %57)
br label %17
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyS1\00"
@O_RDWR = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [13 x i8] c"Starting...\0A\00"
@.str.2 = [17 x i8] c"Calling sbrk...\0A\00"
@.str.3 = [20 x i8] c"New malloc ptr: %p\0A\00"
@.str.6 = [17 x i8] c"Prev value: %d, \00"
@.str.7 = [8 x i8] c"new %d\0A\00"
@.str.9 = [12 x i8] c"Argv 0: %s\0A\00"
@str = [44 x i8] c"Sleeping for 10 seconds after allocation...\00"
@str.10 = [20 x i8] c"Faulting page in...\00"
@str.11 = [16 x i8] c"Writing done...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i32 @dup2(i32 %4, i32 %5)
%7 = load i32, i32* @STDERR_FILENO
%8 = tail call i32 @dup2(i32 %4, i32 %7)
br label %9
9: ; preds = %29, %2
%10 = load i32, i32* @STDOUT_FILENO
%11 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 12)
%12 = tail call i32 @sleep(i32 2)
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 16)
%15 = tail call i32* @malloc(i32 1024)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %15)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @sleep(i32 10)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
br label %20
20: ; preds = %9, %20
%21 = phi i64 [ 0, %9 ], [ %27, %20 ]
%22 = getelementptr inbounds i32, i32* %15, i64 %21
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = trunc i64 %21 to i32
store i32 %25, i32* %22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%27 = add nuw nsw i64 %21, 1
%28 = icmp eq i64 %27, 256
br i1 %28, label %29, label %20
29: ; preds = %20
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0))
%31 = load i8*, i8** %1
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %31)
br label %9
}