#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 /*<<< orphan*/ readbuf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
unsigned long read(int, char *, int);
unsigned long write(int, char *, int);
int main() {
  int fd;
  unsigned long count_w;
  unsigned long count_r;
  char *buf = "hello there!";
  char readbuf[100];
  fd = open("/dev/memdev", O_RDWR);
  if (fd < 0) {
    printf("cannot open the device: memdev\n");
    return -1;
  }
  count_w = write(fd, buf, 13);
  if (count_w < 0)
    printf("cannot write device\n");
  else
    printf("have write %ld\n", count_w);
  memset(readbuf, 0, sizeof(readbuf));
  if ((count_r = read(fd, readbuf, sizeof(readbuf))) < 0)
    printf("cannot read\n");
  else
    printf("have read %ld, the readbuf is %s\n", count_r, readbuf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b9 <main+0x59>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <main+0x10e>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xd,%edx
call   1030 <write@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jae    11ec <main+0x8c>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fe <main+0x9e>
mov    -0x10(%rbp),%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jae    1246 <main+0xe6>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125f <main+0xff>
mov    -0x18(%rbp),%rsi
lea    -0x90(%rbp),%rdx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11f7 <main+0x87>
mov    %eax,%ebx
lea    0xe66(%rip),%rsi
mov    %eax,%edi
mov    $0xd,%edx
call   1040 <write@plt>
lea    0xe6c(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    0xe48(%rip),%rdi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1208 <main+0x98>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"hello there!\00"
@.str.1 = [12 x i8] c"/dev/memdev\00"
@O_RDWR = global i32 0
@.str.2 = [32 x i8] c"cannot open the device: memdev\0A\00"
@.str.3 = [21 x i8] c"cannot write device\0A\00"
@.str.4 = [16 x i8] c"have write %ld\0A\00"
@.str.5 = [13 x i8] c"cannot read\0A\00"
@.str.6 = [34 x i8] c"have read %ld, the readbuf is %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca [100 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
13: ; preds = %0
%14 = load i32, i32* %2
%15 = load i8*, i8** %5
%16 = call i64 @write(i32 %14, i8* %15, i32 13)
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %24
21: ; preds = %13
%22 = load i64, i64* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %22)
br label %24
24: ; preds = %21, %19
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 100)
%27 = load i32, i32* %2
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%29 = call i64 @read(i32 %27, i8* %28, i32 100)
store i64 %29, i64* %4
%30 = icmp ult i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %37
33: ; preds = %24
%34 = load i64, i64* %4
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i64 %34, i8* %35)
br label %37
37: ; preds = %33, %31
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
store i32 0, i32* %1
br label %40
40: ; preds = %37, %11
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [13 x i8] c"hello there!\00"
@.str.1 = [12 x i8] c"/dev/memdev\00"
@O_RDWR = global i32 0
@.str.4 = [16 x i8] c"have write %ld\0A\00"
@.str.6 = [34 x i8] c"have read %ld, the readbuf is %s\0A\00"
@str = [31 x i8] c"cannot open the device: memdev\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %15
8: ; preds = %0
%9 = tail call i64 @write(i32 %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 13)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %9)
%11 = call i32 @memset(i8* %2, i32 0, i32 100)
%12 = call i64 @read(i32 %4, i8* %2, i32 100)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i64 %12, i8* %2)
%14 = call i32 @close(i32 %4)
br label %15
15: ; preds = %8, %6
%16 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %16
}