#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 /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int simple_get_fd(void) {
  static int fd = -1;
  if (fd < 0) {
    if ((fd = open("/dev/simple_dev", O_RDWR)) < 0) {
      printf("/dev/simple_dev open failed \r\n");
      return (-1);
    }
  }
  return (fd);
}


x86 assembly (clang, -O0):
<simple_get_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0x0,0x2f01(%rip)
jge    1176 <simple_get_fd+0x56>
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
lea    0xebb(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ee0(%rip)
cmp    $0x0,%eax
jge    1171 <simple_get_fd+0x51>
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    117f <simple_get_fd+0x5f>
jmp    1176 <simple_get_fd+0x56>
mov    0x2eb4(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_get_fd>:
push   %rax
mov    0x2f09(%rip),%eax
test   %eax,%eax
js     112d <simple_get_fd+0xd>
pop    %rcx
ret
mov    0x2eb4(%rip),%rax
mov    (%rax),%esi
lea    0xec3(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ee8(%rip)
test   %eax,%eax
jns    112b <simple_get_fd+0xb>
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@simple_get_fd.fd = internal global i32 -1
@.str = [16 x i8] c"/dev/simple_dev\00"
@O_RDWR = global i32 0
@.str.1 = [31 x i8] c"/dev/simple_dev open failed \0D\0A\00"
define i32 @simple_get_fd() {
%1 = alloca i32
%2 = load i32, i32* @simple_get_fd.fd
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %11
4: ; preds = %0
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* @simple_get_fd.fd
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %13
10: ; preds = %4
br label %11
11: ; preds = %10, %0
%12 = load i32, i32* @simple_get_fd.fd
store i32 %12, i32* %1
br label %13
13: ; preds = %11, %8
%14 = load i32, i32* %1
ret i32 %14
}

LLVM IR (-O2):
@simple_get_fd.fd = internal unnamed_addr global i32 -1
@.str = [16 x i8] c"/dev/simple_dev\00"
@O_RDWR = global i32 0
@str = [30 x i8] c"/dev/simple_dev open failed \0D\00"
define i32 @simple_get_fd() {
%1 = load i32, i32* @simple_get_fd.fd
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* @simple_get_fd.fd
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %9
9: ; preds = %0, %3, %7
%10 = phi i32 [ -1, %7 ], [ %5, %3 ], [ %1, %0 ]
ret i32 %10
}