#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*/ FILE;

/* Variables and functions */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  FILE *f;
  char c;
  while (1) {
    f = fopen("/tmp/motd.output", "r");
    if (f != NULL) {
      break;
    }
  }
  printf("Got FD, sleeping for a moment.\n");
  usleep(1000000);
  while ((c = getc(f)) != EOF) {
    putchar(c);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119d <main+0x3d>
jmp    11a2 <main+0x42>
jmp    1176 <main+0x16>
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    -0x18(%rbp),%rdi
call   1070 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
mov    0x2e1a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11e7 <main+0x87>
movsbl -0x19(%rbp),%edi
call   1030 <putchar@plt>
jmp    11b8 <main+0x58>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%r14
lea    0xe9e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1180 <main+0x20>
mov    %rax,%rbx
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    %rbx,%rdi
call   1070 <getc@plt>
mov    0x2e30(%rip),%r14
cmp    (%r14),%al
je     11d5 <main+0x75>
nopl   (%rax)
movsbl %al,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1070 <getc@plt>
cmp    (%r14),%al
jne    11c0 <main+0x60>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/tmp/motd.output\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"Got FD, sleeping for a moment.\0A\00"
@EOF = global i8 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %8
8: ; preds = %2, %13
%9 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %8
br label %14
13: ; preds = %8
br label %8
14: ; preds = %12
%15 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @usleep(i32 1000000)
br label %17
17: ; preds = %24, %14
%18 = load i32*, i32** %6
%19 = call signext i8 @getc(i32* %18)
store i8 %19, i8* %7
%20 = sext i8 %19 to i32
%21 = load i8, i8* @EOF
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %20, %22
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i8, i8* %7
%26 = call i32 @putchar(i8 signext %25)
br label %17
27: ; preds = %17
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"/tmp/motd.output\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@str = [31 x i8] c"Got FD, sleeping for a moment.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %3, label %6
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @usleep(i32 1000000)
%9 = tail call signext i8 @getc(i32* %4)
%10 = load i8, i8* @EOF
%11 = icmp eq i8 %9, %10
br i1 %11, label %18, label %12
12: ; preds = %6, %12
%13 = phi i8 [ %15, %12 ], [ %9, %6 ]
%14 = tail call i32 @putchar(i8 signext %13)
%15 = tail call signext i8 @getc(i32* %4)
%16 = load i8, i8* @EOF
%17 = icmp eq i8 %15, %16
br i1 %17, label %18, label %12
18: ; preds = %12, %6
%19 = tail call i32 @fclose(i32* %4)
ret i32 0
}