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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ isdigit(char);
scalar_t__ kill(void *, int /*<<< orphan*/);
int open_berictl_dir(int);
int openat(int, char *, int /*<<< orphan*/);
int read(int, char *, int);
void *strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ warn(char *, char *);
int /*<<< orphan*/ warnx(char *, char *);
int altera_sc_get_status(pid_t *pidp, int *portp) {
  int ccd, pidfd, portfd, port, ret;
  pid_t pid;
  char buf[8];
  if (pidp != NULL)
    *pidp = 0;
  if (portp != NULL)
    *portp = 0;
  ccd = pidfd = portfd = -1;
  ret = -1;
  if ((ccd = open_berictl_dir(1)) == -1)
    goto error;
  if ((pidfd = openat(ccd, "system-console.pid", O_RDONLY)) == -1)
    goto error;
  if (read(pidfd, buf, sizeof(buf)) < 1) {
    warn("failed to read %s", "system-console.pid");
    goto error;
  }
  if (!isdigit(buf[0])) {
    warnx("%s doesn't contain a number", "system-console.pid");
    goto error;
  }
  pid = strtol(buf, NULL, 10);
  if ((portfd = openat(ccd, "system-console.port", O_RDONLY)) == -1)
    goto error;
  if (read(portfd, buf, sizeof(buf)) < 1) {
    warn("failed to read %s", "system-console.pid");
    goto error;
  }
  if (!isdigit(buf[0])) {
    warnx("%s doesn't contain a number", "system-console.port");
    goto error;
  }
  port = strtol(buf, NULL, 10);
  if (kill(pid, 0) == 0) {
    if (pidp != NULL)
      *pidp = pid;
    if (portp != NULL)
      *portp = port;
    ret = 0;
  }
error:
  close(ccd);
  close(pidfd);
  close(portfd);
  return (ret);
}


x86 assembly (clang, -O0):
<altera_sc_get_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b6 <altera_sc_get_status+0x26>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11cb <altera_sc_get_status+0x3b>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    $0x1,%edi
call   10b0 <open_berictl_dir@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1202 <altera_sc_get_status+0x72>
jmp    1382 <altera_sc_get_status+0x1f2>
mov    -0x14(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    0xdeb(%rip),%rsi
call   1050 <openat@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    122b <altera_sc_get_status+0x9b>
jmp    1382 <altera_sc_get_status+0x1f2>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
jge    125d <altera_sc_get_status+0xcd>
lea    0xdc7(%rip),%rdi
lea    0xdad(%rip),%rsi
call   1040 <warn@plt>
jmp    1382 <altera_sc_get_status+0x1f2>
movsbl -0x38(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1287 <altera_sc_get_status+0xf7>
lea    0xdaf(%rip),%rdi
lea    0xd83(%rip),%rsi
call   1060 <warnx@plt>
jmp    1382 <altera_sc_get_status+0x1f2>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    0xd91(%rip),%rsi
call   1050 <openat@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12c6 <altera_sc_get_status+0x136>
jmp    1382 <altera_sc_get_status+0x1f2>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
jge    12f8 <altera_sc_get_status+0x168>
lea    0xd2c(%rip),%rdi
lea    0xd12(%rip),%rsi
call   1040 <warn@plt>
jmp    1382 <altera_sc_get_status+0x1f2>
movsbl -0x38(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1322 <altera_sc_get_status+0x192>
lea    0xd14(%rip),%rdi
lea    0xd29(%rip),%rsi
call   1060 <warnx@plt>
jmp    1382 <altera_sc_get_status+0x1f2>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
call   10a0 <kill@plt>
cmp    $0x0,%rax
jne    137d <altera_sc_get_status+0x1ed>
cmpq   $0x0,-0x8(%rbp)
je     1362 <altera_sc_get_status+0x1d2>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1376 <altera_sc_get_status+0x1e6>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x24(%rbp)
jmp    1382 <altera_sc_get_status+0x1f2>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<altera_sc_get_status>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
test   %rdi,%rdi
je     11b0 <altera_sc_get_status+0x20>
movq   $0x0,(%rbx)
test   %r12,%r12
je     11bd <altera_sc_get_status+0x2d>
movl   $0x0,(%r12)
mov    $0x1,%edi
call   10b0 <open_berictl_dir@plt>
mov    %eax,%ebp
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     12c4 <altera_sc_get_status+0x134>
mov    0x2e09(%rip),%r13
mov    0x0(%r13),%edx
lea    0xe16(%rip),%rsi
mov    %ebp,%edi
call   1050 <openat@plt>
cmp    $0xffffffff,%eax
je     12c4 <altera_sc_get_status+0x134>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12f9 <altera_sc_get_status+0x169>
movsbl (%rsp),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     1314 <altera_sc_get_status+0x184>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,0x8(%rsp)
mov    0x0(%r13),%edx
lea    0xdfd(%rip),%rsi
mov    %ebp,%edi
call   1050 <openat@plt>
cmp    $0xffffffff,%eax
je     1327 <altera_sc_get_status+0x197>
mov    %eax,%r13d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    132f <altera_sc_get_status+0x19f>
movsbl (%rsp),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     1344 <altera_sc_get_status+0x1b4>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   10a0 <kill@plt>
test   %rax,%rax
jne    12d0 <altera_sc_get_status+0x140>
mov    0x10(%rsp),%rcx
test   %rbx,%rbx
je     12b6 <altera_sc_get_status+0x126>
mov    0x8(%rsp),%rax
mov    %rax,(%rbx)
xor    %r14d,%r14d
test   %r12,%r12
je     12d0 <altera_sc_get_status+0x140>
mov    %ecx,(%r12)
jmp    12d0 <altera_sc_get_status+0x140>
mov    $0xffffffff,%r13d
mov    $0xffffffff,%r15d
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd13(%rip),%rdi
lea    0xcf9(%rip),%rsi
call   1040 <warn@plt>
mov    $0xffffffff,%r13d
jmp    12d0 <altera_sc_get_status+0x140>
lea    0xd0a(%rip),%rdi
lea    0xcde(%rip),%rsi
call   1060 <warnx@plt>
mov    $0xffffffff,%r13d
jmp    12d0 <altera_sc_get_status+0x140>
lea    0xcdd(%rip),%rdi
lea    0xcc3(%rip),%rsi
call   1040 <warn@plt>
jmp    12d0 <altera_sc_get_status+0x140>
lea    0xcda(%rip),%rdi
lea    0xcef(%rip),%rsi
call   1060 <warnx@plt>
jmp    12d0 <altera_sc_get_status+0x140>

LLVM IR (-O0):
@.str = [19 x i8] c"system-console.pid\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"failed to read %s\00"
@.str.2 = [28 x i8] c"%s doesn't contain a number\00"
@.str.3 = [20 x i8] c"system-console.port\00"
define i32 @altera_sc_get_status(i8** %0, i32* %1) {
%3 = alloca i8**
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [8 x i8]
store i8** %0, i8*** %3
store i32* %1, i32** %4
%12 = load i8**, i8*** %3
%13 = icmp ne i8** %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i8**, i8*** %3
store i8* null, i8** %15
br label %16
16: ; preds = %14, %2
%17 = load i32*, i32** %4
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32*, i32** %4
store i32 0, i32* %20
br label %21
21: ; preds = %19, %16
store i32 -1, i32* %7
store i32 -1, i32* %6
store i32 -1, i32* %5
store i32 -1, i32* %9
%22 = call i32 @open_berictl_dir(i32 1)
store i32 %22, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %21
br label %88
25: ; preds = %21
%26 = load i32, i32* %5
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @openat(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 %28, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %25
br label %88
31: ; preds = %25
%32 = load i32, i32* %6
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 8)
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %88
38: ; preds = %31
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%40 = load i8, i8* %39
%41 = call i32 @isdigit(i8 signext %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = call i32 @warnx(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %88
45: ; preds = %38
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%47 = call i8* @strtol(i8* %46, i32* null, i32 10)
store i8* %47, i8** %10
%48 = load i32, i32* %5
%49 = load i32, i32* @O_RDONLY
%50 = call i32 @openat(i32 %48, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %49)
store i32 %50, i32* %7
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %45
br label %88
53: ; preds = %45
%54 = load i32, i32* %7
%55 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 8)
%57 = icmp slt i32 %56, 1
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %88
60: ; preds = %53
%61 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%62 = load i8, i8* %61
%63 = call i32 @isdigit(i8 signext %62)
%64 = icmp ne i32 %63, 0
br i1 %64, label %67, label %65
65: ; preds = %60
%66 = call i32 @warnx(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %88
67: ; preds = %60
%68 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%69 = call i8* @strtol(i8* %68, i32* null, i32 10)
%70 = ptrtoint i8* %69 to i32
store i32 %70, i32* %8
%71 = load i8*, i8** %10
%72 = call i64 @kill(i8* %71, i32 0)
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %87
74: ; preds = %67
%75 = load i8**, i8*** %3
%76 = icmp ne i8** %75, null
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i8*, i8** %10
%79 = load i8**, i8*** %3
store i8* %78, i8** %79
br label %80
80: ; preds = %77, %74
%81 = load i32*, i32** %4
%82 = icmp ne i32* %81, null
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* %8
%85 = load i32*, i32** %4
store i32 %84, i32* %85
br label %86
86: ; preds = %83, %80
store i32 0, i32* %9
br label %87
87: ; preds = %86, %67
br label %88
88: ; preds = %87, %65, %58, %52, %43, %36, %30, %24
%89 = load i32, i32* %5
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %6
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %7
%94 = call i32 @close(i32 %93)
%95 = load i32, i32* %9
ret i32 %95
}

LLVM IR (-O2):
@.str = [19 x i8] c"system-console.pid\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"failed to read %s\00"
@.str.2 = [28 x i8] c"%s doesn't contain a number\00"
@.str.3 = [20 x i8] c"system-console.port\00"
define i32 @altera_sc_get_status(i8** writeonly %0, i32* writeonly %1) {
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8** %0, null
br i1 %5, label %7, label %6
6: ; preds = %2
store i8* null, i8** %0
br label %7
7: ; preds = %6, %2
%8 = icmp eq i32* %1, null
br i1 %8, label %10, label %9
9: ; preds = %7
store i32 0, i32* %1
br label %10
10: ; preds = %9, %7
%11 = tail call i32 @open_berictl_dir(i32 1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %54, label %13
13: ; preds = %10
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @openat(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %54, label %17
17: ; preds = %13
%18 = call i32 @read(i32 %15, i8* %4, i32 8)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %54
22: ; preds = %17
%23 = load i8, i8* %4
%24 = tail call i32 @isdigit(i8 signext %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @warnx(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %54
28: ; preds = %22
%29 = call i8* @strtol(i8* nocapture %4, i32* null, i32 10)
%30 = load i32, i32* @O_RDONLY
%31 = tail call i32 @openat(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %54, label %33
33: ; preds = %28
%34 = call i32 @read(i32 %31, i8* %4, i32 8)
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %54
38: ; preds = %33
%39 = load i8, i8* %4
%40 = tail call i32 @isdigit(i8 signext %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = tail call i32 @warnx(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %54
44: ; preds = %38
%45 = call i8* @strtol(i8* nocapture %4, i32* null, i32 10)
%46 = ptrtoint i8* %45 to i64
%47 = trunc i64 %46 to i32
%48 = tail call i64 @kill(i8* %29, i32 0)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %44
br i1 %5, label %52, label %51
51: ; preds = %50
store i8* %29, i8** %0
br label %52
52: ; preds = %51, %50
br i1 %8, label %54, label %53
53: ; preds = %52
store i32 %47, i32* %1
br label %54
54: ; preds = %52, %53, %44, %28, %13, %10, %42, %36, %26, %20
%55 = phi i32 [ -1, %10 ], [ -1, %13 ], [ -1, %20 ], [ -1, %28 ], [ %31, %36 ], [ %31, %44 ], [ %31, %42 ], [ -1, %26 ], [ %31, %53 ], [ %31, %52 ]
%56 = phi i32 [ -1, %10 ], [ -1, %13 ], [ %15, %20 ], [ %15, %28 ], [ %15, %36 ], [ %15, %44 ], [ %15, %42 ], [ %15, %26 ], [ %15, %53 ], [ %15, %52 ]
%57 = phi i32 [ -1, %10 ], [ -1, %13 ], [ -1, %20 ], [ -1, %28 ], [ -1, %36 ], [ -1, %44 ], [ -1, %42 ], [ -1, %26 ], [ 0, %53 ], [ 0, %52 ]
%58 = tail call i32 @close(i32 %11)
%59 = tail call i32 @close(i32 %56)
%60 = tail call i32 @close(i32 %55)
ret i32 %57
}