#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 socklen_t;

/* Variables and functions */
scalar_t__ ERANGE;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_PEERSEC;
char *calloc(int, int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int *);
int smack_new_label_from_socket(int fd, char **label) {
  char dummy;
  int ret;
  socklen_t length = 1;
  char *result;
  ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
  if (ret < 0 && errno != ERANGE)
    return -1;
  result = calloc(length, 1);
  if (result == NULL)
    return -1;
  ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
  if (ret < 0) {
    free(result);
    return -1;
  }
  *label = result;
  return 0;
}


x86 assembly (clang, -O0):
<smack_new_label_from_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
lea    -0x11(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    119b <smack_new_label_from_socket+0x6b>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     119b <smack_new_label_from_socket+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <smack_new_label_from_socket+0xe9>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c3 <smack_new_label_from_socket+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <smack_new_label_from_socket+0xe9>
mov    -0x8(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1207 <smack_new_label_from_socket+0xd7>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <smack_new_label_from_socket+0xe9>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_new_label_from_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
movl   $0x1,(%rsp)
mov    0x2e99(%rip),%r12
mov    (%r12),%esi
mov    0x2e86(%rip),%r13
mov    0x0(%r13),%edx
lea    0x7(%rsp),%rcx
mov    %rsp,%r8
call   1040 <getsockopt@plt>
test   %eax,%eax
jns    118a <smack_new_label_from_socket+0x5a>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    11cd <smack_new_label_from_socket+0x9d>
mov    (%rsp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11cd <smack_new_label_from_socket+0x9d>
mov    %rax,%rbp
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %rsp,%r8
mov    %r15d,%edi
mov    %rax,%rcx
call   1040 <getsockopt@plt>
test   %eax,%eax
js     11c5 <smack_new_label_from_socket+0x95>
mov    %rbp,(%r14)
xor    %ebx,%ebx
jmp    11cd <smack_new_label_from_socket+0x9d>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_PEERSEC = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
define i32 @smack_new_label_from_socket(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%10 = load i32, i32* %4
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_PEERSEC
%13 = call i32 @getsockopt(i32 %10, i32 %11, i32 %12, i8* %6, i32* %8)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @ERANGE
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %3
br label %41
21: ; preds = %16, %2
%22 = load i32, i32* %8
%23 = call i8* @calloc(i32 %22, i32 1)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %3
br label %41
27: ; preds = %21
%28 = load i32, i32* %4
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_PEERSEC
%31 = load i8*, i8** %9
%32 = call i32 @getsockopt(i32 %28, i32 %29, i32 %30, i8* %31, i32* %8)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = load i8*, i8** %9
%37 = call i32 @free(i8* %36)
store i32 -1, i32* %3
br label %41
38: ; preds = %27
%39 = load i8*, i8** %9
%40 = load i8**, i8*** %5
store i8* %39, i8** %40
store i32 0, i32* %3
br label %41
41: ; preds = %38, %35, %26, %20
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_PEERSEC = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
define i32 @smack_new_label_from_socket(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca i8
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_PEERSEC
%8 = call i32 @getsockopt(i32 %0, i32 %6, i32 %7, i8* %3, i32* %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10, %2
%15 = load i32, i32* %4
%16 = call i8* @calloc(i32 %15, i32 1)
%17 = icmp eq i8* %16, null
br i1 %17, label %26, label %18
18: ; preds = %14
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_PEERSEC
%21 = call i32 @getsockopt(i32 %0, i32 %19, i32 %20, i8* %16, i32* %4)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @free(i8* %16)
br label %26
25: ; preds = %18
store i8* %16, i8** %1
br label %26
26: ; preds = %14, %10, %25, %23
%27 = phi i32 [ -1, %23 ], [ 0, %25 ], [ -1, %10 ], [ -1, %14 ]
ret i32 %27
}