#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
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_RDWR);
  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
lea    0xe6a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b9 <main+0x49>
lea    0xe5c(%rip),%rdi
call   1090 <perror@plt>
jmp    11c1 <main+0x51>
mov    -0x8(%rbp),%edi
call   1070 <print_flags@plt>
lea    0xe4e(%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    120a <main+0x9a>
mov    -0x9(%rbp),%al
mov    %al,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
lea    -0xb(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1216 <main+0xa6>
lea    0xe56(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe51(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1246 <main+0xd6>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1252 <main+0xe2>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d77(%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    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1228 <main+0xa8>
mov    %ebx,%edi
call   1080 <print_flags@plt>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
lea    0xd(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1239 <main+0xb9>
mov    0xd(%rsp),%al
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xe34(%rip),%rdi
lea    0xe(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4d(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1247 <main+0xc7>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xddc(%rip),%rdi
call   10a0 <perror@plt>
jmp    11ac <main+0x2c>
lea    0xdf8(%rip),%rdi
call   10a0 <perror@plt>
jmp    11ee <main+0x6e>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
jmp    1212 <main+0x92>

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

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