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

/* Variables and functions */
#define AF_INET 133
#define AF_INET6 132
#define IPPROTO_TCP 131
#define IPPROTO_UDP 130
#define SOCK_DGRAM 129
#define SOCK_STREAM 128
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ setup_socket(int);
int socket(int, int, int);
int manos_socket_create(int addressFamily, int protocolFamily, int *err) {
  static int domains[] = {AF_INET, AF_INET6};
  static int types[] = {SOCK_STREAM, SOCK_DGRAM};
  static int protocols[] = {IPPROTO_TCP, IPPROTO_UDP};
  int result = socket(domains[addressFamily], types[protocolFamily],
                      protocols[protocolFamily]);
  if (result > 0 && setup_socket(result) < 0) {
    *err = errno;
    close(result);
    return -1;
  }
  *err = errno;
  return result;
}


x86 assembly (clang, -O0):
<manos_socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0x8(%rbp),%rcx
lea    0x2eeb(%rip),%rax
mov    (%rax,%rcx,4),%edi
movslq -0xc(%rbp),%rcx
lea    0x2ee5(%rip),%rax
mov    (%rax,%rcx,4),%esi
movslq -0xc(%rbp),%rcx
lea    0x2edf(%rip),%rax
mov    (%rax,%rcx,4),%edx
call   1050 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11b3 <manos_socket_create+0x83>
mov    -0x1c(%rbp),%edi
call   1040 <setup_socket@plt>
cmp    $0x0,%rax
jge    11b3 <manos_socket_create+0x83>
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <manos_socket_create+0x98>
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manos_socket_create>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
movslq %edi,%rax
lea    0xec0(%rip),%rcx
mov    (%rcx,%rax,4),%edi
movslq %esi,%rax
lea    0xebb(%rip),%rcx
mov    (%rcx,%rax,4),%esi
lea    0xeb9(%rip),%rcx
mov    (%rcx,%rax,4),%edx
call   1050 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1171 <manos_socket_create+0x41>
mov    %ebp,%edi
call   1040 <setup_socket@plt>
test   %rax,%rax
js     117e <manos_socket_create+0x4e>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
jmp    1195 <manos_socket_create+0x65>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@manos_socket_create.domains = internal global [2 x i32] [i32 133, i32 132]
@manos_socket_create.types = internal global [2 x i32] [i32 128, i32 129]
@manos_socket_create.protocols = internal global [2 x i32] [i32 131, i32 130]
@errno = global i32 0
define i32 @manos_socket_create(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [2 x i32], [2 x i32]* @manos_socket_create.domains, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [2 x i32], [2 x i32]* @manos_socket_create.types, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [2 x i32], [2 x i32]* @manos_socket_create.protocols, i64 0, i64 %18
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %12, i32 %16, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %3
%25 = load i32, i32* %8
%26 = call i64 @setup_socket(i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = load i32*, i32** %7
store i32 %29, i32* %30
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %4
br label %37
33: ; preds = %24, %3
%34 = load i32, i32* @errno
%35 = load i32*, i32** %7
store i32 %34, i32* %35
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %33, %28
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@manos_socket_create.domains = internal unnamed_addr constant [2 x i32] [i32 133, i32 132]
@manos_socket_create.types = internal unnamed_addr constant [2 x i32] [i32 128, i32 129]
@manos_socket_create.protocols = internal unnamed_addr constant [2 x i32] [i32 131, i32 130]
@errno = global i32 0
define i32 @manos_socket_create(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = sext i32 %0 to i64
%5 = getelementptr inbounds [2 x i32], [2 x i32]* @manos_socket_create.domains, i64 0, i64 %4
%6 = load i32, i32* %5
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds [2 x i32], [2 x i32]* @manos_socket_create.types, i64 0, i64 %7
%9 = load i32, i32* %8
%10 = getelementptr inbounds [2 x i32], [2 x i32]* @manos_socket_create.protocols, i64 0, i64 %7
%11 = load i32, i32* %10
%12 = tail call i32 @socket(i32 %6, i32 %9, i32 %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = tail call i64 @setup_socket(i32 %12)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @errno
store i32 %18, i32* %2
%19 = tail call i32 @close(i32 %12)
br label %22
20: ; preds = %14, %3
%21 = load i32, i32* @errno
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %17
%23 = phi i32 [ -1, %17 ], [ %12, %20 ]
ret i32 %23
}