#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_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, ...);
char *strerrno(int);
int test_fstat() {
  int fd, ret;
  struct stat st;
  errno = 0;
  fd = open("foo.fileio.test", O_RDONLY);
  if (fd >= 0) {
    errno = 0;
    ret = fstat(fd, &st);
    if (!ret)
      printf("fstat 1: ret = %d, errno = %d %s\n", ret, errno,
             st.st_size == 11 ? "OK" : "");
    else
      printf("fstat 1: ret = %d, errno = %d\n", ret, errno);
    close(fd);
  } else
    printf("fstat 1: ret = %d, errno = %d\n", ret, errno);
  errno = 0;
  ret = fstat(999, &st);
  printf("fstat 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
}


x86 assembly (clang, -O0):
<test_fstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     120b <test_fstat+0xbb>
mov    0x2e4a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11e4 <test_fstat+0x94>
mov    -0xc(%rbp),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%edi
lea    0xe90(%rip),%rcx
lea    0xe68(%rip),%rax
cmp    $0xb,%edi
cmove  %rax,%rcx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fe <test_fstat+0xae>
mov    -0xc(%rbp),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    1225 <test_fstat+0xd5>
mov    -0xc(%rbp),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dac(%rip),%rax
movl   $0x0,(%rax)
mov    $0x3e7,%edi
lea    -0x10(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fstat>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a6 <test_fstat+0x56>
mov    %eax,%ebx
movl   $0x0,0x0(%rbp)
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    0x0(%rbp),%edx
test   %eax,%eax
je     11b9 <test_fstat+0x69>
lea    0xe9a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11df <test_fstat+0x8f>
mov    0x0(%rbp),%edx
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e6 <test_fstat+0x96>
cmpl   $0xb,(%rsp)
lea    0xe6e(%rip),%rax
lea    0xe88(%rip),%rcx
cmove  %rax,%rcx
lea    0xe3a(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
movl   $0x0,0x0(%rbp)
mov    %rsp,%rsi
mov    $0x3e7,%edi
call   1070 <fstat@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe47(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"fstat 1: ret = %d, errno = %d %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [31 x i8] c"fstat 1: ret = %d, errno = %d\0A\00"
@.str.5 = [34 x i8] c"fstat 2: ret = %d, errno = %d %s\0A\00"
define i32 @test_fstat() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
store i32 0, i32* @errno
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %0
store i32 0, i32* @errno
%10 = load i32, i32* %2
%11 = call i32 @fstat(i32 %10, %struct.stat* %4)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %9
%15 = load i32, i32* %3
%16 = load i32, i32* @errno
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 11
%20 = zext i1 %19 to i64
%21 = select i1 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%22 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16, i8* %21)
br label %27
23: ; preds = %9
%24 = load i32, i32* %3
%25 = load i32, i32* @errno
%26 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %25)
br label %27
27: ; preds = %23, %14
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
br label %34
30: ; preds = %0
%31 = load i32, i32* %3
%32 = load i32, i32* @errno
%33 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %32)
br label %34
34: ; preds = %30, %27
store i32 0, i32* @errno
%35 = call i32 @fstat(i32 999, %struct.stat* %4)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = load i32, i32* @errno
%38 = load i32, i32* @errno
%39 = call i8* @strerrno(i32 %38)
%40 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %37, i8* %39)
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"fstat 1: ret = %d, errno = %d %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [31 x i8] c"fstat 1: ret = %d, errno = %d\0A\00"
@.str.5 = [34 x i8] c"fstat 2: ret = %d, errno = %d %s\0A\00"
define i32 @test_fstat() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
store i32 0, i32* @errno
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %20
6: ; preds = %0
store i32 0, i32* @errno
%7 = call i32 @fstat(i32 %4, %struct.stat* %1)
%8 = icmp eq i32 %7, 0
%9 = load i32, i32* @errno
br i1 %8, label %10, label %16
10: ; preds = %6
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, 11
%14 = select i1 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%15 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %9, i8* %14)
br label %18
16: ; preds = %6
%17 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %7, i32 %9)
br label %18
18: ; preds = %16, %10
%19 = tail call i32 @close(i32 %4)
br label %23
20: ; preds = %0
%21 = load i32, i32* @errno
%22 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 undef, i32 %21)
br label %23
23: ; preds = %20, %18
store i32 0, i32* @errno
%24 = call i32 @fstat(i32 999, %struct.stat* %1)
%25 = load i32, i32* @errno
%26 = tail call i8* @strerrno(i32 %25)
%27 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %24, i32 %25, i8* %26)
ret i32 undef
}