#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_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_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    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c2 <main+0x52>
lea    0xe53(%rip),%rdi
call   1090 <perror@plt>
jmp    11ca <main+0x5a>
mov    -0x8(%rbp),%edi
call   1070 <print_flags@plt>
lea    0xe45(%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    1213 <main+0xa3>
mov    -0x9(%rbp),%al
mov    %al,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
lea    -0xb(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121f <main+0xaf>
lea    0xe4d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe48(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    124f <main+0xdf>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125b <main+0xeb>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1231 <main+0xb1>
mov    %ebx,%edi
call   1080 <print_flags@plt>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
lea    0xd(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1242 <main+0xc2>
mov    0xd(%rsp),%al
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xe2b(%rip),%rdi
lea    0xe(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe44(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1250 <main+0xd0>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
jmp    11b5 <main+0x35>
lea    0xdef(%rip),%rdi
call   10a0 <perror@plt>
jmp    11f7 <main+0x77>
lea    0xded(%rip),%rdi
call   10a0 <perror@plt>
jmp    121b <main+0x9b>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/pts/0\00"
@O_RDONLY = 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_APPEND
%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 eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %16
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @print_flags(i32 %14)
br label %16
16: ; preds = %13, %11
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = call i32 @read(i32 %18, i8* %3, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i8, i8* %3
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 %22, i8* %23
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 0, i8* %24
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %29
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = load i32, i32* %2
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @EXIT_SUCCESS
ret i32 %40
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/pts/0\00"
@O_RDONLY = 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_APPEND
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %13
11: ; preds = %0
%12 = tail call i32 @print_flags(i32 %7)
br label %13
13: ; preds = %11, %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%15 = call i32 @read(i32 %7, i8* %1, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i8, i8* %1
store i8 %18, i8* %3
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 0, i8* %19
%20 = 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 %23
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = call i32 @write(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
%31 = call i32 @close(i32 %7)
%32 = load i32, i32* @EXIT_SUCCESS
ret i32 %32
}