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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_PEERSEC;
char *calloc(size_t, int);
int /*<<< orphan*/ free(char *);
int get_label(char *, char *, int /*<<< orphan*/ *);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, size_t *);
ssize_t smack_new_label_from_socket(int fd, char **label) {
  char buf[255 + 2];
  int ret;
  socklen_t length = 255 + 1;
  char *result;
  ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &length);
  if (ret < 0)
    return -1;
  buf[length] = '\0';
  result = calloc(length + 1, 1);
  if (result == NULL)
    return -1;
  ret = get_label(result, buf, NULL);
  if (ret < 0) {
    free(result);
    return -1;
  }
  *label = result;
  return ret;
}


x86 assembly (clang, -O0):
<smack_new_label_from_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x100,-0x130(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
lea    -0x120(%rbp),%rcx
lea    -0x130(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11a4 <smack_new_label_from_socket+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <smack_new_label_from_socket+0x102>
mov    -0x130(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x130(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    11e9 <smack_new_label_from_socket+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <smack_new_label_from_socket+0x102>
mov    -0x138(%rbp),%rdi
lea    -0x120(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <get_label@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    122b <smack_new_label_from_socket+0xeb>
mov    -0x138(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <smack_new_label_from_socket+0x102>
mov    -0x138(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_new_label_from_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    %rsi,%r14
movq   $0x100,0x8(%rsp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
call   1040 <getsockopt@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11c3 <smack_new_label_from_socket+0x83>
mov    0x8(%rsp),%rdi
movb   $0x0,0x10(%rsp,%rdi,1)
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11c3 <smack_new_label_from_socket+0x83>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <get_label@plt>
test   %eax,%eax
js     11bb <smack_new_label_from_socket+0x7b>
mov    %rbx,(%r14)
mov    %eax,%ebp
jmp    11c3 <smack_new_label_from_socket+0x83>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_PEERSEC = global i32 0
define i32 @smack_new_label_from_socket(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [257 x i8]
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 256, i64* %8
%10 = load i32, i32* %4
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_PEERSEC
%13 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%14 = call i32 @getsockopt(i32 %10, i32 %11, i32 %12, i8* %13, i64* %8)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %40
18: ; preds = %2
%19 = load i64, i64* %8
%20 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 %19
store i8 0, i8* %20
%21 = load i64, i64* %8
%22 = add i64 %21, 1
%23 = call i8* @calloc(i64 %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 = %18
store i32 -1, i32* %3
br label %40
27: ; preds = %18
%28 = load i8*, i8** %9
%29 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%30 = call i32 @get_label(i8* %28, i8* %29, i32* null)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i8*, i8** %9
%35 = call i32 @free(i8* %34)
store i32 -1, i32* %3
br label %40
36: ; preds = %27
%37 = load i8*, i8** %9
%38 = load i8**, i8*** %5
store i8* %37, i8** %38
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %40
40: ; preds = %36, %33, %26, %17
%41 = load i32, i32* %3
ret i32 %41
}

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