#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 /*<<< orphan*/ O_RDONLY;
int close(int);
int errno;
int fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
int stat(const char *pathname, struct stat *buf) {
  int fd = open(pathname, O_RDONLY);
  if (fd == -1)
    return -1;
  int fstat_ret = fstat(fd, buf);
  int fstat_errno = errno;
  int close_ret = close(fd);
  if (fstat_ret == -1) {
    errno = fstat_errno;
    return -1;
  }
  if (close_ret == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    116b <stat+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <stat+0xa0>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11b3 <stat+0x83>
mov    -0x24(%rbp),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <stat+0xa0>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11c9 <stat+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <stat+0xa0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1188 <stat+0x58>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <fstat@plt>
mov    %eax,%r14d
mov    0x2e6f(%rip),%r15
mov    (%r15),%r12d
mov    %ebp,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%r14d
je     1185 <stat+0x55>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
neg    %ebx
jmp    1188 <stat+0x58>
mov    %r12d,(%r15)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @stat(i8* %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store %struct.stat* %1, %struct.stat** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %32
16: ; preds = %2
%17 = load i32, i32* %6
%18 = load %struct.stat*, %struct.stat** %5
%19 = call i32 @fstat(i32 %17, %struct.stat* %18)
store i32 %19, i32* %7
%20 = load i32, i32* @errno
store i32 %20, i32* %8
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = load i32, i32* %8
store i32 %26, i32* @errno
store i32 -1, i32* %3
br label %32
27: ; preds = %16
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -1, i32* %3
br label %32
31: ; preds = %27
store i32 0, i32* %3
br label %32
32: ; preds = %31, %30, %25, %15
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @stat(i8* nocapture %0, %struct.stat* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = tail call i32 @fstat(i32 %4, %struct.stat* %1)
%8 = load i32, i32* @errno
%9 = tail call i32 @close(i32 %4)
%10 = icmp eq i32 %7, -1
br i1 %10, label %11, label %12
11: ; preds = %6
store i32 %8, i32* @errno
br label %15
12: ; preds = %6
%13 = icmp eq i32 %9, -1
%14 = sext i1 %13 to i32
br label %15
15: ; preds = %11, %12, %2
%16 = phi i32 [ -1, %2 ], [ -1, %11 ], [ %14, %12 ]
ret i32 %16
}