#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_ino;
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: ./a.out /path/to/file\n");
    return 1;
  }
  int fd = open(argv[1], O_RDONLY);
  struct stat stat;
  int ret = fstat(fd, &stat);
  if (ret) {
    perror("fstat failed");
    return 1;
  }
  printf("inode number of fd = %d is %d\n", fd, stat.st_ino);
  printf("size of fd = %d is %ld\n", fd, stat.st_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121e <main+0xbe>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11eb <main+0x8b>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    121e <main+0xbe>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    11bd <main+0x5d>
mov    0x8(%rsi),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %eax,%eax
jne    11dd <main+0x7d>
mov    0x8(%rsp),%edx
lea    0xe92(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    0xc(%rsp),%edx
lea    0xe9f(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11d7 <main+0x77>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
jmp    11d2 <main+0x72>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Usage: ./a.out /path/to/file\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fstat failed\00"
@.str.2 = [31 x i8] c"inode number of fd = %d is %d\0A\00"
@.str.3 = [24 x i8] c"size of fd = %d is %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %37
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = call i32 @fstat(i32 %20, %struct.stat* %7)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %37
26: ; preds = %14
%27 = load i32, i32* %6
%28 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %29)
%31 = load i32, i32* %6
%32 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%33 = load i32, i32* %32
%34 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %33)
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %26, %24, %11
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Usage: ./a.out /path/to/file\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fstat failed\00"
@.str.2 = [31 x i8] c"inode number of fd = %d is %d\0A\00"
@.str.3 = [24 x i8] c"size of fd = %d is %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %28
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = bitcast %struct.stat* %3 to i8*
%14 = call i32 @fstat(i32 %12, %struct.stat* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %26
18: ; preds = %8
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %20)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%23 = load i32, i32* %22
%24 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 %23)
%25 = tail call i32 @close(i32 %12)
br label %26
26: ; preds = %18, %16
%27 = phi i32 [ 1, %16 ], [ 0, %18 ]
br label %28
28: ; preds = %26, %5
%29 = phi i32 [ 1, %5 ], [ %27, %26 ]
ret i32 %29
}