#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 */
int /*<<< orphan*/ R_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ getchar();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ show_struct(int /*<<< orphan*/ *);
int show_file(const char *pFile) {
  if (access(pFile, R_OK) != 0) {
    printf("access file [%s] faild\n", pFile);
    return -1;
  }
  printf("my pid = [%d]\n", getpid());
  printf("you can run [ls -l /proc/%d/fd] to look "
         "which file is opend by this process\n",
         getpid());
  printf("Entering [Enter] to continue ...\n");
  getchar();
  FILE *fp = fopen(pFile, "r");
  if (NULL == fp) {
    printf("open file [%s] faild\n", pFile);
    return -1;
  }
  printf("sizeof(FILE) = [%lu]\n", sizeof(FILE));
  show_struct(fp);
  printf("you can run [ls -l /proc/%d/fd] again to look "
         "which file is opend by this process\n",
         getpid());
  printf("Entering [Enter] to exit...\n");
  getchar();
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<show_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     11b6 <show_file+0x46>
mov    -0x10(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <show_file+0x11f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1237 <show_file+0xc7>
mov    -0x10(%rbp),%rsi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <show_file+0x11f>
lea    0xe70(%rip),%rdi
mov    $0x4,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <show_struct@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
test   %rax,%rax
je     11b6 <show_file+0x36>
lea    0xe5f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%r14d
jmp    125e <show_file+0xde>
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe51(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe49(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xf07(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1266 <show_file+0xe6>
mov    %rax,%rbp
lea    0xe74(%rip),%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1050 <show_struct@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe68(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe09(%rip),%rdi
jmp    11a1 <show_file+0x21>

LLVM IR (-O0):
@R_OK = global i32 0
@.str = [24 x i8] c"access file [%s] faild\0A\00"
@.str.1 = [15 x i8] c"my pid = [%d]\0A\00"
@.str.2 = [77 x i8] c"you can run [ls -l /proc/%d/fd] to look which file is opend by this process\0A\00"
@.str.3 = [34 x i8] c"Entering [Enter] to continue ...\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [22 x i8] c"open file [%s] faild\0A\00"
@.str.6 = [22 x i8] c"sizeof(FILE) = [%lu]\0A\00"
@.str.7 = [83 x i8] c"you can run [ls -l /proc/%d/fd] again to look which file is opend by this process\0A\00"
@.str.8 = [29 x i8] c"Entering [Enter] to exit...\0A\00"
define i32 @show_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @R_OK
%7 = call i64 @access(i8* %5, i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
store i32 -1, i32* %2
br label %36
12: ; preds = %1
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 (...) @getchar()
%19 = load i8*, i8** %3
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %20, i32** %4
%21 = load i32*, i32** %4
%22 = icmp eq i32* null, %21
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %24)
store i32 -1, i32* %2
br label %36
26: ; preds = %12
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i64 4)
%28 = load i32*, i32** %4
%29 = call i32 @show_struct(i32* %28)
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.7, i64 0, i64 0), i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%33 = call i32 (...) @getchar()
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %2
br label %36
36: ; preds = %26, %23, %9
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@R_OK = global i32 0
@.str = [24 x i8] c"access file [%s] faild\0A\00"
@.str.1 = [15 x i8] c"my pid = [%d]\0A\00"
@.str.2 = [77 x i8] c"you can run [ls -l /proc/%d/fd] to look which file is opend by this process\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [22 x i8] c"open file [%s] faild\0A\00"
@.str.6 = [22 x i8] c"sizeof(FILE) = [%lu]\0A\00"
@.str.7 = [83 x i8] c"you can run [ls -l /proc/%d/fd] again to look which file is opend by this process\0A\00"
@str = [33 x i8] c"Entering [Enter] to continue ...\00"
@str.9 = [28 x i8] c"Entering [Enter] to exit...\00"
define i32 @show_file(i8* %0) {
%2 = load i32, i32* @R_OK
%3 = tail call i64 @access(i8* %0, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %26
7: ; preds = %1
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([77 x i8], [77 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 (...) @getchar()
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %26
18: ; preds = %7
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i64 4)
%20 = tail call i32 @show_struct(i32* %14)
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @.str.7, i64 0, i64 0), i32 %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.9, i64 0, i64 0))
%24 = tail call i32 (...) @getchar()
%25 = tail call i32 @fclose(i32* %14)
br label %26
26: ; preds = %16, %18, %5
%27 = phi i32 [ -1, %5 ], [ -1, %16 ], [ 0, %18 ]
ret i32 %27
}