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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_PEERSEC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *,
                      int *);
int sscanf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int smack_get_peer(char *cmd, int sock, char *peer, char *rawpeer) {
  char raw[256 * 2];
  int rawlen = sizeof(raw);
  if (getsockopt(sock, SOL_SOCKET, SO_PEERSEC, raw, &rawlen) < 0) {
    fprintf(stderr, "%s getsockopt SO_PEERSEC %s\n", cmd, strerror(errno));
    return -1;
  }
  if (rawpeer != NULL)
    strcpy(rawpeer, raw);
  if (sscanf(raw, "smack='%[^']'", peer) != 1)
    strcpy(peer, raw);
  return 0;
}


x86 assembly (clang, -O0):
<smack_get_peer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x200,-0x234(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
lea    -0x230(%rbp),%rcx
lea    -0x234(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0x0,%rax
jge    11f6 <smack_get_peer+0xa6>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x244(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x244(%rbp),%edi
mov    -0x240(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <smack_get_peer+0xf8>
cmpq   $0x0,-0x28(%rbp)
je     1211 <smack_get_peer+0xc1>
mov    -0x28(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdfa(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1241 <smack_get_peer+0xf1>
mov    -0x20(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_get_peer>:
push   %r15
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %esi,%eax
mov    %rdi,%r14
movl   $0x200,0xc(%rsp)
mov    0x2e72(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e61(%rip),%rcx
mov    (%rcx),%edx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %eax,%edi
call   1040 <getsockopt@plt>
test   %rax,%rax
js     11d3 <smack_get_peer+0x83>
test   %rbx,%rbx
je     11a9 <smack_get_peer+0x59>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe6d(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15,%rdx
call   1060 <sscanf@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
je     1203 <smack_get_peer+0xb3>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
jmp    1203 <smack_get_peer+0xb3>
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe0f(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_PEERSEC = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"%s getsockopt SO_PEERSEC %s\0A\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"smack='%[^']'\00"
define i32 @smack_get_peer(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [512 x i8]
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 512, i32* %11
%12 = load i32, i32* %7
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_PEERSEC
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%16 = call i64 @getsockopt(i32 %12, i32 %13, i32 %14, i8* %15, i32* %11)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %6
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %22)
store i32 -1, i32* %5
br label %41
24: ; preds = %4
%25 = load i8*, i8** %9
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i8*, i8** %9
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%30 = call i32 @strcpy(i8* %28, i8* %29)
br label %31
31: ; preds = %27, %24
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%33 = load i8*, i8** %8
%34 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = load i8*, i8** %8
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%39 = call i32 @strcpy(i8* %37, i8* %38)
br label %40
40: ; preds = %36, %31
store i32 0, i32* %5
br label %41
41: ; preds = %40, %18
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_PEERSEC = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"%s getsockopt SO_PEERSEC %s\0A\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"smack='%[^']'\00"
define i32 @smack_get_peer(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [512 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = bitcast i32* %6 to i8*
store i32 512, i32* %6
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_PEERSEC
%11 = call i64 @getsockopt(i32 %1, i32 %9, i32 %10, i8* %7, i32* %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %16)
br label %27
18: ; preds = %4
%19 = icmp eq i8* %3, null
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = call i32 @strcpy(i8* %3, i8* %7)
br label %22
22: ; preds = %20, %18
%23 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%24 = icmp eq i32 %23, 1
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @strcpy(i8* %2, i8* %7)
br label %27
27: ; preds = %22, %25, %13
%28 = phi i32 [ -1, %13 ], [ 0, %25 ], [ 0, %22 ]
ret i32 %28
}