#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 sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct android_sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ stor;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ darwin_convert_addr_from_native(struct sockaddr *,
                                                   struct android_sockaddr *,
                                                   int);
int getsockname(int, struct sockaddr *, int *);
int darwin_my_getsockname(int sockfd, struct android_sockaddr *addr,
                          socklen_t *addrlen) {
  struct sockaddr_storage stor;
  socklen_t ret_size = sizeof(stor);
  int ret = getsockname(sockfd, (struct sockaddr *)&stor, &ret_size);
  if (ret == 0) {
    if (!darwin_convert_addr_from_native((struct sockaddr *)&stor, addr,
                                         *addrlen))
      return -1;
    *addrlen = ret_size - 1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<darwin_my_getsockname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getsockname@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    118b <darwin_my_getsockname+0x6b>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1030 <darwin_convert_addr_from_native@plt>
cmp    $0x0,%eax
jne    117f <darwin_my_getsockname+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1191 <darwin_my_getsockname+0x71>
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<darwin_my_getsockname>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <getsockname@plt>
test   %eax,%eax
jne    116f <darwin_my_getsockname+0x4f>
mov    (%r14),%edx
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <darwin_convert_addr_from_native@plt>
test   %eax,%eax
je     116a <darwin_my_getsockname+0x4a>
mov    0xc(%rsp),%eax
add    $0xffffffff,%eax
mov    %eax,(%r14)
xor    %eax,%eax
jmp    116f <darwin_my_getsockname+0x4f>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.android_sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @darwin_my_getsockname(i32 %0, %struct.android_sockaddr* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.android_sockaddr*
%7 = alloca i32*
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store %struct.android_sockaddr* %1, %struct.android_sockaddr** %6
store i32* %2, i32** %7
store i32 4, i32* %9
%11 = load i32, i32* %5
%12 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%13 = call i32 @getsockname(i32 %11, %struct.sockaddr* %12, i32* %9)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %3
%17 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%18 = load %struct.android_sockaddr*, %struct.android_sockaddr** %6
%19 = load i32*, i32** %7
%20 = load i32, i32* %19
%21 = call i32 @darwin_convert_addr_from_native(%struct.sockaddr* %17, %struct.android_sockaddr* %18, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %16
store i32 -1, i32* %4
br label %30
24: ; preds = %16
%25 = load i32, i32* %9
%26 = sub nsw i32 %25, 1
%27 = load i32*, i32** %7
store i32 %26, i32* %27
br label %28
28: ; preds = %24, %3
%29 = load i32, i32* %10
store i32 %29, i32* %4
br label %30
30: ; preds = %28, %23
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.android_sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @darwin_my_getsockname(i32 %0, %struct.android_sockaddr* %1, i32* nocapture %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%9 = call i32 @getsockname(i32 %0, %struct.sockaddr* %8, i32* %5)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %3
%12 = load i32, i32* %2
%13 = call i32 @darwin_convert_addr_from_native(%struct.sockaddr* %8, %struct.android_sockaddr* %1, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = load i32, i32* %5
%17 = add nsw i32 %16, -1
store i32 %17, i32* %2
br label %18
18: ; preds = %3, %15, %11
%19 = phi i32 [ -1, %11 ], [ 0, %15 ], [ %9, %3 ]
ret i32 %19
}