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

/* Variables and functions */
int EBADF;
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
int /*<<< orphan*/ printf(char *, int, ...);
int main() {
  int i = 0;
  close(2);
  printf("EBADF = [%d]\n", EBADF);
  for (i = 0; i < 6; i++) {
    errno = 0;
    struct stat buf;
    int r = fstat(i, &buf);
    printf("ret : [%d] of fd [%d], errno = [%d]\n", r, i, errno);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x6,-0x8(%rbp)
jge    11bf <main+0x8f>
mov    0x2e61(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    0x2e3f(%rip),%rax
mov    (%rax),%ecx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116e <main+0x3e>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e7f(%rip),%rbx
movl   $0x0,(%rbx)
lea    0x8(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    (%rbx),%ecx
lea    0xe8f(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    $0x1,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    (%rbx),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    $0x2,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    (%rbx),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    $0x3,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    (%rbx),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x3,%edx
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    $0x4,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    (%rbx),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    $0x5,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    (%rbx),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"EBADF = [%d]\0A\00"
@EBADF = global i32 0
@errno = global i32 0
@.str.1 = [37 x i8] c"ret : [%d] of fd [%d], errno = [%d]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = call i32 @close(i32 2)
%6 = load i32, i32* @EBADF
%7 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 0, i32* %2
br label %8
8: ; preds = %18, %0
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 6
br i1 %10, label %11, label %21
11: ; preds = %8
store i32 0, i32* @errno
%12 = load i32, i32* %2
%13 = call i32 @fstat(i32 %12, %struct.stat* %3)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = load i32, i32* %2
%16 = load i32, i32* @errno
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15, i32 %16)
br label %18
18: ; preds = %11
%19 = load i32, i32* %2
%20 = add nsw i32 %19, 1
store i32 %20, i32* %2
br label %8
21: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"EBADF = [%d]\0A\00"
@EBADF = global i32 0
@errno = global i32 0
@.str.1 = [37 x i8] c"ret : [%d] of fd [%d], errno = [%d]\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = tail call i32 @close(i32 2)
%3 = load i32, i32* @EBADF
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = bitcast %struct.stat* %1 to i8*
store i32 0, i32* @errno
%6 = call i32 @fstat(i32 0, %struct.stat* %1)
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 0, i32 %7)
store i32 0, i32* @errno
%9 = call i32 @fstat(i32 1, %struct.stat* %1)
%10 = load i32, i32* @errno
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 1, i32 %10)
store i32 0, i32* @errno
%12 = call i32 @fstat(i32 2, %struct.stat* %1)
%13 = load i32, i32* @errno
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 2, i32 %13)
store i32 0, i32* @errno
%15 = call i32 @fstat(i32 3, %struct.stat* %1)
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 3, i32 %16)
store i32 0, i32* @errno
%18 = call i32 @fstat(i32 4, %struct.stat* %1)
%19 = load i32, i32* @errno
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 4, i32 %19)
store i32 0, i32* @errno
%21 = call i32 @fstat(i32 5, %struct.stat* %1)
%22 = load i32, i32* @errno
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 5, i32 %22)
ret i32 0
}