#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ natpmp_addr;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int OpenAndConfNATPMPSocket(in_addr_t addr) {
  int snatpmp;
  snatpmp = socket(PF_INET, SOCK_DGRAM, 0);
  if (snatpmp < 0) {
    syslog(LOG_ERR, "socket(natpmp): %m");
    return -1;
  } else {
    struct sockaddr_in natpmp_addr;
    memset(&natpmp_addr, 0, sizeof(natpmp_addr));
    natpmp_addr.sin_family = AF_INET;
    natpmp_addr.sin_port = htons((5351));
    natpmp_addr.sin_addr.s_addr = addr;
    if (bind(snatpmp, (struct sockaddr *)&natpmp_addr, sizeof(natpmp_addr)) <
        0) {
      syslog(LOG_ERR, "bind(natpmp): %m");
      close(snatpmp);
      return -1;
    }
  }
  return snatpmp;
}


x86 assembly (clang, -O0):
<OpenAndConfNATPMPSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b2 <OpenAndConfNATPMPSocket+0x52>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1060 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <OpenAndConfNATPMPSocket+0xd0>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x14e7,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1225 <OpenAndConfNATPMPSocket+0xc5>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1060 <syslog@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <OpenAndConfNATPMPSocket+0xd0>
jmp    122a <OpenAndConfNATPMPSocket+0xca>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenAndConfNATPMPSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11eb <OpenAndConfNATPMPSocket+0x8b>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x14e7,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1205 <OpenAndConfNATPMPSocket+0xa5>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1060 <syslog@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1200 <OpenAndConfNATPMPSocket+0xa0>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@LOG_ERR = global i32 0
@.str = [19 x i8] c"socket(natpmp): %m\00"
@AF_INET = global i32 0
@.str.1 = [17 x i8] c"bind(natpmp): %m\00"
define i32 @OpenAndConfNATPMPSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @LOG_ERR
%13 = call i32 @syslog(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 5351)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %3
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %14
%28 = load i32, i32* @LOG_ERR
%29 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %2
br label %35
32: ; preds = %14
br label %33
33: ; preds = %32
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %27, %11
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@LOG_ERR = global i32 0
@.str = [19 x i8] c"socket(natpmp): %m\00"
@AF_INET = global i32 0
@.str.1 = [17 x i8] c"bind(natpmp): %m\00"
define i32 @OpenAndConfNATPMPSocket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @LOG_ERR
%9 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %27
10: ; preds = %1
%11 = bitcast %struct.sockaddr_in* %2 to i8*
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 5351)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %0, i32* %17
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %5, %struct.sockaddr* %18, i32 12)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %10
%22 = load i32, i32* @LOG_ERR
%23 = call i32 @syslog(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %10, %21
%26 = phi i32 [ %5, %10 ], [ -1, %21 ]
br label %27
27: ; preds = %25, %7
%28 = phi i32 [ -1, %7 ], [ %26, %25 ]
ret i32 %28
}