#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 */
typedef int pid_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ altera_sc_clear_status();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, int);
int open_berictl_dir(int /*<<< orphan*/);
int openat(int, char *, int, int);
int /*<<< orphan*/ warn(char *, char *);
int altera_sc_write_status(pid_t pid, int port) {
  int ccd, pidfd, portfd, ret;
  ccd = pidfd = portfd = -1;
  ret = -1;
  if ((ccd = open_berictl_dir(0)) == -1)
    goto error;
  if ((pidfd = openat(ccd, "system-console.pid", O_WRONLY | O_CREAT | O_TRUNC,
                      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) == -1) {
    warn("can't open %s", "system-console.pid");
    goto error;
  }
  if ((portfd = openat(ccd, "system-console.port", O_WRONLY | O_CREAT | O_TRUNC,
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) ==
      -1) {
    warn("can't open %s", "system-console.port");
    goto error;
  }
  dprintf(pidfd, "%d\n", pid);
  dprintf(portfd, "%d\n", port);
  ret = 0;
error:
  close(ccd);
  close(pidfd);
  close(portfd);
  if (ret != 0)
    altera_sc_clear_status();
  return (ret);
}


x86 assembly (clang, -O0):
<altera_sc_write_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x18(%rbp)
xor    %edi,%edi
call   1070 <open_berictl_dir@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11a2 <altera_sc_write_status+0x42>
jmp    12c3 <altera_sc_write_status+0x163>
mov    -0xc(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
mov    0x2e43(%rip),%rax
or     (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    0x2de9(%rip),%rax
mov    (%rax),%ecx
mov    0x2e00(%rip),%rax
or     (%rax),%ecx
mov    0x2de7(%rip),%rax
or     (%rax),%ecx
mov    0x2df6(%rip),%rax
or     (%rax),%ecx
mov    0x2dcd(%rip),%rax
or     (%rax),%ecx
lea    0xe0c(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    121d <altera_sc_write_status+0xbd>
lea    0xe07(%rip),%rdi
lea    0xded(%rip),%rsi
call   1030 <warn@plt>
jmp    12c3 <altera_sc_write_status+0x163>
mov    -0xc(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    0x2dc8(%rip),%rax
or     (%rax),%edx
mov    0x2db7(%rip),%rax
or     (%rax),%edx
mov    0x2d6e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d85(%rip),%rax
or     (%rax),%ecx
mov    0x2d6c(%rip),%rax
or     (%rax),%ecx
mov    0x2d7b(%rip),%rax
or     (%rax),%ecx
mov    0x2d52(%rip),%rax
or     (%rax),%ecx
lea    0xdb2(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1298 <altera_sc_write_status+0x138>
lea    0xd8c(%rip),%rdi
lea    0xd93(%rip),%rsi
call   1030 <warn@plt>
jmp    12c3 <altera_sc_write_status+0x163>
mov    -0x10(%rbp),%edi
mov    -0x4(%rbp),%edx
lea    0xd90(%rip),%rsi
call   1060 <dprintf@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%edx
lea    0xd7e(%rip),%rsi
call   1060 <dprintf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x18(%rbp)
je     12ec <altera_sc_write_status+0x18c>
mov    $0x0,%al
call   1080 <altera_sc_clear_status@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<altera_sc_write_status>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %r12d,%r12d
xor    %edi,%edi
call   1070 <open_berictl_dir@plt>
mov    %eax,%r13d
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     1283 <altera_sc_write_status+0x123>
mov    %ebp,(%rsp)
mov    %ebx,0x4(%rsp)
mov    0x2e07(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e45(%rip),%rax
or     (%rax),%edx
mov    0x2dfc(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dfa(%rip),%r14
or     (%r14),%ecx
mov    0x2e08(%rip),%r12
or     (%r12),%ecx
mov    0x2ddd(%rip),%rbx
or     (%rbx),%ecx
lea    0xe1c(%rip),%rsi
mov    %r13d,%edi
call   1040 <openat@plt>
cmp    $0xffffffff,%eax
je     126d <altera_sc_write_status+0x10d>
mov    %eax,%ebp
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    0x2d9d(%rip),%rax
or     (%rax),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
mov    0x2d92(%rip),%rax
or     (%rax),%ecx
or     (%r14),%ecx
or     (%r12),%ecx
or     (%rbx),%ecx
lea    0xdf1(%rip),%rsi
mov    %r13d,%edi
call   1040 <openat@plt>
cmp    $0xffffffff,%eax
je     128a <altera_sc_write_status+0x12a>
mov    %eax,%r14d
lea    0xdee(%rip),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    (%rsp),%edx
call   1060 <dprintf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    0x4(%rsp),%edx
call   1060 <dprintf@plt>
xor    %ebx,%ebx
mov    $0x1,%r12b
mov    %r14d,%r15d
jmp    12a5 <altera_sc_write_status+0x145>
lea    0xd9f(%rip),%rdi
lea    0xd85(%rip),%rsi
call   1030 <warn@plt>
xor    %r12d,%r12d
mov    $0xffffffff,%ebp
jmp    12a0 <altera_sc_write_status+0x140>
lea    0xd82(%rip),%rdi
lea    0xd89(%rip),%rsi
call   1030 <warn@plt>
xor    %r12d,%r12d
mov    $0xffffffff,%ebx
mov    %r13d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
test   %r12b,%r12b
jne    12c8 <altera_sc_write_status+0x168>
xor    %eax,%eax
call   1080 <altera_sc_clear_status@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"system-console.pid\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [20 x i8] c"system-console.port\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @altera_sc_write_status(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 -1, i32* %7
store i32 -1, i32* %6
store i32 -1, i32* %5
store i32 -1, i32* %8
%9 = call i32 @open_berictl_dir(i32 0)
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
br label %59
12: ; preds = %2
%13 = load i32, i32* %5
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IWGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IROTH
%27 = or i32 %25, %26
%28 = call i32 @openat(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %27)
store i32 %28, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %12
%31 = call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %59
32: ; preds = %12
%33 = load i32, i32* %5
%34 = load i32, i32* @O_WRONLY
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRUSR
%40 = load i32, i32* @S_IWUSR
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRGRP
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IWGRP
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IROTH
%47 = or i32 %45, %46
%48 = call i32 @openat(i32 %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %38, i32 %47)
store i32 %48, i32* %7
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %32
%51 = call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %59
52: ; preds = %32
%53 = load i32, i32* %6
%54 = load i32, i32* %3
%55 = call i32 @dprintf(i32 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %7
%57 = load i32, i32* %4
%58 = call i32 @dprintf(i32 %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %57)
store i32 0, i32* %8
br label %59
59: ; preds = %52, %50, %30, %11
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %8
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %59
%69 = call i32 (...) @altera_sc_clear_status()
br label %70
70: ; preds = %68, %59
%71 = load i32, i32* %8
ret i32 %71
}

LLVM IR (-O2):
@.str = [19 x i8] c"system-console.pid\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [20 x i8] c"system-console.port\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @altera_sc_write_status(i32 %0, i32 %1) {
%3 = tail call i32 @open_berictl_dir(i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %46, label %5
5: ; preds = %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = tail call i32 @openat(i32 %3, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %5
%23 = tail call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %46
24: ; preds = %5
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %31, %30
%33 = load i32, i32* @S_IRGRP
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IWGRP
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IROTH
%38 = or i32 %36, %37
%39 = tail call i32 @openat(i32 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %24
%42 = tail call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %46
43: ; preds = %24
%44 = tail call i32 @dprintf(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%45 = tail call i32 @dprintf(i32 %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %1)
br label %46
46: ; preds = %2, %43, %41, %22
%47 = phi i32 [ -1, %2 ], [ -1, %22 ], [ %20, %41 ], [ %20, %43 ]
%48 = phi i32 [ -1, %2 ], [ -1, %22 ], [ -1, %41 ], [ %39, %43 ]
%49 = phi i1 [ false, %2 ], [ false, %22 ], [ false, %41 ], [ true, %43 ]
%50 = phi i32 [ -1, %2 ], [ -1, %22 ], [ -1, %41 ], [ 0, %43 ]
%51 = tail call i32 @close(i32 %3)
%52 = tail call i32 @close(i32 %47)
%53 = tail call i32 @close(i32 %48)
br i1 %49, label %56, label %54
54: ; preds = %46
%55 = tail call i32 (...) @altera_sc_clear_status()
br label %56
56: ; preds = %54, %46
ret i32 %50
}