#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 S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char **, int);
void main() {
  int fd;
  char buf[2];
  fd = open("/dev/fgj", O_RDWR, S_IRUSR | S_IWUSR);
  if (fd != -1) {
    read(fd, &buf, 2);
    buf[2] = 0;
    printf("The data is %s\n", buf);
  } else {
    printf("device open failed\n");
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
mov    0x2e7f(%rip),%rax
or     (%rax),%edx
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     11a8 <main+0x68>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
movb   $0x0,-0x4(%rbp)
lea    -0x6(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11b6 <main+0x76>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    0x2e6c(%rip),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ae <main+0x5e>
lea    0x6(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1060 <read@plt>
movb   $0x0,0x8(%rsp)
lea    0xe67(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11ba <main+0x6a>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/fgj\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [16 x i8] c"The data is %s\0A\00"
@.str.2 = [20 x i8] c"device open failed\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca [2 x i8]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %0
%11 = load i32, i32* %1
%12 = bitcast [2 x i8]* %2 to i8**
%13 = call i32 @read(i32 %11, i8** %12, i32 2)
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 2
store i8 0, i8* %14
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %19
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
%20 = load i32, i32* %1
%21 = call i32 @close(i32 %20)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/fgj\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [16 x i8] c"The data is %s\0A\00"
@str = [19 x i8] c"device open failed\00"
define void @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %0
%10 = bitcast [2 x i8]* %1 to i8**
%11 = call i32 @read(i32 %7, i8** %10, i32 2)
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 2
store i8 0, i8* %12
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %16
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = call i32 @close(i32 %7)
ret void
}