#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 */
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int int32_t;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ IPPROTO_UDP;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int32_t sock_init(in_addr_t addr, uint16_t port, int32_t non_blocking,
                  struct sockaddr_in *dest) {
  int32_t sock, flags;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    fprintf(stderr, "Failed to create socket!\n");
    return -3;
  }
  if (non_blocking) {
    if ((flags = fcntl(sock, F_GETFL, 0)) < 0) {
      fprintf(stderr, "Failed to get socket flags!\n");
      return -3;
    }
    if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
      fprintf(stderr, "Failed to set socket flags!\n");
      return -3;
    }
  }
  memset(dest, 0, sizeof(*dest));
  dest->sin_family = AF_INET;
  dest->sin_addr.s_addr = htonl(addr);
  dest->sin_port = htons(port);
  return sock;
}


x86 assembly (clang, -O0):
<sock_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c2 <sock_init+0x62>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12a0 <sock_init+0x140>
cmpl   $0x0,-0x10(%rbp)
je     1259 <sock_init+0xf9>
mov    -0x1c(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    120c <sock_init+0xac>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12a0 <sock_init+0x140>
mov    -0x1c(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2dbe(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1254 <sock_init+0xf4>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12a0 <sock_init+0x140>
jmp    1259 <sock_init+0xf9>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e6b(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     120b <sock_init+0xab>
mov    %eax,%ebp
test   %ebx,%ebx
je     11d8 <sock_init+0x78>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     121d <sock_init+0xbd>
mov    0x2e01(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e18(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     122f <sock_init+0xcf>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x8(%r12)
mov    %r15d,%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%r12)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%r12)
jmp    1249 <sock_init+0xe9>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
jmp    123f <sock_init+0xdf>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
jmp    123f <sock_init+0xdf>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xfffffffd,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Failed to create socket!\0A\00"
@F_GETFL = global i32 0
@.str.1 = [29 x i8] c"Failed to get socket flags!\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [29 x i8] c"Failed to set socket flags!\0A\00"
define i32 @sock_init(i32 %0, i32 %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = load i32, i32* @IPPROTO_UDP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -3, i32* %5
br label %60
20: ; preds = %4
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %44
23: ; preds = %20
%24 = load i32, i32* %10
%25 = load i32, i32* @F_GETFL
%26 = call i64 @fcntl(i32 %24, i32 %25, i32 0)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %11
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -3, i32* %5
br label %60
32: ; preds = %23
%33 = load i32, i32* %10
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* %11
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i64 @fcntl(i32 %33, i32 %34, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %5
br label %60
43: ; preds = %32
br label %44
44: ; preds = %43, %20
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%46 = call i32 @memset(%struct.sockaddr_in* %45, i32 0, i32 12)
%47 = load i32, i32* @AF_INET
%48 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %48, i32 0, i32 2
store i32 %47, i32* %49
%50 = load i32, i32* %6
%51 = call i32 @htonl(i32 %50)
%52 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %52, i32 0, i32 1
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
store i32 %51, i32* %54
%55 = load i32, i32* %7
%56 = call i32 @htons(i32 %55)
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 0
store i32 %56, i32* %58
%59 = load i32, i32* %10
store i32 %59, i32* %5
br label %60
60: ; preds = %44, %40, %29, %17
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Failed to create socket!\0A\00"
@F_GETFL = global i32 0
@.str.1 = [29 x i8] c"Failed to get socket flags!\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [29 x i8] c"Failed to set socket flags!\0A\00"
define i32 @sock_init(i32 %0, i32 %1, i32 %2, %struct.sockaddr_in* %3) {
%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 slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %40
13: ; preds = %4
%14 = icmp eq i32 %2, 0
br i1 %14, label %32, label %15
15: ; preds = %13
%16 = load i32, i32* @F_GETFL
%17 = tail call i64 @fcntl(i32 %8, i32 %16, i32 0)
%18 = trunc i64 %17 to i32
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %40
23: ; preds = %15
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %18
%27 = tail call i64 @fcntl(i32 %8, i32 %24, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %40
32: ; preds = %23, %13
%33 = tail call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %34, i32* %35
%36 = tail call i32 @htonl(i32 %0)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %36, i32* %37
%38 = tail call i32 @htons(i32 %1)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %38, i32* %39
br label %40
40: ; preds = %32, %29, %20, %10
%41 = phi i32 [ -3, %10 ], [ -3, %20 ], [ -3, %29 ], [ %8, %32 ]
ret i32 %41
}