#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 */
struct stat {
  int st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int S_IFCHR;
int S_IFMT;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int);
int strlen(char *);
int chgreel(int x, int fl) {
  register int f;
  char str[22];
  FILE *devtty;
  struct stat statb;
  err("errno: %d, ", errno);
  err("Can't %s\n", x ? "write output" : "read input");
  fstat(fl, &statb);
  if ((statb.st_mode & S_IFMT) != S_IFCHR)
    exit(1);
again:
  err("If you want to go on, type device/file name when ready\n");
  devtty = fopen("/dev/tty", "r");
  fgets(str, 20, devtty);
  str[strlen(str) - 1] = '\0';
  if (!*str)
    exit(1);
  close(fl);
  if ((f = open(str, x ? 1 : 0)) < 0) {
    err("That didn't work");
    fclose(devtty);
    goto again;
  }
  return f;
}


x86 assembly (clang, -O0):
<chgreel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x4(%rbp),%ecx
lea    0xe64(%rip),%rsi
lea    0xe50(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x40(%rbp),%eax
mov    0x2def(%rip),%rcx
and    (%rcx),%eax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%eax
je     120c <chgreel+0x7c>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1211 <chgreel+0x81>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1070 <err@plt>
lea    0xe40(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x14,%esi
call   1060 <fgets@plt>
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x30(%rbp,%rax,1)
cmpb   $0x0,-0x30(%rbp)
jne    126f <chgreel+0xdf>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
xor    %esi,%esi
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    12b8 <chgreel+0x128>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1211 <chgreel+0x81>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chgreel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r13d
mov    %edi,%ebx
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe4d(%rip),%rdi
xor    %eax,%eax
call   1070 <err@plt>
test   %ebx,%ebx
lea    0xe60(%rip),%rax
lea    0xe4c(%rip),%rsi
cmove  %rax,%rsi
lea    0xe37(%rip),%rdi
xor    %eax,%eax
call   1070 <err@plt>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
call   10b0 <fstat@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%eax
jne    129f <chgreel+0x10f>
xor    %ebp,%ebp
test   %ebx,%ebx
setne  %bpl
lea    0xe5b(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xe51(%rip),%r14
lea    0xe07(%rip),%rdi
xor    %eax,%eax
call   1070 <err@plt>
lea    0xe31(%rip),%rdi
mov    %r12,%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x14,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
cmpb   $0x0,0x10(%rsp)
je     129f <chgreel+0x10f>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <open@plt>
test   %eax,%eax
jns    1290 <chgreel+0x100>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <err@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1220 <chgreel+0x90>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"errno: %d, \00"
@errno = global i32 0
@.str.1 = [10 x i8] c"Can't %s\0A\00"
@.str.2 = [13 x i8] c"write output\00"
@.str.3 = [11 x i8] c"read input\00"
@S_IFMT = global i32 0
@S_IFCHR = global i32 0
@.str.4 = [56 x i8] c"If you want to go on, type device/file name when ready\0A\00"
@.str.5 = [9 x i8] c"/dev/tty\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [17 x i8] c"That didn't work\00"
define i32 @chgreel(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [22 x i8]
%7 = alloca i32*
%8 = alloca %struct.stat
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* @errno
%10 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
%13 = zext i1 %12 to i64
%14 = select i1 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0)
%15 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i32, i32* %4
%17 = call i32 @fstat(i32 %16, %struct.stat* %8)
%18 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @S_IFMT
%21 = and i32 %19, %20
%22 = load i32, i32* @S_IFCHR
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
br label %27
27: ; preds = %53, %26
%28 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %29, i32** %7
%30 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%31 = load i32*, i32** %7
%32 = call i32 @fgets(i8* %30, i32 20, i32* %31)
%33 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 %36
store i8 0, i8* %37
%38 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %27
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %27
%44 = load i32, i32* %4
%45 = call i32 @close(i32 %44)
%46 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* %3
%48 = icmp ne i32 %47, 0
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 1, i32 0
%51 = call i32 @open(i8* %46, i32 %50)
store i32 %51, i32* %5
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %43
%54 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%55 = load i32*, i32** %7
%56 = call i32 @fclose(i32* %55)
br label %27
57: ; preds = %43
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"errno: %d, \00"
@errno = global i32 0
@.str.1 = [10 x i8] c"Can't %s\0A\00"
@.str.2 = [13 x i8] c"write output\00"
@.str.3 = [11 x i8] c"read input\00"
@S_IFMT = global i32 0
@S_IFCHR = global i32 0
@.str.4 = [56 x i8] c"If you want to go on, type device/file name when ready\0A\00"
@.str.5 = [9 x i8] c"/dev/tty\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [17 x i8] c"That didn't work\00"
define i32 @chgreel(i32 %0, i32 %1) {
%3 = alloca [22 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [22 x i8], [22 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, ...) @err(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %0, 0
%10 = select i1 %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0)
%11 = tail call i32 (i8*, ...) @err(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @fstat(i32 %1, %struct.stat* %4)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @S_IFMT
%16 = and i32 %15, %14
%17 = load i32, i32* @S_IFCHR
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = xor i1 %9, true
%21 = zext i1 %20 to i32
br label %24
22: ; preds = %2
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %19, %40
%25 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%27 = call i32 @fgets(i8* %5, i32 20, i32* %26)
%28 = call i32 @strlen(i8* %5)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [22 x i8], [22 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i8, i8* %5
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %24
%37 = call i32 @close(i32 %1)
%38 = call i32 @open(i8* %5, i32 %21)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%42 = call i32 @fclose(i32* %26)
br label %24
43: ; preds = %36
ret i32 %38
}