#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 O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd = -1;
  char buffer[32] = {0};
  fd = open("/dev/hello", O_RDWR | O_NOCTTY);
  if (fd < 0) {
    printf("Device cannot be opened!!! \n");
    exit(1);
  } else {
    write(fd, buffer, 10);
    memset(buffer, 10, 0);
    read(fd, buffer, 10);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
lea    0xe51(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d7 <main+0x67>
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
lea    -0x30(%rbp),%rdi
mov    $0xa,%esi
xor    %edx,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e5e(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe64(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e8 <main+0x78>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    %r14,%rdi
mov    $0xa,%esi
xor    %edx,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/hello\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [29 x i8] c"Device cannot be opened!!! \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [32 x i8]
store i32 0, i32* %1
store i32 -1, i32* %2
%4 = bitcast [32 x i8]* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, 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 %14
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%17 = call i32 @write(i32 %15, i8* %16, i32 10)
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 10, i32 0)
%20 = load i32, i32* %2
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 10)
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/hello\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@str = [28 x i8] c"Device cannot be opened!!! \00"
define i32 @main() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @write(i32 %6, i8* %2, i32 10)
%13 = call i32 @memset(i8* %2, i32 10, i32 0)
%14 = call i32 @read(i32 %6, i8* %2, i32 10)
%15 = call i32 @close(i32 %6)
ret i32 0
}