#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 BUFSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, size_t);
size_t read(int, char *, int);
size_t write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  size_t size;
  char buffer[BUFSIZ];
  if ((fd = open("/dev/fd0", O_RDWR)) == 0) {
    perror("Open error: ");
    exit(-1);
  }
  if ((size = write(fd, "Marco", 5)) == -1) {
    perror("Write error: ");
    exit(-1);
  }
  lseek(fd, 0, SEEK_SET);
  if ((size = read(fd, buffer, BUFSIZ)) == -1) {
    perror("Read error: ");
    exit(-1);
  }
  printf("%s\nSize: %d\n", buffer, size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%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,-0x30(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    11f9 <main+0x79>
lea    0xe1f(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xe13(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    1231 <main+0xb1>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    127d <main+0xfd>
lea    0xdbc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e47(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
je     1224 <main+0xa4>
mov    %eax,%ebx
lea    0xe4f(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     122d <main+0xad>
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     1236 <main+0xb6>
lea    0xe32(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdde(%rip),%rdi
jmp    123d <main+0xbd>
lea    0xde8(%rip),%rdi
jmp    123d <main+0xbd>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [9 x i8] c"/dev/fd0\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"Open error: \00"
@.str.2 = [6 x i8] c"Marco\00"
@.str.3 = [14 x i8] c"Write error: \00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"Read error: \00"
@.str.5 = [13 x i8] c"%s\0ASize: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %8
%13 = alloca i8, i64 %11
store i64 %11, i64* %9
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i64 @write(i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
store i64 %22, i64* %7
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %20
%28 = load i32, i32* %6
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @lseek(i32 %28, i32 0, i32 %29)
%31 = load i32, i32* %6
%32 = load i32, i32* @BUFSIZ
%33 = call i64 @read(i32 %31, i8* %13, i32 %32)
store i64 %33, i64* %7
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %27
%39 = load i64, i64* %7
%40 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %13, i64 %39)
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
store i32 0, i32* %3
%43 = load i8*, i8** %8
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [9 x i8] c"/dev/fd0\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"Open error: \00"
@.str.2 = [6 x i8] c"Marco\00"
@.str.3 = [14 x i8] c"Write error: \00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"Read error: \00"
@.str.5 = [13 x i8] c"%s\0ASize: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = tail call i64 @write(i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @lseek(i32 %7, i32 0, i32 %19)
%21 = load i32, i32* @BUFSIZ
%22 = call i64 @read(i32 %7, i8* %5, i32 %21)
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %18
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %5, i64 %22)
%29 = call i32 @close(i32 %7)
ret i32 0
}