#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 EXIT_SUCCESS;
int O_APPEND;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_flags(int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int fd = -1;
  char chr;
  char buf[2];
  fd = open("/dev/pts/0", O_RDONLY | O_NONBLOCK | O_APPEND);
  if (fd == -1)
    perror("Open error");
  else
    print_flags(fd);
  printf("Please enter one character and then hit enter\n");
  if ((read(fd, &chr, 1)) == 1) {
    buf[0] = chr;
    buf[1] = '\0';
    printf("Read succeeded. Character was %s\n", buf);
  } else
    perror("Read fail");
  if ((write(fd, "A", 1)) == 1)
    printf("\nWrite succeeded.\n");
  else
    perror("Write failed");
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe58(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cb <main+0x5b>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
jmp    11d3 <main+0x63>
mov    -0x8(%rbp),%edi
call   1070 <print_flags@plt>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    121c <main+0xac>
mov    -0x9(%rbp),%al
mov    %al,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
lea    -0xb(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1228 <main+0xb8>
lea    0xe44(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe3f(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1258 <main+0xe8>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1264 <main+0xf4>
lea    0xe27(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e54(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     123a <main+0xba>
mov    %ebx,%edi
call   1080 <print_flags@plt>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
lea    0xd(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    124b <main+0xcb>
mov    0xd(%rsp),%al
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xe22(%rip),%rdi
lea    0xe(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3b(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1259 <main+0xd9>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdca(%rip),%rdi
call   10a0 <perror@plt>
jmp    11be <main+0x3e>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
jmp    1200 <main+0x80>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
jmp    1224 <main+0xa4>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/pts/0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_APPEND = global i32 0
@.str.1 = [11 x i8] c"Open error\00"
@.str.2 = [47 x i8] c"Please enter one character and then hit enter\0A\00"
@.str.3 = [34 x i8] c"Read succeeded. Character was %s\0A\00"
@.str.4 = [10 x i8] c"Read fail\00"
@.str.5 = [2 x i8] c"A\00"
@.str.6 = [19 x i8] c"\0AWrite succeeded.\0A\00"
@.str.7 = [13 x i8] c"Write failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca [2 x i8]
store i32 0, i32* %1
store i32 -1, i32* %2
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %5, %6
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %18
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @print_flags(i32 %16)
br label %18
18: ; preds = %15, %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %2
%21 = call i32 @read(i32 %20, i8* %3, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i8, i8* %3
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 %24, i8* %25
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 0, i8* %26
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %31
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = load i32, i32* %2
%33 = call i32 @write(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %39
39: ; preds = %37, %35
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/pts/0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_APPEND = global i32 0
@.str.1 = [11 x i8] c"Open error\00"
@.str.3 = [34 x i8] c"Read succeeded. Character was %s\0A\00"
@.str.4 = [10 x i8] c"Read fail\00"
@.str.5 = [2 x i8] c"A\00"
@.str.7 = [13 x i8] c"Write failed\00"
@EXIT_SUCCESS = global i32 0
@str = [46 x i8] c"Please enter one character and then hit enter\00"
@str.8 = [18 x i8] c"\0AWrite succeeded.\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %0
%14 = tail call i32 @print_flags(i32 %9)
br label %15
15: ; preds = %13, %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%17 = call i32 @read(i32 %9, i8* %1, i32 1)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i8, i8* %1
store i8 %20, i8* %3
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 0, i8* %21
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %25
23: ; preds = %15
%24 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 @write(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
%33 = call i32 @close(i32 %9)
%34 = load i32, i32* @EXIT_SUCCESS
ret i32 %34
}