#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;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
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() {
  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 = INADDR_ANY;
    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    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11af <OpenAndConfNATPMPSocket+0x4f>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1060 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <OpenAndConfNATPMPSocket+0xd3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x14e7,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1228 <OpenAndConfNATPMPSocket+0xc8>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1060 <syslog@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <OpenAndConfNATPMPSocket+0xd3>
jmp    122d <OpenAndConfNATPMPSocket+0xcd>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenAndConfNATPMPSocket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f4 <OpenAndConfNATPMPSocket+0x94>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x14e7,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    120e <OpenAndConfNATPMPSocket+0xae>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1060 <syslog@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1209 <OpenAndConfNATPMPSocket+0xa9>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
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
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"bind(natpmp): %m\00"
define i32 @OpenAndConfNATPMPSocket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @LOG_ERR
%11 = call i32 @syslog(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
12: ; preds = %0
%13 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 5351)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* %2
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %12
%26 = load i32, i32* @LOG_ERR
%27 = call i32 @syslog(i32 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %1
br label %33
30: ; preds = %12
br label %31
31: ; preds = %30
%32 = load i32, i32* %2
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %25, %9
%34 = load i32, i32* %1
ret i32 %34
}

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