#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 void FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(void *);
void *fgets(char *, int, void *);
void *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  char buf[100];
  FILE *fp;
  long x;
  fp = fopen("temp.in", "r");
  if (fp == ((void *)0)) {
    printf("can't open temp.in\n");
    return (0);
  }
  x = 0;
  while (fgets(buf, sizeof buf, fp) != ((void *)0)) {
    x++;
  }
  fclose(fp);
  printf("x is %ld\n", x);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    118e <main+0x4e>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <main+0xa5>
movq   $0x0,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11c3 <main+0x83>
mov    -0x80(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x80(%rbp)
jmp    1196 <main+0x56>
mov    -0x78(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x80(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
lea    0xeb7(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b4 <main+0x64>
mov    %rax,%r14
mov    $0xffffffffffffffff,%rbx
mov    %rsp,%r15
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x64,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1180 <main+0x30>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe5a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11c0 <main+0x70>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"temp.in\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"can't open temp.in\0A\00"
@.str.3 = [10 x i8] c"x is %ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
%5 = call i8* @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 i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %24
10: ; preds = %0
store i64 0, i64* %4
br label %11
11: ; preds = %16, %10
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%13 = load i8*, i8** %3
%14 = call i8* @fgets(i8* %12, i32 100, i8* %13)
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i64, i64* %4
%18 = add nsw i64 %17, 1
store i64 %18, i64* %4
br label %11
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = call i32 @fclose(i8* %20)
%22 = load i64, i64* %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %22)
store i32 0, i32* %1
br label %24
24: ; preds = %19, %8
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [8 x i8] c"temp.in\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [10 x i8] c"x is %ld\0A\00"
@str = [19 x i8] c"can't open temp.in\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @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))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %15
7: ; preds = %0, %7
%8 = phi i64 [ %11, %7 ], [ 0, %0 ]
%9 = call i8* @fgets(i8* %2, i32 100, i8* %3)
%10 = icmp eq i8* %9, null
%11 = add nuw nsw i64 %8, 1
br i1 %10, label %12, label %7
12: ; preds = %7
%13 = call i32 @fclose(i8* %3)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %8)
br label %15
15: ; preds = %12, %5
ret i32 0
}