#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 */
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ close(int);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
               int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int util_get_udp_buffer_size(int sd) {
  int size;
  int len = sizeof(int);
  if (sd == -1) {
    int fd;
    if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
      perror("socket");
      return (0);
    }
    if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, (socklen_t *)&len) == -1) {
      perror("getsockopt(bufsize)");
      close(fd);
    }
    return (size);
  }
  if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &size, (socklen_t *)&len) == -1) {
    perror("getsockopt(bufsize)");
  }
  return (size);
}


x86 assembly (clang, -O0):
<util_get_udp_buffer_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x10(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ea <util_get_udp_buffer_size+0xaa>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <util_get_udp_buffer_size+0x60>
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1227 <util_get_udp_buffer_size+0xe7>
mov    -0x14(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0xffffffff,%eax
jne    11df <util_get_udp_buffer_size+0x9f>
lea    0xe35(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <util_get_udp_buffer_size+0xe7>
mov    -0x8(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0xffffffff,%eax
jne    1221 <util_get_udp_buffer_size+0xe1>
lea    0xdeb(%rip),%rdi
call   1050 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_get_udp_buffer_size>:
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0xc(%rsp)
cmp    $0xffffffff,%edi
je     1186 <util_get_udp_buffer_size+0x46>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1040 <getsockopt@plt>
cmp    $0xffffffff,%eax
jne    11d5 <util_get_udp_buffer_size+0x95>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
jmp    11d5 <util_get_udp_buffer_size+0x95>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11df <util_get_udp_buffer_size+0x9f>
mov    %eax,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
cmp    $0xffffffff,%eax
je     11ef <util_get_udp_buffer_size+0xaf>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe1a(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    11d9 <util_get_udp_buffer_size+0x99>
lea    0xe11(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11d5 <util_get_udp_buffer_size+0x95>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.1 = [20 x i8] c"getsockopt(bufsize)\00"
define i32 @util_get_udp_buffer_size(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %29
9: ; preds = %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %39
17: ; preds = %9
%18 = load i32, i32* %6
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_RCVBUF
%21 = call i32 @getsockopt(i32 %18, i32 %19, i32 %20, i32* %4, i32* %5)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %23, %17
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %39
29: ; preds = %1
%30 = load i32, i32* %3
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_RCVBUF
%33 = call i32 @getsockopt(i32 %30, i32 %31, i32 %32, i32* %4, i32* %5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %27, %15
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.1 = [20 x i8] c"getsockopt(bufsize)\00"
define i32 @util_get_udp_buffer_size(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = icmp eq i32 %0, -1
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %34
15: ; preds = %7
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_RCVBUF
%18 = call i32 @getsockopt(i32 %11, i32 %16, i32 %17, i32* %2, i32* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @close(i32 %11)
br label %23
23: ; preds = %20, %15
%24 = load i32, i32* %2
br label %34
25: ; preds = %1
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_RCVBUF
%28 = call i32 @getsockopt(i32 %0, i32 %26, i32 %27, i32* %2, i32* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
%33 = load i32, i32* %2
br label %34
34: ; preds = %13, %23, %32
%35 = phi i32 [ %33, %32 ], [ 0, %13 ], [ %24, %23 ]
ret i32 %35
}