#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 /*<<< orphan*/ F_GETFD;
int atoi(char *);
int errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int *);
int test_open(char *arg) {
  int fd, rc;
  int status;
  extern char *optarg;
  extern int optind;
  fd = atoi(arg);
  rc = fcntl(fd, F_GETFD, &status);
  if (rc == -1 && errno == EBADF) {
    exit(0);
  }
  if (rc != -1)
    exit(3);
  exit(errno);
  return -1;
}


x86 assembly (clang, -O0):
<test_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <atoi@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1189 <test_open+0x59>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
jne    1189 <test_open+0x59>
xor    %edi,%edi
call   1050 <exit@plt>
cmpl   $0xffffffff,-0x10(%rbp)
je     119d <test_open+0x6d>
mov    $0x3,%edi
call   1050 <exit@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<test_open>:
push   %rax
call   1040 <atoi@plt>
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%esi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    116b <test_open+0x3b>
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
cmp    (%rax),%edi
jne    1170 <test_open+0x40>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x3,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@errno = global i32 0
@EBADF = global i32 0
define i32 @test_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @atoi(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = load i32, i32* @F_GETFD
%10 = call i32 @fcntl(i32 %8, i32 %9, i32* %5)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = load i32, i32* @EBADF
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %13, %1
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @exit(i32 3)
unreachable
24: ; preds = %19
%25 = load i32, i32* @errno
%26 = call i32 @exit(i32 %25)
unreachable
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@errno = global i32 0
@EBADF = global i32 0
define i32 @test_open(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @atoi(i8* %0)
%5 = load i32, i32* @F_GETFD
%6 = call i32 @fcntl(i32 %4, i32 %5, i32* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = load i32, i32* @EBADF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %1
%15 = call i32 @exit(i32 3)
unreachable
16: ; preds = %8
%17 = call i32 @exit(i32 %9)
unreachable
}