#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*/ si_me;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
short atoi(char const *);
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mqtt_sn_log_debug(char *, char const *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bind_udp_socket(const char *port_str) {
  struct sockaddr_in si_me;
  short port = atoi(port_str);
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  memset((char *)&si_me, 0, sizeof(si_me));
  si_me.sin_family = AF_INET;
  si_me.sin_port = htons(port);
  si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (const struct sockaddr *)&si_me, sizeof(si_me)) == -1) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  mqtt_sn_log_debug("mqtt-sn-dump listening on port %s", port_str);
  return sock;
}


x86 assembly (clang, -O0):
<bind_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <atoi@plt>
mov    %ax,-0x1a(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11ef <bind_udp_socket+0x5f>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movswl -0x1a(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    125c <bind_udp_socket+0xcc>
lea    0xdbe(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xda5(%rip),%rdi
call   1060 <mqtt_sn_log_debug@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_udp_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    0x2e3b(%rip),%r12
mov    (%r12),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1238 <bind_udp_socket+0xa8>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
movswl %bx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1241 <bind_udp_socket+0xb1>
lea    0xdeb(%rip),%rdi
mov    %r14,%rsi
call   1060 <mqtt_sn_log_debug@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc1(%rip),%rdi
jmp    1248 <bind_udp_socket+0xb8>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [34 x i8] c"mqtt-sn-dump listening on port %s\00"
define i32 @bind_udp_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca i16
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call signext i16 @atoi(i8* %6)
store i16 %7, i16* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = bitcast %struct.sockaddr_in* %3 to i8*
%19 = call i32 @memset(i8* %18, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i16, i16* %4
%23 = call i32 @htons(i16 signext %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %5
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %17
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %17
%38 = load i8*, i8** %2
%39 = call i32 @mqtt_sn_log_debug(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [34 x i8] c"mqtt-sn-dump listening on port %s\00"
define i32 @bind_udp_socket(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = tail call signext i16 @atoi(i8* %0)
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_UDP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = call i32 @memset(i8* %3, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i16 signext %4)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %8, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %14
%31 = call i32 @mqtt_sn_log_debug(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
ret i32 %8
}