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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ udp_socket_error();
int udp_socket_create(int port) {
  int rc;
  int s;
  struct sockaddr_in myaddr;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    fprintf(stderr, "could not open socket, errno=%d\n", errno);
    udp_socket_error();
    return s;
  }
  rc = fcntl(s, F_SETFL, O_NONBLOCK);
  if (rc < 0) {
    fprintf(stderr, "could not set nonblocking, errno=%d\n", errno);
    udp_socket_error();
    return rc;
  }
  myaddr.sin_family = AF_INET;
  myaddr.sin_port = htons(port);
  myaddr.sin_addr.s_addr = INADDR_ANY;
  rc = bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr));
  if (rc < 0) {
    fprintf(stderr, "could not bind socket, errno=%d\n", errno);
    udp_socket_error();
    return rc;
  }
  return s;
}


x86 assembly (clang, -O0):
<udp_socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c1 <udp_socket_create+0x61>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1060 <udp_socket_error@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <udp_socket_create+0x12f>
mov    -0x10(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1218 <udp_socket_create+0xb8>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    0xe20(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1060 <udp_socket_error@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <udp_socket_create+0x12f>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1289 <udp_socket_create+0x129>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
lea    0xdd4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1060 <udp_socket_error@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <udp_socket_create+0x12f>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_socket_create>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fc <udp_socket_create+0x9c>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     1223 <udp_socket_create+0xc3>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jns    124c <udp_socket_create+0xec>
mov    %eax,%ebp
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0xe4c(%rip),%rsi
jmp    123e <udp_socket_create+0xde>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
lea    0xdeb(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1060 <udp_socket_error@plt>
jmp    124c <udp_socket_create+0xec>
mov    %eax,%ebp
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
lea    0xde3(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1060 <udp_socket_error@plt>
mov    %ebp,%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
@stderr = global i32 0
@.str = [33 x i8] c"could not open socket, errno=%d\0A\00"
@errno = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [37 x i8] c"could not set nonblocking, errno=%d\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [33 x i8] c"could not bind socket, errno=%d\0A\00"
define i32 @udp_socket_create(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 (...) @udp_socket_error()
%17 = load i32, i32* %5
store i32 %17, i32* %2
br label %53
18: ; preds = %1
%19 = load i32, i32* %5
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = call i32 @fcntl(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @udp_socket_error()
%30 = load i32, i32* %4
store i32 %30, i32* %2
br label %53
31: ; preds = %18
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* %3
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %5
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %31
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = call i32 (...) @udp_socket_error()
%50 = load i32, i32* %4
store i32 %50, i32* %2
br label %53
51: ; preds = %31
%52 = load i32, i32* %5
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %45, %25, %12
%54 = load i32, i32* %2
ret i32 %54
}

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
@stderr = global i32 0
@.str = [33 x i8] c"could not open socket, errno=%d\0A\00"
@errno = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [37 x i8] c"could not set nonblocking, errno=%d\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [33 x i8] c"could not bind socket, errno=%d\0A\00"
define i32 @udp_socket_create(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = tail call i32 (...) @udp_socket_error()
br label %38
13: ; preds = %1
%14 = load i32, i32* @F_SETFL
%15 = load i32, i32* @O_NONBLOCK
%16 = tail call i32 @fcntl(i32 %6, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = tail call i32 (...) @udp_socket_error()
br label %38
23: ; preds = %13
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %24, i32* %25
%26 = tail call i32 @htons(i32 %0)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%31 = call i32 @bind(i32 %6, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = call i32 (...) @udp_socket_error()
br label %38
38: ; preds = %23, %33, %18, %8
%39 = phi i32 [ %6, %8 ], [ %16, %18 ], [ %31, %33 ], [ %6, %23 ]
ret i32 %39
}