#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 */
struct sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};

/* Variables and functions */
int socket(int /*<<< orphan*/, int, int);
int open_socket(struct sockaddr_storage saddr_both[2], size_t size_both[2],
                int type, int proto, struct sockaddr_storage **saddr_used,
                size_t *size_used) {
  size_t i;
  int fd;
  for (i = 0; (i != 2) && (size_both[i]); ++i) {
    const struct sockaddr *const sa = (const struct sockaddr *)&saddr_both[i];
    fd = socket(sa->sa_family, type, proto);
    if (fd != -1)
      break;
  }
  if ((i == 2) || (size_both[i] == 0))
    return -1;
  *saddr_used = &saddr_both[i];
  *size_used = size_both[i];
  return fd;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x2,-0x38(%rbp)
mov    %al,-0x49(%rbp)
je     1159 <open_socket+0x49>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1169 <open_socket+0x59>
jmp    11b5 <open_socket+0xa5>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1030 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
je     119f <open_socket+0x8f>
jmp    11b5 <open_socket+0xa5>
jmp    11a4 <open_socket+0x94>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1136 <open_socket+0x26>
cmpq   $0x2,-0x38(%rbp)
je     11d3 <open_socket+0xc3>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11df <open_socket+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <open_socket+0xfe>
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
mov    $0xffffffff,%r13d
cmpq   $0x0,(%rsi)
je     1160 <open_socket+0x50>
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %r8,0x8(%rsp)
mov    %r9,0x10(%rsp)
mov    (%r12),%edi
mov    %edx,%esi
mov    %ecx,%edx
call   1030 <socket@plt>
cmp    $0xffffffff,%eax
je     116a <open_socket+0x5a>
xor    %r15d,%r15d
mov    0x10(%rsp),%r9
mov    0x8(%rsp),%r8
jmp    1195 <open_socket+0x85>
xor    %r15d,%r15d
mov    $0xffffffff,%eax
jmp    1195 <open_socket+0x85>
mov    $0x1,%r15d
cmpq   $0x0,0x8(%rbx)
je     11bd <open_socket+0xad>
mov    0x4(%r12),%edi
mov    %r14d,%esi
mov    %ebp,%edx
call   1030 <socket@plt>
cmp    $0xffffffff,%eax
mov    0x10(%rsp),%r9
mov    0x8(%rsp),%r8
je     11c4 <open_socket+0xb4>
mov    (%rbx,%r15,8),%rcx
test   %rcx,%rcx
je     11ab <open_socket+0x9b>
lea    (%r12,%r15,4),%rdx
mov    %rdx,(%r8)
mov    %rcx,(%r9)
mov    %eax,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1154 <open_socket+0x44>
mov    $0xffffffff,%r13d
jmp    11ab <open_socket+0x9b>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @open_socket(%struct.sockaddr_storage* %0, i64* %1, i32 %2, i32 %3, %struct.sockaddr_storage** %4, i64* %5) {
%7 = alloca i32
%8 = alloca %struct.sockaddr_storage*
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_storage**
%13 = alloca i64*
%14 = alloca i64
%15 = alloca i32
%16 = alloca %struct.sockaddr*
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %8
store i64* %1, i64** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store %struct.sockaddr_storage** %4, %struct.sockaddr_storage*** %12
store i64* %5, i64** %13
store i64 0, i64* %14
br label %17
17: ; preds = %43, %6
%18 = load i64, i64* %14
%19 = icmp ne i64 %18, 2
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i64*, i64** %9
%22 = load i64, i64* %14
%23 = getelementptr inbounds i64, i64* %21, i64 %22
%24 = load i64, i64* %23
%25 = icmp ne i64 %24, 0
br label %26
26: ; preds = %20, %17
%27 = phi i1 [ false, %17 ], [ %25, %20 ]
br i1 %27, label %28, label %46
28: ; preds = %26
%29 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %8
%30 = load i64, i64* %14
%31 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %29, i64 %30
%32 = bitcast %struct.sockaddr_storage* %31 to %struct.sockaddr*
store %struct.sockaddr* %32, %struct.sockaddr** %16
%33 = load %struct.sockaddr*, %struct.sockaddr** %16
%34 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* %10
%37 = load i32, i32* %11
%38 = call i32 @socket(i32 %35, i32 %36, i32 %37)
store i32 %38, i32* %15
%39 = load i32, i32* %15
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %28
br label %46
42: ; preds = %28
br label %43
43: ; preds = %42
%44 = load i64, i64* %14
%45 = add i64 %44, 1
store i64 %45, i64* %14
br label %17
46: ; preds = %41, %26
%47 = load i64, i64* %14
%48 = icmp eq i64 %47, 2
br i1 %48, label %55, label %49
49: ; preds = %46
%50 = load i64*, i64** %9
%51 = load i64, i64* %14
%52 = getelementptr inbounds i64, i64* %50, i64 %51
%53 = load i64, i64* %52
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %49, %46
store i32 -1, i32* %7
br label %67
56: ; preds = %49
%57 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %8
%58 = load i64, i64* %14
%59 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %57, i64 %58
%60 = load %struct.sockaddr_storage**, %struct.sockaddr_storage*** %12
store %struct.sockaddr_storage* %59, %struct.sockaddr_storage** %60
%61 = load i64*, i64** %9
%62 = load i64, i64* %14
%63 = getelementptr inbounds i64, i64* %61, i64 %62
%64 = load i64, i64* %63
%65 = load i64*, i64** %13
store i64 %64, i64* %65
%66 = load i32, i32* %15
store i32 %66, i32* %7
br label %67
67: ; preds = %56, %55
%68 = load i32, i32* %7
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
define i32 @open_socket(%struct.sockaddr_storage* %0, i64* nocapture %1, i32 %2, i32 %3, %struct.sockaddr_storage** nocapture writeonly %4, i64* nocapture writeonly %5) {
%7 = load i64, i64* %1
%8 = icmp eq i64 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @socket(i32 %11, i32 %2, i32 %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %9
%15 = getelementptr inbounds i64, i64* %1, i64 1
%16 = load i64, i64* %15
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 1, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 @socket(i32 %20, i32 %2, i32 %3)
%22 = icmp eq i32 %21, -1
br i1 %22, label %31, label %23
23: ; preds = %18, %14, %9, %6
%24 = phi i64 [ 0, %9 ], [ 0, %6 ], [ 1, %14 ], [ 1, %18 ]
%25 = phi i32 [ %12, %9 ], [ -1, %6 ], [ -1, %14 ], [ %21, %18 ]
%26 = getelementptr inbounds i64, i64* %1, i64 %24
%27 = load i64, i64* %26
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 %24
store %struct.sockaddr_storage* %30, %struct.sockaddr_storage** %4
store i64 %27, i64* %5
br label %31
31: ; preds = %18, %23, %29
%32 = phi i32 [ %25, %29 ], [ -1, %23 ], [ -1, %18 ]
ret i32 %32
}