#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*/ type;
typedef int /*<<< orphan*/ socktypes;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_TYPE;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int __sapi_get_sock_type(int sock, char *socket_type) {
  char *socktypes[] = {
      "tcp",
      "udp",
      "unknown",
  };
  int type = -1;
  int typelen = sizeof(type);
  int ret;
  ret = getsockopt(sock, SOL_SOCKET, SO_TYPE, &type, &typelen);
  if (ret < 0) {
    return -1;
  }
  if (type > sizeof(socktypes) / sizeof(socktypes[0])) {
    return -1;
  }
  socket_type = socktypes[type - 1];
  return 0;
}


x86 assembly (clang, -O0):
<__sapi_get_sock_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2cca(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2cc7(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2cc4(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x4,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1189 <__sapi_get_sock_type+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <__sapi_get_sock_type+0xab>
movslq -0x34(%rbp),%rax
cmp    $0x3,%rax
jbe    11a3 <__sapi_get_sock_type+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <__sapi_get_sock_type+0xab>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x30(%rbp,%rax,8),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sapi_get_sock_type>:
push   %rax
movl   $0xffffffff,(%rsp)
movl   $0x4,0x4(%rsp)
mov    0x2ec1(%rip),%rax
mov    (%rax),%esi
mov    0x2eb0(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
lea    0x4(%rsp),%r8
call   1030 <getsockopt@plt>
cmpl   $0x4,(%rsp)
setae  %cl
test   %eax,%eax
sets   %al
or     %cl,%al
movzbl %al,%eax
neg    %eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@.str.2 = [8 x i8] c"unknown\00"
@__const.__sapi_get_sock_type.socktypes = [3 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0)]
@SOL_SOCKET = global i32 0
@SO_TYPE = global i32 0
define i32 @__sapi_get_sock_type(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [3 x i8*]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = bitcast [3 x i8*]* %6 to i8*
store i32 -1, i32* %7
store i32 4, i32* %8
%11 = load i32, i32* %4
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_TYPE
%14 = call i32 @getsockopt(i32 %11, i32 %12, i32 %13, i32* %7, i32* %8)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %29
18: ; preds = %2
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = icmp ugt i64 %20, 3
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %29
23: ; preds = %18
%24 = load i32, i32* %7
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %26
%28 = load i8*, i8** %27
store i8* %28, i8** %5
store i32 0, i32* %3
br label %29
29: ; preds = %23, %22, %17
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_TYPE = global i32 0
define i32 @__sapi_get_sock_type(i32 %0, i8* nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%6 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%7 = load i32, i32* @SOL_SOCKET
%8 = load i32, i32* @SO_TYPE
%9 = call i32 @getsockopt(i32 %0, i32 %7, i32 %8, i32* %3, i32* %4)
%10 = load i32, i32* %3
%11 = icmp ugt i32 %10, 3
%12 = icmp slt i32 %9, 0
%13 = select i1 %12, i1 true, i1 %11
%14 = sext i1 %13 to i32
ret i32 %14
}