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


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   $0x0,-0x1c(%rbp)
jge    116a <stat+0x3a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <stat+0x6f>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e41(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1174 <stat+0x44>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <fstat@plt>
mov    %eax,%r15d
mov    0x2e76(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,(%r14)
mov    %r15d,%eax
jmp    1176 <stat+0x46>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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
store i8* %0, i8** %4
store %struct.stat* %1, %struct.stat** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %6
store i32 %15, i32* %3
br label %25
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)
%23 = load i32, i32* %8
store i32 %23, i32* @errno
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %25
25: ; preds = %16, %14
%26 = load i32, i32* %3
ret i32 %26
}

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 slt i32 %4, 0
br i1 %5, label %10, 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)
store i32 %8, i32* @errno
br label %10
10: ; preds = %2, %6
%11 = phi i32 [ %7, %6 ], [ %4, %2 ]
ret i32 %11
}