#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_open(char *laddr, int port) {
  struct sockaddr_in lsin;
  int sock;
  int reuse = 1;
  memset(&lsin, 0, sizeof(struct sockaddr_in));
  sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0)
    return sock;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse,
                 sizeof(reuse)) < 0) {
    close(sock);
    return -1;
  }
  lsin.sin_family = AF_INET;
  lsin.sin_addr.s_addr = INADDR_ANY;
  if (laddr)
    inet_aton(laddr, &lsin.sin_addr);
  lsin.sin_port = htons(port);
  if (bind(sock, (struct sockaddr *)&lsin, sizeof(struct sockaddr_in)) != 0) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<socket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ce <socket_open+0x5e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1284 <socket_open+0x114>
mov    -0x24(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1210 <socket_open+0xa0>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <socket_open+0x114>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1244 <socket_open+0xd4>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
call   1070 <inet_aton@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     127e <socket_open+0x10e>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <socket_open+0x114>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
movl   $0x1,0xc(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     123f <socket_open+0xcf>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1233 <socket_open+0xc3>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
test   %rbp,%rbp
je     1214 <socket_open+0xa4>
lea    0x4(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <inet_aton@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
je     123f <socket_open+0xcf>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @socket_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %8
%9 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %53
18: ; preds = %2
%19 = load i32, i32* %7
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = bitcast i32* %8 to i8*
%23 = call i64 @setsockopt(i32 %19, i32 %20, i32 %21, i8* %22, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %53
28: ; preds = %18
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i8*, i8** %4
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = load i8*, i8** %4
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%39 = call i32 @inet_aton(i8* %37, %struct.TYPE_2__* %38)
br label %40
40: ; preds = %36, %28
%41 = load i32, i32* %5
%42 = call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %7
%45 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%46 = call i64 @bind(i32 %44, %struct.sockaddr* %45, i32 12)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %3
br label %53
51: ; preds = %40
%52 = load i32, i32* %7
store i32 %52, i32* %3
br label %53
53: ; preds = %51, %48, %25, %16
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @socket_open(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %35, label %13
13: ; preds = %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i64 @setsockopt(i32 %11, i32 %14, i32 %15, i8* %6, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %33, label %18
18: ; preds = %13
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i64 0, i32 0
store i32 %21, i32* %23
%24 = icmp eq i8* %0, null
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = call i32 @inet_aton(i8* %0, %struct.TYPE_2__* %22)
br label %27
27: ; preds = %25, %18
%28 = call i32 @htons(i32 %1)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @bind(i32 %11, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %27, %13
%34 = call i32 @close(i32 %11)
br label %35
35: ; preds = %33, %27, %2
%36 = phi i32 [ %11, %2 ], [ %11, %27 ], [ -1, %33 ]
ret i32 %36
}