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

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ lsa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ error_exit(char *, ...);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(char *adapter, int port) {
  struct sockaddr_in lsa;
  int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (fd == -1)
    error_exit("failed creating socket");
  memset((char *)&lsa, '\0', sizeof(lsa));
  lsa.sin_family = AF_INET;
  if (inet_aton(adapter, &lsa.sin_addr) == 0)
    error_exit("%s is not a valid ip-address of an adapter to listen on",
               adapter);
  lsa.sin_port = htons(port);
  if (bind(fd, (struct sockaddr *)&lsa, sizeof(struct sockaddr)) == -1)
    error_exit("bind failed");
  return fd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11aa <create_socket+0x4a>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1050 <error_exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
add    $0x4,%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%rax
jne    11f3 <create_socket+0x93>
mov    -0x8(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <error_exit@plt>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1226 <create_socket+0xc6>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <error_exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e71(%rip),%r15
mov    (%r15),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a6 <create_socket+0x46>
lea    0xe61(%rip),%rdi
xor    %eax,%eax
call   1050 <error_exit@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <inet_aton@plt>
test   %rax,%rax
jne    11e1 <create_socket+0x81>
lea    0xe40(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <error_exit@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1211 <create_socket+0xb1>
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1050 <error_exit@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [23 x i8] c"failed creating socket\00"
@.str.1 = [56 x i8] c"%s is not a valid ip-address of an adapter to listen on\00"
@.str.2 = [12 x i8] c"bind failed\00"
define i32 @create_socket(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = bitcast %struct.sockaddr_in* %5 to i8*
%17 = call i32 @memset(i8* %16, i8 signext 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i8*, i8** %3
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%22 = call i64 @inet_aton(i8* %20, i32* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i8*, i8** %3
%26 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %15
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %27
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [23 x i8] c"failed creating socket\00"
@.str.1 = [56 x i8] c"%s is not a valid ip-address of an adapter to listen on\00"
@.str.2 = [12 x i8] c"bind failed\00"
define i32 @create_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%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 %12
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = call i32 @memset(i8* %4, i8 signext 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%17 = call i64 @inet_aton(i8* %0, i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %19, %12
%22 = call i32 @htons(i32 %1)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %8, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
ret i32 %8
}