#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;
struct stat *F_GETFD;
int /*<<< orphan*/ __procfdname(char *, int);
int __syscall(int, int, struct stat *);
int __syscall_ret(int);
int syscall(int, char *, struct stat *);
int fstat(int fd, struct stat *st) {
  int ret = __syscall(108, fd, st);
  if (ret != -EBADF || __syscall(55, fd, F_GETFD) < 0)
    return __syscall_ret(ret);
  char buf[15 + 3 * sizeof(int)];
  __procfdname(buf, fd);
  return syscall(106, buf, st);
}


x86 assembly (clang, -O0):
<fstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    $0x6c,%edi
call   1040 <__syscall@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e69(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1199 <fstat+0x59>
mov    -0x8(%rbp),%esi
mov    0x2e5d(%rip),%rax
mov    (%rax),%rdx
mov    $0x37,%edi
call   1040 <__syscall@plt>
cmp    $0x0,%eax
jge    11a9 <fstat+0x69>
mov    -0x14(%rbp),%edi
call   1060 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    11ca <fstat+0x8a>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <__procfdname@plt>
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x6a,%edi
call   1050 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fstat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x6c,%edi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <__syscall@plt>
mov    %eax,%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
add    %ebx,%eax
jne    11ab <fstat+0x6b>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rdx
mov    $0x37,%edi
mov    %ebp,%esi
call   1040 <__syscall@plt>
test   %eax,%eax
js     11ab <fstat+0x6b>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <__procfdname@plt>
mov    $0x6a,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <syscall@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <__syscall_ret@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EBADF = global i32 0
@F_GETFD = global %struct.stat* null
define i32 @fstat(i32 %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat*
%6 = alloca i32
%7 = alloca [27 x i8]
store i32 %0, i32* %4
store %struct.stat* %1, %struct.stat** %5
%8 = load i32, i32* %4
%9 = load %struct.stat*, %struct.stat** %5
%10 = call i32 @__syscall(i32 108, i32 %8, %struct.stat* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = load i32, i32* @EBADF
%13 = sub nsw i32 0, %12
%14 = icmp ne i32 %11, %13
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load %struct.stat*, %struct.stat** @F_GETFD
%18 = call i32 @__syscall(i32 55, i32 %16, %struct.stat* %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15, %2
%21 = load i32, i32* %6
%22 = call i32 @__syscall_ret(i32 %21)
store i32 %22, i32* %3
br label %30
23: ; preds = %15
%24 = getelementptr inbounds [27 x i8], [27 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* %4
%26 = call i32 @__procfdname(i8* %24, i32 %25)
%27 = getelementptr inbounds [27 x i8], [27 x i8]* %7, i64 0, i64 0
%28 = load %struct.stat*, %struct.stat** %5
%29 = call i32 @syscall(i32 106, i8* %27, %struct.stat* %28)
store i32 %29, i32* %3
br label %30
30: ; preds = %23, %20
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EBADF = global i32 0
@F_GETFD = global %struct.stat* null
define i32 @fstat(i32 %0, %struct.stat* %1) {
%3 = alloca [27 x i8]
%4 = tail call i32 @__syscall(i32 108, i32 %0, %struct.stat* %1)
%5 = load i32, i32* @EBADF
%6 = sub nsw i32 0, %5
%7 = icmp eq i32 %4, %6
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load %struct.stat*, %struct.stat** @F_GETFD
%10 = tail call i32 @__syscall(i32 55, i32 %0, %struct.stat* %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8, %2
%13 = tail call i32 @__syscall_ret(i32 %4)
br label %18
14: ; preds = %8
%15 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 0
%16 = call i32 @__procfdname(i8* %15, i32 %0)
%17 = call i32 @syscall(i32 106, i8* %15, %struct.stat* %1)
br label %18
18: ; preds = %14, %12
%19 = phi i32 [ %13, %12 ], [ %17, %14 ]
ret i32 %19
}