#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 */
typedef int /*<<< orphan*/ taddr;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(int);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_open(int port, char *addr, struct sockaddr_in *taddr2) {
  int rt;
  int fd;
  struct sockaddr_in taddr;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    return -1;
  }
  if (addr) {
    taddr2->sin_family = AF_INET;
    taddr2->sin_port = htons(port);
    taddr2->sin_addr.s_addr = inet_addr(addr);
    port = 0;
  }
  taddr.sin_family = AF_INET;
  taddr.sin_port = htons(port);
  taddr.sin_addr.s_addr = htonl(INADDR_ANY);
  rt = bind(fd, (struct sockaddr *)&taddr, sizeof(taddr));
  if (rt) {
    close(fd);
    return -2;
  }
  return fd;
}


x86 assembly (clang, -O0):
<udp_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11a5 <udp_open+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <udp_open+0xf0>
cmpq   $0x0,-0x10(%rbp)
je     11ed <udp_open+0x8d>
mov    0x2e31(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x8(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     124a <udp_open+0xea>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1250 <udp_open+0xf0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     120a <udp_open+0xaa>
mov    %eax,%ebx
test   %r15,%r15
je     11bd <udp_open+0x5d>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%r14)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%r14)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
test   %eax,%eax
je     120f <udp_open+0xaf>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffe,%ebx
jmp    120f <udp_open+0xaf>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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