#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 timeval {
  unsigned int tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct in_addr *, int);
unsigned int ntohs(scalar_t__);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char const *,
                      int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_ping_init(struct in_addr listen_ip, unsigned int u_timeout,
                  unsigned int *server_port, int sock[2]) {
  struct sockaddr_in server;
  struct timeval t = {.tv_sec = 0, .tv_usec = u_timeout};
  socklen_t s_len = sizeof(server);
  sock[0] = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock[0] < 0) {
    ksft_print_msg("%d[%u] "
                   "socket()"
                   ": %m"
                   "\n",
                   getpid(), 589);
    return -1;
  }
  server.sin_family = AF_INET;
  server.sin_port = 0;
  memcpy(&server.sin_addr.s_addr, &listen_ip, sizeof(struct in_addr));
  if (bind(sock[0], (struct sockaddr *)&server, s_len)) {
    ksft_print_msg("%d[%u] "
                   "bind()"
                   ": %m"
                   "\n",
                   getpid(), 598);
    goto err_close_server;
  }
  if (getsockname(sock[0], (struct sockaddr *)&server, &s_len)) {
    ksft_print_msg("%d[%u] "
                   "getsockname()"
                   ": %m"
                   "\n",
                   getpid(), 603);
    goto err_close_server;
  }
  *server_port = ntohs(server.sin_port);
  if (setsockopt(sock[0], SOL_SOCKET, SO_RCVTIMEO, (const char *)&t,
                 sizeof t)) {
    ksft_print_msg("%d[%u] "
                   "setsockopt()"
                   ": %m"
                   "\n",
                   getpid(), 610);
    goto err_close_server;
  }
  sock[1] = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock[1] < 0) {
    ksft_print_msg("%d[%u] "
                   "socket()"
                   ": %m"
                   "\n",
                   getpid(), 616);
    goto err_close_server;
  }
  return 0;
err_close_server:
  close(sock[0]);
  return -1;
}


x86 assembly (clang, -O0):
<udp_ping_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x10,-0x3c(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jge    120e <udp_ping_init+0x7e>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe08(%rip),%rdi
mov    $0x24d,%edx
call   1080 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <udp_ping_init+0x1ed>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1273 <udp_ping_init+0xe3>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdb1(%rip),%rdi
mov    $0x256,%edx
call   1080 <ksft_print_msg@plt>
jmp    136b <udp_ping_init+0x1db>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0x0,%rax
je     12af <udp_ping_init+0x11f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd88(%rip),%rdi
mov    $0x25b,%edx
call   1080 <ksft_print_msg@plt>
jmp    136b <udp_ping_init+0x1db>
mov    -0x30(%rbp),%rdi
call   1070 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
mov    0x2cf2(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1310 <udp_ping_init+0x180>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd41(%rip),%rdi
mov    $0x262,%edx
call   1080 <ksft_print_msg@plt>
jmp    136b <udp_ping_init+0x1db>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,0x4(%rax)
jge    135f <udp_ping_init+0x1cf>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xcb0(%rip),%rdi
mov    $0x268,%edx
call   1080 <ksft_print_msg@plt>
jmp    136b <udp_ping_init+0x1db>
movl   $0x0,-0x4(%rbp)
jmp    137d <udp_ping_init+0x1ed>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_ping_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %edi,0x8(%rsp)
mov    %esi,0x10(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x10,0xc(%rsp)
mov    0x2e21(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0f(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     122d <udp_ping_init+0x9d>
mov    0x0(%rbp),%eax
mov    %eax,0x24(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    (%rbx),%edi
lea    0x18(%rsp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     124c <udp_ping_init+0xbc>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf4(%rip),%rdi
mov    %eax,%esi
mov    $0x256,%edx
jmp    12c4 <udp_ping_init+0x134>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdc5(%rip),%rdi
mov    %eax,%esi
mov    $0x24d,%edx
call   1080 <ksft_print_msg@plt>
jmp    12d0 <udp_ping_init+0x140>
mov    (%rbx),%edi
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10a0 <getsockname@plt>
test   %rax,%rax
je     1279 <udp_ping_init+0xe9>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdb8(%rip),%rdi
mov    %eax,%esi
mov    $0x25b,%edx
jmp    12c4 <udp_ping_init+0x134>
mov    0x18(%rsp),%rdi
call   1070 <ntohs@plt>
mov    %eax,(%r14)
mov    (%rbx),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     12e2 <udp_ping_init+0x152>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd85(%rip),%rdi
mov    %eax,%esi
mov    $0x262,%edx
call   1080 <ksft_print_msg@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%edi
mov    (%r15),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,0x4(%rbx)
test   %eax,%eax
jns    12d5 <udp_ping_init+0x145>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xcfa(%rip),%rdi
mov    %eax,%esi
mov    $0x268,%edx
jmp    12c4 <udp_ping_init+0x134>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"%d[%u] socket(): %m\0A\00"
@.str.1 = [19 x i8] c"%d[%u] bind(): %m\0A\00"
@.str.2 = [26 x i8] c"%d[%u] getsockname(): %m\0A\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.3 = [25 x i8] c"%d[%u] setsockopt(): %m\0A\00"
define i32 @udp_ping_init(i32 %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.in_addr
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %6, i32 0, i32 0
store i32 %0, i32* %13
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%15 = load i32, i32* %7
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 0, i32* %16
store i32 16, i32* %12
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = load i32*, i32** %9
%21 = getelementptr inbounds i32, i32* %20, i64 0
store i32 %19, i32* %21
%22 = load i32*, i32** %9
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %4
%27 = call i32 (...) @getpid()
%28 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %27, i32 589)
store i32 -1, i32* %5
br label %91
29: ; preds = %4
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i64 0, i64* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = call i32 @memcpy(i32* %34, %struct.in_addr* %6, i32 4)
%36 = load i32*, i32** %9
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%40 = load i32, i32* %12
%41 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %29
%44 = call i32 (...) @getpid()
%45 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %44, i32 598)
br label %86
46: ; preds = %29
%47 = load i32*, i32** %9
%48 = getelementptr inbounds i32, i32* %47, i64 0
%49 = load i32, i32* %48
%50 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%51 = call i64 @getsockname(i32 %49, %struct.sockaddr* %50, i32* %12)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = call i32 (...) @getpid()
%55 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %54, i32 603)
br label %86
56: ; preds = %46
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%58 = load i64, i64* %57
%59 = call i32 @ntohs(i64 %58)
%60 = load i32*, i32** %8
store i32 %59, i32* %60
%61 = load i32*, i32** %9
%62 = getelementptr inbounds i32, i32* %61, i64 0
%63 = load i32, i32* %62
%64 = load i32, i32* @SOL_SOCKET
%65 = load i32, i32* @SO_RCVTIMEO
%66 = bitcast %struct.timeval* %11 to i8*
%67 = call i64 @setsockopt(i32 %63, i32 %64, i32 %65, i8* %66, i32 8)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %56
%70 = call i32 (...) @getpid()
%71 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %70, i32 610)
br label %86
72: ; preds = %56
%73 = load i32, i32* @AF_INET
%74 = load i32, i32* @SOCK_DGRAM
%75 = call i32 @socket(i32 %73, i32 %74, i32 0)
%76 = load i32*, i32** %9
%77 = getelementptr inbounds i32, i32* %76, i64 1
store i32 %75, i32* %77
%78 = load i32*, i32** %9
%79 = getelementptr inbounds i32, i32* %78, i64 1
%80 = load i32, i32* %79
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %72
%83 = call i32 (...) @getpid()
%84 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %83, i32 616)
br label %86
85: ; preds = %72
store i32 0, i32* %5
br label %91
86: ; preds = %82, %69, %53, %43
%87 = load i32*, i32** %9
%88 = getelementptr inbounds i32, i32* %87, i64 0
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %5
br label %91
91: ; preds = %86, %85, %26
%92 = load i32, i32* %5
ret i32 %92
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"%d[%u] socket(): %m\0A\00"
@.str.1 = [19 x i8] c"%d[%u] bind(): %m\0A\00"
@.str.2 = [26 x i8] c"%d[%u] getsockname(): %m\0A\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.3 = [25 x i8] c"%d[%u] setsockopt(): %m\0A\00"
define i32 @udp_ping_init(i32 %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture %3) {
%5 = alloca %struct.in_addr
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i64 0, i32 0
store i32 %0, i32* %9
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = bitcast %struct.timeval* %7 to i8*
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %1, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i32 0, i32* %13
%14 = bitcast i32* %8 to i8*
store i32 16, i32* %8
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %4
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 @ksft_print_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %20, i32 589)
br label %65
22: ; preds = %4
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
%27 = call i32 @memcpy(i32* %26, %struct.in_addr* %5, i32 4)
%28 = load i32, i32* %3
%29 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 16)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %22
%33 = call i32 (...) @getpid()
%34 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 598)
br label %62
35: ; preds = %22
%36 = load i32, i32* %3
%37 = call i64 @getsockname(i32 %36, %struct.sockaddr* %29, i32* %8)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = call i32 (...) @getpid()
%41 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %40, i32 603)
br label %62
42: ; preds = %35
%43 = load i64, i64* %25
%44 = call i32 @ntohs(i64 %43)
store i32 %44, i32* %2
%45 = load i32, i32* %3
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_RCVTIMEO
%48 = call i64 @setsockopt(i32 %45, i32 %46, i32 %47, i8* %11, i32 8)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %42
%51 = call i32 (...) @getpid()
%52 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 610)
br label %62
53: ; preds = %42
%54 = load i32, i32* @AF_INET
%55 = load i32, i32* @SOCK_DGRAM
%56 = call i32 @socket(i32 %54, i32 %55, i32 0)
%57 = getelementptr inbounds i32, i32* %3, i64 1
store i32 %56, i32* %57
%58 = icmp slt i32 %56, 0
br i1 %58, label %59, label %65
59: ; preds = %53
%60 = call i32 (...) @getpid()
%61 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %60, i32 616)
br label %62
62: ; preds = %59, %50, %39, %32
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %53, %62, %19
%66 = phi i32 [ -1, %19 ], [ -1, %62 ], [ 0, %53 ]
ret i32 %66
}