#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*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
void main(void) {
  int fd;
  int i;
  char data[256];
  int retval;
  fd = open("/dev/fgj", O_RDWR);
  if (fd == -1) {
    perror("erroropen\n");
    exit(-1);
  }
  printf("open/dev/fgjsuccessfully\n");
  retval = write(fd, "fgj", 3);
  if (retval == -1) {
    perror("writeerror\n");
    exit(-1);
  }
  retval = read(fd, data, 3);
  if (retval == -1) {
    perror("readerror\n");
    exit(-1);
  }
  data[retval] = 0;
  printf("readsuccessfully:%s\n", data);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11b3 <main+0x43>
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    0xe3a(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    11fe <main+0x8e>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x3,%edx
call   1060 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    123b <main+0xcb>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x100,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1203 <main+0x83>
mov    %eax,%ebx
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     120c <main+0x8c>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1215 <main+0x95>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe42(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x100,%rsp
pop    %rbx
ret
lea    0xdff(%rip),%rdi
jmp    121c <main+0x9c>
lea    0xe01(%rip),%rdi
jmp    121c <main+0x9c>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/fgj\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"erroropen\0A\00"
@.str.2 = [26 x i8] c"open/dev/fgjsuccessfully\0A\00"
@.str.3 = [4 x i8] c"fgj\00"
@.str.4 = [12 x i8] c"writeerror\0A\00"
@.str.5 = [11 x i8] c"readerror\0A\00"
@.str.6 = [21 x i8] c"readsuccessfully:%s\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* %1
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %12
%22 = load i32, i32* %1
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 3)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %21
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %1
%37 = call i32 @close(i32 %36)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/fgj\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"erroropen\0A\00"
@.str.3 = [4 x i8] c"fgj\00"
@.str.4 = [12 x i8] c"writeerror\0A\00"
@.str.5 = [11 x i8] c"readerror\0A\00"
@.str.6 = [21 x i8] c"readsuccessfully:%s\0A\00"
@str = [25 x i8] c"open/dev/fgjsuccessfully\00"
define void @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %9
%17 = call i32 @read(i32 %4, i8* %2, i32 3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %16
%23 = sext i32 %17 to i64
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%26 = call i32 @close(i32 %4)
ret void
}