#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 ssize_t;
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 *);
ssize_t 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, 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 length;
}


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    121b <smack_new_label_from_socket+0xeb>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <smack_new_label_from_socket+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <smack_new_label_from_socket+0xeb>
mov    -0x8(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%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    120a <smack_new_label_from_socket+0xda>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <smack_new_label_from_socket+0xeb>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-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    11d1 <smack_new_label_from_socket+0xa1>
mov    (%rsp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11d1 <smack_new_label_from_socket+0xa1>
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     11c9 <smack_new_label_from_socket+0x99>
mov    %rbp,(%r14)
mov    (%rsp),%ebx
jmp    11d1 <smack_new_label_from_socket+0xa1>
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 %43
21: ; preds = %16, %2
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
%24 = call i8* @calloc(i32 %23, i32 1)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %3
br label %43
28: ; preds = %21
%29 = load i32, i32* %4
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_PEERSEC
%32 = load i8*, i8** %9
%33 = call i32 @getsockopt(i32 %29, i32 %30, i32 %31, i8* %32, i32* %8)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = load i8*, i8** %9
%38 = call i32 @free(i8* %37)
store i32 -1, i32* %3
br label %43
39: ; preds = %28
%40 = load i8*, i8** %9
%41 = load i8**, i8*** %5
store i8* %40, i8** %41
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %43
43: ; preds = %39, %36, %27, %20
%44 = load i32, i32* %3
ret i32 %44
}

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 %28
14: ; preds = %10, %2
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
%17 = call i8* @calloc(i32 %16, i32 1)
%18 = icmp eq i8* %17, null
br i1 %18, label %28, label %19
19: ; preds = %14
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_PEERSEC
%22 = call i32 @getsockopt(i32 %0, i32 %20, i32 %21, i8* %17, i32* %4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @free(i8* %17)
br label %28
26: ; preds = %19
store i8* %17, i8** %1
%27 = load i32, i32* %4
br label %28
28: ; preds = %14, %10, %26, %24
%29 = phi i32 [ -1, %24 ], [ %27, %26 ], [ -1, %10 ], [ -1, %14 ]
ret i32 %29
}