#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 */
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int DosRead(int /*<<< orphan*/, char *, int, int *);
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NDELAY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fcntl(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ hexwrite(int /*<<< orphan*/ *, char *, int);
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ write(int, char *, int);
main() {
  char buf[100];
  int nread, rc;
  FILE *fd = fopen("log.dbg", "w");
  if (!fd) {
    fprintf(stderr, "Cannot open log.dbg\n");
    exit(1);
  }
  fprintf(fd, "Log opened, %d\n", isatty(0));
  fflush(fd);
  fcntl(0, F_SETFL, O_NDELAY);
  for (;;) {
    memset(buf, 0, 100);
    rc = DosRead(0, buf, sizeof(buf), &nread);
    if (rc) {
      fprintf(fd, "rc=%d\n", rc);
      break;
    }
    if (buf[0] == 'x' && buf[1] == 'y')
      break;
    if (nread < 0) {
      fprintf(fd, "nread = %d\n", nread);
      fflush(fd);
      break;
    }
    if (nread) {
      hexwrite(fd, buf, nread);
      write(1, buf, nread);
    }
  }
  fprintf(fd, "Log closed\n");
  fflush(fd);
  fclose(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rdi
lea    0xe38(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    1206 <main+0x56>
mov    0x2df5(%rip),%rax
mov    (%rax),%rdi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
xor    %edi,%edi
call   1030 <isatty@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%edx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x80(%rbp),%rdi
call   10a0 <fflush@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1040 <fcntl@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
lea    -0x70(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
lea    -0x74(%rbp),%rcx
call   1080 <DosRead@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
je     129c <main+0xec>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%edx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1318 <main+0x168>
movsbl -0x70(%rbp),%eax
cmp    $0x78,%eax
jne    12bb <main+0x10b>
movsbl -0x6f(%rbp),%eax
cmp    $0x79,%eax
jne    12bb <main+0x10b>
jmp    1318 <main+0x168>
cmpl   $0x0,-0x74(%rbp)
jge    12e8 <main+0x138>
mov    -0x80(%rbp),%rdi
mov    -0x74(%rbp),%edx
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x80(%rbp),%rdi
call   10a0 <fflush@plt>
jmp    1318 <main+0x168>
cmpl   $0x0,-0x74(%rbp)
je     1313 <main+0x163>
mov    -0x80(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    -0x74(%rbp),%edx
call   10b0 <hexwrite@plt>
lea    -0x70(%rbp),%rsi
mov    -0x74(%rbp),%edx
mov    $0x1,%edi
call   1050 <write@plt>
jmp    1251 <main+0xa1>
mov    -0x80(%rbp),%rdi
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x80(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x80(%rbp),%rdi
call   1060 <fclose@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
lea    0xe2d(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
jne    120e <main+0x4e>
mov    0x2df5(%rip),%rax
mov    (%rax),%rcx
lea    0xe15(%rip),%rdi
mov    $0x14,%esi
mov    $0x1,%edx
call   10e0 <fwrite@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %rax,%r14
xor    %edi,%edi
call   1030 <isatty@plt>
lea    0xe00(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r14,%rdi
call   10a0 <fflush@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1040 <fcntl@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
lea    0xc(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <DosRead@plt>
test   %eax,%eax
jne    12eb <main+0x12b>
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
jmp    12b5 <main+0xf5>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
mov    %r15,%rcx
call   1080 <DosRead@plt>
test   %eax,%eax
jne    12eb <main+0x12b>
cmpb   $0x78,0x10(%rsp)
jne    12c3 <main+0x103>
cmpb   $0x79,0x11(%rsp)
je     1319 <main+0x159>
mov    0xc(%rsp),%edx
test   %edx,%edx
js     1300 <main+0x140>
je     1290 <main+0xd0>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10b0 <hexwrite@plt>
mov    0xc(%rsp),%edx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
jmp    1290 <main+0xd0>
lea    0xd3d(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1319 <main+0x159>
lea    0xd2f(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r14,%rdi
call   10a0 <fflush@plt>
lea    0xd22(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10e0 <fwrite@plt>
mov    %r14,%rdi
call   10a0 <fflush@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"log.dbg\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [21 x i8] c"Cannot open log.dbg\0A\00"
@.str.3 = [16 x i8] c"Log opened, %d\0A\00"
@F_SETFL = global i32 0
@O_NDELAY = global i32 0
@.str.4 = [7 x i8] c"rc=%d\0A\00"
@.str.5 = [12 x i8] c"nread = %d\0A\00"
@.str.6 = [12 x i8] c"Log closed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %0
%10 = load i32*, i32** @stderr
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32*, i32** %5
%15 = call i32 @isatty(i32 0)
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %5
%18 = call i32 @fflush(i32* %17)
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @O_NDELAY
%21 = call i32 @fcntl(i32 0, i32 %19, i32 %20)
br label %22
22: ; preds = %64, %13
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 100)
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%26 = call i32 @DosRead(i32 0, i8* %25, i32 100, i32* %3)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i32*, i32** %5
%31 = load i32, i32* %4
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %65
33: ; preds = %22
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 120
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 1
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 121
br i1 %42, label %43, label %44
43: ; preds = %38
br label %65
44: ; preds = %38, %33
%45 = load i32, i32* %3
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i32*, i32** %5
%49 = load i32, i32* %3
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = load i32*, i32** %5
%52 = call i32 @fflush(i32* %51)
br label %65
53: ; preds = %44
%54 = load i32, i32* %3
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %53
%57 = load i32*, i32** %5
%58 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%59 = load i32, i32* %3
%60 = call i32 @hexwrite(i32* %57, i8* %58, i32 %59)
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%62 = load i32, i32* %3
%63 = call i32 @write(i32 1, i8* %61, i32 %62)
br label %64
64: ; preds = %56, %53
br label %22
65: ; preds = %47, %43, %29
%66 = load i32*, i32** %5
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %66, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%68 = load i32*, i32** %5
%69 = call i32 @fflush(i32* %68)
%70 = load i32*, i32** %5
%71 = call i32 @fclose(i32* %70)
%72 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"log.dbg\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [21 x i8] c"Cannot open log.dbg\0A\00"
@.str.3 = [16 x i8] c"Log opened, %d\0A\00"
@F_SETFL = global i32 0
@O_NDELAY = global i32 0
@.str.4 = [7 x i8] c"rc=%d\0A\00"
@.str.5 = [12 x i8] c"nread = %d\0A\00"
@.str.6 = [12 x i8] c"Log closed\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32*, i32** @stderr
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 20, i64 1, i32* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @isatty(i32 0)
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i32 @fflush(i32* %5)
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* @O_NDELAY
%17 = tail call i32 @fcntl(i32 0, i32 %15, i32 %16)
%18 = call i32 @memset(i8* %3, i32 0, i32 100)
%19 = call i32 @DosRead(i32 0, i8* %3, i32 100, i32* %2)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 1
br label %26
23: ; preds = %44, %11
%24 = phi i32 [ %19, %11 ], [ %46, %44 ]
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br label %48
26: ; preds = %21, %44
%27 = load i8, i8* %3
%28 = icmp eq i8 %27, 120
%29 = load i8, i8* %22
%30 = icmp eq i8 %29, 121
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %48, label %32
32: ; preds = %26
%33 = load i32, i32* %2
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%37 = call i32 @fflush(i32* %5)
br label %48
38: ; preds = %32
%39 = icmp eq i32 %33, 0
br i1 %39, label %44, label %40
40: ; preds = %38
%41 = call i32 @hexwrite(i32* %5, i8* %3, i32 %33)
%42 = load i32, i32* %2
%43 = call i32 @write(i32 1, i8* %3, i32 %42)
br label %44
44: ; preds = %40, %38
%45 = call i32 @memset(i8* %3, i32 0, i32 100)
%46 = call i32 @DosRead(i32 0, i8* %3, i32 100, i32* %2)
%47 = icmp eq i32 %46, 0
br i1 %47, label %26, label %23
48: ; preds = %26, %35, %23
%49 = call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i64 11, i64 1, i32* %5)
%50 = call i32 @fflush(i32* %5)
%51 = call i32 @fclose(i32* %5)
%52 = call i32 @exit(i32 0)
unreachable
}