#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 EINVAL;
char *getenv(char *);
int getpid();
int /*<<< orphan*/ log_error(char *);
int strtoul(char const *, int /*<<< orphan*/ *, int);
int mgmt_ipc_systemd(void) {
  const char *env;
  env = getenv("LISTEN_PID");
  if (!env || (strtoul(env, NULL, 10) != getpid()))
    return -EINVAL;
  env = getenv("LISTEN_FDS");
  if (!env)
    return -EINVAL;
  if (strtoul(env, NULL, 10) != 1) {
    log_error("Did not receive exactly one IPC socket from systemd");
    return -EINVAL;
  }
  return 3;
}


x86 assembly (clang, -O0):
<mgmt_ipc_systemd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118c <mgmt_ipc_systemd+0x4c>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
je     119f <mgmt_ipc_systemd+0x5f>
xor    %eax,%eax
mov    0x2e4b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <mgmt_ipc_systemd+0xce>
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cd <mgmt_ipc_systemd+0x8d>
xor    %eax,%eax
mov    0x2e1d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <mgmt_ipc_systemd+0xce>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoul@plt>
cmp    $0x1,%eax
je     1207 <mgmt_ipc_systemd+0xc7>
lea    0xe27(%rip),%rdi
call   1050 <log_error@plt>
xor    %eax,%eax
mov    0x2de3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <mgmt_ipc_systemd+0xce>
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgmt_ipc_systemd>:
push   %rbx
lea    0xeb8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b5 <mgmt_ipc_systemd+0x75>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %eax,%ebx
jne    11b5 <mgmt_ipc_systemd+0x75>
lea    0xe96(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b5 <mgmt_ipc_systemd+0x75>
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    $0x1,%ecx
je     11b3 <mgmt_ipc_systemd+0x73>
lea    0xe73(%rip),%rdi
call   1050 <log_error@plt>
mov    0x2e31(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2e24(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"LISTEN_PID\00"
@EINVAL = global i32 0
@.str.1 = [11 x i8] c"LISTEN_FDS\00"
@.str.2 = [52 x i8] c"Did not receive exactly one IPC socket from systemd\00"
define i32 @mgmt_ipc_systemd() {
%1 = alloca i32
%2 = alloca i8*
%3 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i8*, i8** %2
%8 = call i32 @strtoul(i8* %7, i32* null, i32 10)
%9 = call i32 (...) @getpid()
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %6, %0
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
store i32 %13, i32* %1
br label %30
14: ; preds = %6
%15 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
store i32 %20, i32* %1
br label %30
21: ; preds = %14
%22 = load i8*, i8** %2
%23 = call i32 @strtoul(i8* %22, i32* null, i32 10)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EINVAL
%28 = sub nsw i32 0, %27
store i32 %28, i32* %1
br label %30
29: ; preds = %21
store i32 3, i32* %1
br label %30
30: ; preds = %29, %25, %18, %11
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [11 x i8] c"LISTEN_PID\00"
@EINVAL = global i32 0
@.str.1 = [11 x i8] c"LISTEN_FDS\00"
@.str.2 = [52 x i8] c"Did not receive exactly one IPC socket from systemd\00"
define i32 @mgmt_ipc_systemd() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %7, label %3
3: ; preds = %0
%4 = tail call i32 @strtoul(i8* nocapture %1, i32* null, i32 10)
%5 = tail call i32 (...) @getpid()
%6 = icmp eq i32 %4, %5
br i1 %6, label %10, label %7
7: ; preds = %3, %0
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %23
10: ; preds = %3
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %23
16: ; preds = %10
%17 = tail call i32 @strtoul(i8* nocapture %11, i32* null, i32 10)
%18 = icmp eq i32 %17, 1
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = tail call i32 @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EINVAL
%22 = sub nsw i32 0, %21
br label %23
23: ; preds = %16, %19, %13, %7
%24 = phi i32 [ %9, %7 ], [ %22, %19 ], [ %15, %13 ], [ 3, %16 ]
ret i32 %24
}