#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ CLIENT;

/* Variables and functions */
int /*<<< orphan*/ BUFSIZ;
int /*<<< orphan*/ CLSET_TIMEOUT;
int /*<<< orphan*/ clnt_control(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                char *);
int /*<<< orphan*/ clnt_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ clnt_pcreateerror(char *);
int /*<<< orphan*/ *clnttcp_create(struct sockaddr_in *, unsigned int,
                                   unsigned int, int *, int /*<<< orphan*/,
                                   int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getsockname(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
CLIENT *convert_socket_to_rpc_client(int connfd, unsigned int prog,
                                     unsigned int vers) {
  struct sockaddr_in control_name;
  struct timeval tv;
  unsigned int control_name_len = sizeof(struct sockaddr);
  CLIENT *clnt;
  if (getsockname(connfd, (struct sockaddr *)&control_name, &control_name_len) <
      0) {
    perror("getsockname");
    return NULL;
  }
  if ((clnt = clnttcp_create(&control_name, prog, vers, &connfd, BUFSIZ,
                             BUFSIZ)) == NULL) {
    clnt_pcreateerror("clnttcp_create");
    return NULL;
  }
  tv.tv_sec = 30 * 60;
  tv.tv_usec = 0;
  if (!clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv)) {
    fprintf(stderr, "rpc_init: changing timeout failed");
    clnt_destroy(clnt);
    return NULL;
  }
  return clnt;
}


x86 assembly (clang, -O0):
<convert_socket_to_rpc_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1090 <getsockname@plt>
cmp    $0x0,%rax
jge    11bb <convert_socket_to_rpc_client+0x4b>
lea    0xe57(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    126a <convert_socket_to_rpc_client+0xfa>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    0x2e10(%rip),%rax
mov    (%rax),%r8d
mov    0x2e06(%rip),%rax
mov    (%rax),%r9d
lea    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rcx
call   1050 <clnttcp_create@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1209 <convert_socket_to_rpc_client+0x99>
lea    0xe15(%rip),%rdi
call   1060 <clnt_pcreateerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    126a <convert_socket_to_rpc_client+0xfa>
movl   $0x708,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <clnt_control@plt>
cmp    $0x0,%eax
jne    1262 <convert_socket_to_rpc_client+0xf2>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <clnt_destroy@plt>
movq   $0x0,-0x8(%rbp)
jmp    126a <convert_socket_to_rpc_client+0xfa>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_socket_to_rpc_client>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,0x14(%rsp)
movl   $0x4,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1090 <getsockname@plt>
test   %rax,%rax
js     1228 <convert_socket_to_rpc_client+0xb8>
mov    0x2e33(%rip),%rax
mov    (%rax),%r8d
lea    0x8(%rsp),%rdi
lea    0x14(%rsp),%rcx
mov    %ebp,%esi
mov    %ebx,%edx
mov    %r8d,%r9d
call   1050 <clnttcp_create@plt>
test   %rax,%rax
je     1210 <convert_socket_to_rpc_client+0xa0>
mov    %rax,%rbx
movl   $0x708,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
call   1030 <clnt_control@plt>
test   %eax,%eax
jne    121e <convert_socket_to_rpc_client+0xae>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <clnt_destroy@plt>
jmp    121c <convert_socket_to_rpc_client+0xac>
lea    0xdf5(%rip),%rdi
call   1060 <clnt_pcreateerror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
jmp    121c <convert_socket_to_rpc_client+0xac>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getsockname\00"
@BUFSIZ = global i32 0
@.str.1 = [15 x i8] c"clnttcp_create\00"
@CLSET_TIMEOUT = global i32 0
@stderr = global i32 0
@.str.2 = [34 x i8] c"rpc_init: changing timeout failed\00"
define i32* @convert_socket_to_rpc_client(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 4, i32* %10
%12 = load i32, i32* %5
%13 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%14 = call i64 @getsockname(i32 %12, %struct.sockaddr* %13, i32* %10)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %4
br label %42
18: ; preds = %3
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = load i32, i32* @BUFSIZ
%22 = load i32, i32* @BUFSIZ
%23 = call i32* @clnttcp_create(%struct.sockaddr_in* %8, i32 %19, i32 %20, i32* %5, i32 %21, i32 %22)
store i32* %23, i32** %11
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @clnt_pcreateerror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %4
br label %42
27: ; preds = %18
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1800, i32* %28
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %29
%30 = load i32*, i32** %11
%31 = load i32, i32* @CLSET_TIMEOUT
%32 = bitcast %struct.timeval* %9 to i8*
%33 = call i32 @clnt_control(i32* %30, i32 %31, i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %27
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32*, i32** %11
%39 = call i32 @clnt_destroy(i32* %38)
store i32* null, i32** %4
br label %42
40: ; preds = %27
%41 = load i32*, i32** %11
store i32* %41, i32** %4
br label %42
42: ; preds = %40, %35, %25, %16
%43 = load i32*, i32** %4
ret i32* %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getsockname\00"
@BUFSIZ = global i32 0
@.str.1 = [15 x i8] c"clnttcp_create\00"
@CLSET_TIMEOUT = global i32 0
@stderr = global i32 0
@.str.2 = [34 x i8] c"rpc_init: changing timeout failed\00"
define i32* @convert_socket_to_rpc_client(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.timeval
%7 = alloca i32
store i32 %0, i32* %4
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = bitcast %struct.timeval* %6 to i8*
%10 = bitcast i32* %7 to i8*
store i32 4, i32* %7
%11 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%12 = call i64 @getsockname(i32 %0, %struct.sockaddr* %11, i32* %7)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %32
16: ; preds = %3
%17 = load i32, i32* @BUFSIZ
%18 = call i32* @clnttcp_create(%struct.sockaddr_in* %5, i32 %1, i32 %2, i32* %4, i32 %17, i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @clnt_pcreateerror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %32
22: ; preds = %16
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 1800, i32* %23
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %24
%25 = load i32, i32* @CLSET_TIMEOUT
%26 = call i32 @clnt_control(i32* %18, i32 %25, i8* %9)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @clnt_destroy(i32* %18)
br label %32
32: ; preds = %22, %28, %20, %14
%33 = phi i32* [ null, %14 ], [ null, %20 ], [ null, %28 ], [ %18, %22 ]
ret i32* %33
}