#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_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ pthread_exit(int *);
char *strerror(int /*<<< orphan*/);
void *test_openoneM(void *arg) {
  int fd, retval = 0;
  fd = open("/dev/OPEN_ONCE", O_RDONLY);
  if (fd < 0) {
    printf("open "
           "/dev/OPEN_ONCE"
           " is not possible, %s.\n",
           strerror(errno));
    retval = -1;
    pthread_exit(&retval);
  }
  if (close(fd) < 0) {
    printf("close "
           "/dev/OPEN_ONCE"
           " is not possible, %s.\n",
           strerror(errno));
    retval = -1;
    pthread_exit(&retval);
  }
  pthread_exit(&retval);
}


x86 assembly (clang, -O0):
<test_openoneM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b2 <test_openoneM+0x62>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pthread_exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    11f1 <test_openoneM+0xa1>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pthread_exit@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_openoneM>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a7 <test_openoneM+0x57>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe82(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rdi
call   1050 <pthread_exit@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11e2 <test_openoneM+0x92>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe71(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rdi
call   1050 <pthread_exit@plt>
lea    0xc(%rsp),%rdi
call   1050 <pthread_exit@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/OPEN_ONCE\00"
@O_RDONLY = global i32 0
@.str.1 = [42 x i8] c"open /dev/OPEN_ONCE is not possible, %s.\0A\00"
@errno = global i32 0
@.str.2 = [43 x i8] c"close /dev/OPEN_ONCE is not possible, %s.\0A\00"
define i8* @test_openoneM(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 -1, i32* %5
%14 = call i32 @pthread_exit(i32* %5)
br label %15
15: ; preds = %10, %1
%16 = load i32, i32* %4
%17 = call i64 @close(i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
%23 = call i32 @pthread_exit(i32* %5)
br label %24
24: ; preds = %19, %15
%25 = call i32 @pthread_exit(i32* %5)
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/OPEN_ONCE\00"
@O_RDONLY = global i32 0
@.str.1 = [42 x i8] c"open /dev/OPEN_ONCE is not possible, %s.\0A\00"
@errno = global i32 0
@.str.2 = [43 x i8] c"close /dev/OPEN_ONCE is not possible, %s.\0A\00"
define noalias i8* @test_openoneM(i8* nocapture readnone %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %9)
store i32 -1, i32* %2
%11 = call i32 @pthread_exit(i32* %2)
br label %12
12: ; preds = %7, %1
%13 = call i64 @close(i32 %5)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %17)
store i32 -1, i32* %2
%19 = call i32 @pthread_exit(i32* %2)
br label %20
20: ; preds = %15, %12
%21 = call i32 @pthread_exit(i32* %2)
ret i8* undef
}