#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 */

/* Variables and functions */
int EBADF;
int EINVAL;
int close(int);
int errno;
int ut_file_fd_close(int *fd) {
  int ret;
  if (NULL == fd)
    return -EINVAL;
  if (-1 == *fd) {
    ret = -EBADF;
    goto out;
  }
  if (*fd == 0) {
    ret = -EBADF;
    goto out;
  }
  ret = close(*fd);
  if (ret == -1) {
    ret = -errno;
    goto out;
  }
  ret = 0;
out:
  *fd = -1;
  return ret;
}


x86 assembly (clang, -O0):
<ut_file_fd_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    113b <ut_file_fd_close+0x2b>
xor    %eax,%eax
mov    0x2ea7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <ut_file_fd_close+0xb1>
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%eax
cmp    (%rcx),%eax
jne    115f <ut_file_fd_close+0x4f>
xor    %eax,%eax
mov    0x2e7b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    11b1 <ut_file_fd_close+0xa1>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    117f <ut_file_fd_close+0x6f>
xor    %eax,%eax
mov    0x2e5b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    11b1 <ut_file_fd_close+0xa1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11aa <ut_file_fd_close+0x9a>
xor    %eax,%eax
mov    0x2e40(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    11b1 <ut_file_fd_close+0xa1>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ut_file_fd_close>:
push   %rbx
test   %rdi,%rdi
je     1131 <ut_file_fd_close+0x21>
mov    %rdi,%rbx
mov    (%rdi),%edi
test   %edi,%edi
je     1124 <ut_file_fd_close+0x14>
cmp    $0xffffffff,%edi
jne    113e <ut_file_fd_close+0x2e>
mov    0x2ea5(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    114c <ut_file_fd_close+0x3c>
mov    0x2ea0(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret
call   1030 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1154 <ut_file_fd_close+0x44>
movl   $0xffffffff,(%rbx)
pop    %rbx
ret
mov    0x2e85(%rip),%rcx
jmp    112b <ut_file_fd_close+0x1b>

LLVM IR (-O0):
@EINVAL = global i32 0
@EBADF = global i32 0
@errno = global i32 0
define i32 @ut_file_fd_close(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = icmp eq i32* null, %5
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
store i32 %9, i32* %2
br label %37
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = load i32, i32* %11
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @EBADF
%16 = sub nsw i32 0, %15
store i32 %16, i32* %4
br label %34
17: ; preds = %10
%18 = load i32*, i32** %3
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @EBADF
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %34
24: ; preds = %17
%25 = load i32*, i32** %3
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %4
br label %34
33: ; preds = %24
store i32 0, i32* %4
br label %34
34: ; preds = %33, %30, %21, %14
%35 = load i32*, i32** %3
store i32 -1, i32* %35
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %37
37: ; preds = %34, %7
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@EINVAL = global i32 0
@EBADF = global i32 0
@errno = global i32 0
define i32 @ut_file_fd_close(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @EINVAL
%5 = sub nsw i32 0, %4
br label %22
6: ; preds = %1
%7 = load i32, i32* %0
switch i32 %7, label %14 [
i32 -1, label %8
i32 0, label %11
]
8: ; preds = %6
%9 = load i32, i32* @EBADF
%10 = sub nsw i32 0, %9
br label %20
11: ; preds = %6
%12 = load i32, i32* @EBADF
%13 = sub nsw i32 0, %12
br label %20
14: ; preds = %6
%15 = tail call i32 @close(i32 %7)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %20
20: ; preds = %14, %17, %11, %8
%21 = phi i32 [ %10, %8 ], [ %13, %11 ], [ %19, %17 ], [ 0, %14 ]
store i32 -1, i32* %0
br label %22
22: ; preds = %20, %3
%23 = phi i32 [ %5, %3 ], [ %21, %20 ]
ret i32 %23
}