#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 /*<<< orphan*/ key;

/* Variables and functions */
scalar_t__ errno;
char *getenv(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int strtol(char const *, int /*<<< orphan*/ *, int);
int android_get_control_socket(const char *name) {
  char key[64] = "ANDROID_SOCKET_";
  const char *val;
  int fd;
  strncpy(key + sizeof("ANDROID_SOCKET_") - 1, name,
          sizeof(key) - sizeof("ANDROID_SOCKET_"));
  key[sizeof(key) - 1] = '\0';
  val = getenv(key);
  if (!val)
    return -1;
  errno = 0;
  fd = strtol(val, NULL, 10);
  if (errno)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<android_get_control_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xea9(%rip),%rsi
mov    $0x40,%edx
call   1060 <memcpy@plt>
lea    -0x50(%rbp),%rdi
add    $0x10,%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x10(%rbp),%rsi
mov    $0x30,%edx
call   1040 <strncpy@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x50(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11a3 <android_get_control_socket+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <android_get_control_socket+0xa9>
mov    0x2e36(%rip),%rax
movq   $0x0,(%rax)
mov    -0x58(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x5c(%rbp)
mov    0x2e13(%rip),%rax
cmpq   $0x0,(%rax)
je     11e3 <android_get_control_socket+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <android_get_control_socket+0xa9>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<android_get_control_socket>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rsi
movaps 0xeef(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xed3(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xeb7(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe9b(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xf(%rsp),%rdi
mov    $0x30,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x3f(%rsp)
mov    %rsp,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b5 <android_get_control_socket+0x85>
mov    0x2e4f(%rip),%rbx
movq   $0x0,(%rbx)
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    (%rbx),%r14
mov    %eax,%ecx
sbb    %eax,%eax
or     %ecx,%eax
jmp    11ba <android_get_control_socket+0x8a>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.android_get_control_socket.key = [64 x i8] c"ANDROID_SOCKET_\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@errno = global i64 0
define i32 @android_get_control_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [64 x i8]
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = bitcast [64 x i8]* %4 to i8*
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %8, i64 16
%10 = getelementptr inbounds i8, i8* %9, i64 -1
%11 = load i8*, i8** %3
%12 = call i32 @strncpy(i8* %10, i8* %11, i32 48)
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 63
store i8 0, i8* %13
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = call i8* @getenv(i8* %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %1
store i32 -1, i32* %2
br label %27
19: ; preds = %1
store i64 0, i64* @errno
%20 = load i8*, i8** %5
%21 = call i32 @strtol(i8* %20, i32* null, i32 10)
store i32 %21, i32* %6
%22 = load i64, i64* @errno
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %2
br label %27
25: ; preds = %19
%26 = load i32, i32* %6
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %24, %18
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@__const.android_get_control_socket.key = [64 x i8] c"ANDROID_SOCKET_\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@errno = global i64 0
define i32 @android_get_control_socket(i8* %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 15
%5 = call i32 @strncpy(i8* %4, i8* %0, i32 48)
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 63
store i8 0, i8* %6
%7 = call i8* @getenv(i8* %3)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %1
store i64 0, i64* @errno
%10 = call i32 @strtol(i8* nocapture %7, i32* null, i32 10)
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i32 %10, i32 -1
br label %14
14: ; preds = %9, %1
%15 = phi i32 [ -1, %1 ], [ %13, %9 ]
ret i32 %15
}