#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 scalar_t__ pid_t;

/* Variables and functions */
unsigned long ULONG_MAX;
char *getenv(char *);
scalar_t__ getpid();
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int);
int sd_get_socket_count(void) {
  unsigned long n;
  const char *env;
  env = getenv("LISTEN_PID");
  if (!env)
    return 0;
  n = strtoul(env, NULL, 10);
  if (n == ULONG_MAX || (pid_t)n != getpid())
    return 0;
  env = getenv("LISTEN_FDS");
  if (!env)
    return 0;
  n = strtoul(env, NULL, 10);
  if (n == ULONG_MAX)
    return 0;
  return (int)n;
}


x86 assembly (clang, -O0):
<sd_get_socket_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115f <sd_get_socket_count+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    1218 <sd_get_socket_count+0xe8>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <sd_get_socket_count+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     11b4 <sd_get_socket_count+0x84>
movl   $0x0,-0x4(%rbp)
jmp    1218 <sd_get_socket_count+0xe8>
lea    0xe50(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11db <sd_get_socket_count+0xab>
movl   $0x0,-0x4(%rbp)
jmp    1218 <sd_get_socket_count+0xe8>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <sd_get_socket_count+0xe1>
movl   $0x0,-0x4(%rbp)
jmp    1218 <sd_get_socket_count+0xe8>
mov    -0x10(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_get_socket_count>:
push   %r15
push   %r14
push   %rbx
lea    0xec4(%rip),%rdi
call   1030 <getenv@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11a2 <sd_get_socket_count+0x72>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    0x2e79(%rip),%r15
cmp    (%r15),%rax
je     11a2 <sd_get_socket_count+0x72>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %rax,%rbx
jne    11a2 <sd_get_socket_count+0x72>
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a2 <sd_get_socket_count+0x72>
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,%r14
cmp    (%r15),%rax
cmove  %ebx,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"LISTEN_PID\00"
@ULONG_MAX = global i64 0
@.str.1 = [11 x i8] c"LISTEN_FDS\00"
define i32 @sd_get_socket_count() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i32 0, i32* %1
br label %34
8: ; preds = %0
%9 = load i8*, i8** %3
%10 = call i64 @strtoul(i8* %9, i32* null, i32 10)
store i64 %10, i64* %2
%11 = load i64, i64* %2
%12 = load i64, i64* @ULONG_MAX
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = load i64, i64* %2
%16 = call i64 (...) @getpid()
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14, %8
store i32 0, i32* %1
br label %34
19: ; preds = %14
%20 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %19
store i32 0, i32* %1
br label %34
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = call i64 @strtoul(i8* %25, i32* null, i32 10)
store i64 %26, i64* %2
%27 = load i64, i64* %2
%28 = load i64, i64* @ULONG_MAX
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 0, i32* %1
br label %34
31: ; preds = %24
%32 = load i64, i64* %2
%33 = trunc i64 %32 to i32
store i32 %33, i32* %1
br label %34
34: ; preds = %31, %30, %23, %18, %7
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [11 x i8] c"LISTEN_PID\00"
@ULONG_MAX = global i64 0
@.str.1 = [11 x i8] c"LISTEN_FDS\00"
define i32 @sd_get_socket_count() {
%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 %19, label %3
3: ; preds = %0
%4 = tail call i64 @strtoul(i8* nocapture %1, i32* null, i32 10)
%5 = load i64, i64* @ULONG_MAX
%6 = icmp eq i64 %4, %5
br i1 %6, label %19, label %7
7: ; preds = %3
%8 = tail call i64 (...) @getpid()
%9 = icmp eq i64 %4, %8
br i1 %9, label %10, label %19
10: ; preds = %7
%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 %19, label %13
13: ; preds = %10
%14 = tail call i64 @strtoul(i8* nocapture %11, i32* null, i32 10)
%15 = load i64, i64* @ULONG_MAX
%16 = icmp eq i64 %14, %15
%17 = trunc i64 %14 to i32
%18 = select i1 %16, i32 0, i32 %17
br label %19
19: ; preds = %13, %10, %3, %7, %0
%20 = phi i32 [ 0, %0 ], [ 0, %7 ], [ 0, %3 ], [ 0, %10 ], [ %18, %13 ]
ret i32 %20
}