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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ htons(int);
int /*<<< orphan*/ inet_addr(char *);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int ntohs(scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in bind_addr, connected_addr;
  int sockfd;
  socklen_t len = sizeof(struct sockaddr);
  bind_addr.sin_family = AF_INET;
  bind_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("getsockname: error in socket %d\n", errno);
    return 1;
  }
  bind_addr.sin_port = htons(1088);
  if (bind(sockfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
    printf("getsockname: error in bind %d\n", errno);
    goto fail;
  }
  if (listen(sockfd, 3) < 0) {
    printf("getsockname: error in listen %d\n", errno);
    goto fail;
  }
  len = sizeof(connected_addr);
  if (getsockname(sockfd, (struct sockaddr *)&connected_addr, &len) < 0) {
    printf("getsockname: error in getsockname %d\n", errno);
    goto fail;
  }
  if (len != sizeof(connected_addr)) {
    printf("getsockname: unexpected length of sockaddr\n");
    goto fail;
  }
  if (ntohs(connected_addr.sin_port) != 1088) {
    printf("getsockname: port returned from getsockname is %d != %d\n",
           ntohs(connected_addr.sin_port), 1088);
    goto fail;
  }
  printf("getsockname: Local IP address is: %s:%d\n",
         inet_ntoa(connected_addr.sin_addr), ntohs(connected_addr.sin_port));
  printf("getsockname: Got socket name with static port OK\n");
  close(sockfd);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("getsockname: error in socket %d\n", errno);
    return 1;
  }
  bind_addr.sin_port = 0;
  if (bind(sockfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
    printf("getsockname: error in bind %d\n", errno);
    goto fail;
  }
  if (listen(sockfd, 3) < 0) {
    printf("getsockname: error in listen %d\n", errno);
    goto fail;
  }
  len = sizeof(connected_addr);
  if (getsockname(sockfd, (struct sockaddr *)&connected_addr, &len) < 0) {
    printf("getsockname: error in getsockname %d\n", errno);
    goto fail;
  }
  if (len != sizeof(connected_addr)) {
    printf("getsockname: unexpected length of sockaddr\n");
    goto fail;
  }
  if (connected_addr.sin_port == 0) {
    printf("getsockname: port returned from getsockname is %d == 0\n",
           ntohs(connected_addr.sin_port));
    goto fail;
  }
  close(sockfd);
  printf("getsockname: Local IP address is: %s:%d\n",
         inet_ntoa(connected_addr.sin_addr), ntohs(connected_addr.sin_port));
  printf("getsockname: Got socket name with arbitrary port OK\n");
  return 0;
fail:
  close(sockfd);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4,-0x30(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe37(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    121a <main+0x7a>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1502 <main+0x362>
mov    $0x440,%edi
call   1040 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    125f <main+0xbf>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
mov    -0x2c(%rbp),%edi
mov    $0x3,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    1292 <main+0xf2>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
movl   $0x10,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10b0 <getsockname@plt>
cmp    $0x0,%rax
jge    12cf <main+0x12f>
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
movslq -0x30(%rbp),%rax
cmp    $0x10,%rax
je     12f0 <main+0x150>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
mov    -0x28(%rbp),%rdi
call   1090 <ntohs@plt>
cmp    $0x440,%eax
je     1327 <main+0x187>
mov    -0x28(%rbp),%rdi
call   1090 <ntohs@plt>
mov    %eax,%esi
lea    0xda7(%rip),%rdi
mov    $0x440,%edx
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
mov    -0x20(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
mov    0x2c62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    13af <main+0x20f>
mov    0x2c3d(%rip),%rax
mov    (%rax),%esi
lea    0xc6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1502 <main+0x362>
movq   $0x0,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    13ee <main+0x24e>
mov    0x2bf7(%rip),%rax
mov    (%rax),%esi
lea    0xc49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
mov    -0x2c(%rbp),%edi
mov    $0x3,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    1421 <main+0x281>
mov    0x2bc4(%rip),%rax
mov    (%rax),%esi
lea    0xc35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
movl   $0x10,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10b0 <getsockname@plt>
cmp    $0x0,%rax
jge    145e <main+0x2be>
mov    0x2b87(%rip),%rax
mov    (%rax),%esi
lea    0xc19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
movslq -0x30(%rbp),%rax
cmp    $0x10,%rax
je     147f <main+0x2df>
lea    0xc1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
cmpq   $0x0,-0x28(%rbp)
jne    14a8 <main+0x308>
mov    -0x28(%rbp),%rdi
call   1090 <ntohs@plt>
mov    %eax,%esi
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14f3 <main+0x353>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <ntohs@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xc24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xca9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1502 <main+0x362>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x4,0x4(%rsp)
mov    0x2e1e(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x24(%rsp)
lea    0xe28(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x20(%rsp)
mov    (%r15),%edi
mov    0x2ded(%rip),%r12
mov    (%r12),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1385 <main+0x1d5>
mov    %eax,%ebx
mov    $0x440,%edi
call   1050 <htons@plt>
mov    %rax,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     139e <main+0x1ee>
mov    %ebx,%edi
mov    $0x3,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     13b0 <main+0x200>
movl   $0x10,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <getsockname@plt>
test   %rax,%rax
js     13c2 <main+0x212>
cmpl   $0x10,0x4(%rsp)
jne    13f3 <main+0x243>
mov    0x8(%rsp),%rdi
call   10a0 <ntohs@plt>
cmp    $0x440,%eax
jne    1401 <main+0x251>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
call   10a0 <ntohs@plt>
lea    0xe2d(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    (%r15),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1385 <main+0x1d5>
mov    %eax,%ebx
movq   $0x0,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     139e <main+0x1ee>
mov    %ebx,%edi
mov    $0x3,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     13b0 <main+0x200>
movl   $0x10,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <getsockname@plt>
test   %rax,%rax
js     13c2 <main+0x212>
cmpl   $0x10,0x4(%rsp)
jne    13f3 <main+0x243>
cmpq   $0x0,0x8(%rsp)
je     1422 <main+0x272>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
call   10a0 <ntohs@plt>
lea    0xd61(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
jmp    13e5 <main+0x235>
mov    0x2c44(%rip),%rax
mov    (%rax),%esi
lea    0xc75(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    13e0 <main+0x230>
mov    0x2c2b(%rip),%rax
mov    (%rax),%esi
lea    0xc7d(%rip),%rdi
jmp    13d2 <main+0x222>
mov    0x2c19(%rip),%rax
mov    (%rax),%esi
lea    0xc8a(%rip),%rdi
jmp    13d2 <main+0x222>
mov    0x2c07(%rip),%rax
mov    (%rax),%esi
lea    0xc99(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
jmp    13d9 <main+0x229>
mov    0x8(%rsp),%rdi
call   10a0 <ntohs@plt>
lea    0xc7f(%rip),%rdi
mov    %eax,%esi
mov    $0x440,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    13d9 <main+0x229>
xor    %edi,%edi
call   10a0 <ntohs@plt>
lea    0xcc3(%rip),%rdi
mov    %eax,%esi
jmp    13d2 <main+0x222>

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"getsockname: error in socket %d\0A\00"
@errno = global i32 0
@.str.2 = [31 x i8] c"getsockname: error in bind %d\0A\00"
@.str.3 = [33 x i8] c"getsockname: error in listen %d\0A\00"
@.str.4 = [38 x i8] c"getsockname: error in getsockname %d\0A\00"
@.str.5 = [44 x i8] c"getsockname: unexpected length of sockaddr\0A\00"
@.str.6 = [57 x i8] c"getsockname: port returned from getsockname is %d != %d\0A\00"
@.str.7 = [41 x i8] c"getsockname: Local IP address is: %s:%d\0A\00"
@.str.8 = [50 x i8] c"getsockname: Got socket name with static port OK\0A\00"
@.str.9 = [56 x i8] c"getsockname: port returned from getsockname is %d == 0\0A\00"
@.str.10 = [53 x i8] c"getsockname: Got socket name with arbitrary port OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 4, i32* %5
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load i32, i32* @errno
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 1, i32* %1
br label %134
19: ; preds = %0
%20 = call i64 @htons(i32 1088)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i64 %20, i64* %21
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 16)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %131
29: ; preds = %19
%30 = load i32, i32* %4
%31 = call i64 @listen(i32 %30, i32 3)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %131
36: ; preds = %29
store i32 16, i32* %5
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i64 @getsockname(i32 %37, %struct.sockaddr* %38, i32* %5)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %42)
br label %131
44: ; preds = %36
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = icmp ne i64 %46, 16
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %131
50: ; preds = %44
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%52 = load i64, i64* %51
%53 = call i32 @ntohs(i64 %52)
%54 = icmp ne i32 %53, 1088
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%57 = load i64, i64* %56
%58 = call i32 @ntohs(i64 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0), i32 %58, i32 1088)
br label %131
60: ; preds = %50
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i8* @inet_ntoa(i32 %63)
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%66 = load i64, i64* %65
%67 = call i32 @ntohs(i64 %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %64, i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.8, i64 0, i64 0))
%70 = load i32, i32* %4
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* @AF_INET
%73 = load i32, i32* @SOCK_STREAM
%74 = call i32 @socket(i32 %72, i32 %73, i32 0)
store i32 %74, i32* %4
%75 = load i32, i32* %4
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %60
%78 = load i32, i32* @errno
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %78)
store i32 1, i32* %1
br label %134
80: ; preds = %60
%81 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i64 0, i64* %81
%82 = load i32, i32* %4
%83 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%84 = call i64 @bind(i32 %82, %struct.sockaddr* %83, i32 16)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %80
%87 = load i32, i32* @errno
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %87)
br label %131
89: ; preds = %80
%90 = load i32, i32* %4
%91 = call i64 @listen(i32 %90, i32 3)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %96
93: ; preds = %89
%94 = load i32, i32* @errno
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %94)
br label %131
96: ; preds = %89
store i32 16, i32* %5
%97 = load i32, i32* %4
%98 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%99 = call i64 @getsockname(i32 %97, %struct.sockaddr* %98, i32* %5)
%100 = icmp slt i64 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %96
%102 = load i32, i32* @errno
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %102)
br label %131
104: ; preds = %96
%105 = load i32, i32* %5
%106 = sext i32 %105 to i64
%107 = icmp ne i64 %106, 16
br i1 %107, label %108, label %110
108: ; preds = %104
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %131
110: ; preds = %104
%111 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%112 = load i64, i64* %111
%113 = icmp eq i64 %112, 0
br i1 %113, label %114, label %119
114: ; preds = %110
%115 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%116 = load i64, i64* %115
%117 = call i32 @ntohs(i64 %116)
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0), i32 %117)
br label %131
119: ; preds = %110
%120 = load i32, i32* %4
%121 = call i32 @close(i32 %120)
%122 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%123 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %122, i32 0, i32 0
%124 = load i32, i32* %123
%125 = call i8* @inet_ntoa(i32 %124)
%126 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%127 = load i64, i64* %126
%128 = call i32 @ntohs(i64 %127)
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %125, i32 %128)
%130 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %1
br label %134
131: ; preds = %114, %108, %101, %93, %86, %55, %48, %41, %33, %26
%132 = load i32, i32* %4
%133 = call i32 @close(i32 %132)
store i32 1, i32* %1
br label %134
134: ; preds = %131, %119, %77, %16
%135 = load i32, i32* %1
ret i32 %135
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"getsockname: error in socket %d\0A\00"
@errno = global i32 0
@.str.2 = [31 x i8] c"getsockname: error in bind %d\0A\00"
@.str.3 = [33 x i8] c"getsockname: error in listen %d\0A\00"
@.str.4 = [38 x i8] c"getsockname: error in getsockname %d\0A\00"
@.str.6 = [57 x i8] c"getsockname: port returned from getsockname is %d != %d\0A\00"
@.str.7 = [41 x i8] c"getsockname: Local IP address is: %s:%d\0A\00"
@.str.9 = [56 x i8] c"getsockname: port returned from getsockname is %d == 0\0A\00"
@str = [49 x i8] c"getsockname: Got socket name with static port OK\00"
@str.11 = [52 x i8] c"getsockname: Got socket name with arbitrary port OK\00"
@str.13 = [43 x i8] c"getsockname: unexpected length of sockaddr\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %110
18: ; preds = %0
%19 = tail call i64 @htons(i32 1088)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i64 %19, i64* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @bind(i32 %13, %struct.sockaddr* %21, i32 16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %107
27: ; preds = %18
%28 = call i64 @listen(i32 %13, i32 3)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %107
33: ; preds = %27
store i32 16, i32* %3
%34 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%35 = call i64 @getsockname(i32 %13, %struct.sockaddr* %34, i32* %3)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @errno
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %107
40: ; preds = %33
%41 = load i32, i32* %3
%42 = icmp eq i32 %41, 16
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0))
br label %107
45: ; preds = %40
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%47 = load i64, i64* %46
%48 = call i32 @ntohs(i64 %47)
%49 = icmp eq i32 %48, 1088
br i1 %49, label %54, label %50
50: ; preds = %45
%51 = load i64, i64* %46
%52 = call i32 @ntohs(i64 %51)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0), i32 %52, i32 1088)
br label %107
54: ; preds = %45
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%56 = load i32, i32* %55
%57 = call i8* @inet_ntoa(i32 %56)
%58 = load i64, i64* %46
%59 = call i32 @ntohs(i64 %58)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %57, i32 %59)
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%62 = call i32 @close(i32 %13)
%63 = load i32, i32* @AF_INET
%64 = load i32, i32* @SOCK_STREAM
%65 = call i32 @socket(i32 %63, i32 %64, i32 0)
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %54
%68 = load i32, i32* @errno
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %68)
br label %110
70: ; preds = %54
store i64 0, i64* %20
%71 = call i64 @bind(i32 %65, %struct.sockaddr* %21, i32 16)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32, i32* @errno
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %74)
br label %107
76: ; preds = %70
%77 = call i64 @listen(i32 %65, i32 3)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i32, i32* @errno
%81 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %80)
br label %107
82: ; preds = %76
store i32 16, i32* %3
%83 = call i64 @getsockname(i32 %65, %struct.sockaddr* %34, i32* %3)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i32, i32* @errno
%87 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %86)
br label %107
88: ; preds = %82
%89 = load i32, i32* %3
%90 = icmp eq i32 %89, 16
br i1 %90, label %93, label %91
91: ; preds = %88
%92 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0))
br label %107
93: ; preds = %88
%94 = load i64, i64* %46
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = call i32 @ntohs(i64 0)
%98 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0), i32 %97)
br label %107
99: ; preds = %93
%100 = call i32 @close(i32 %65)
%101 = load i32, i32* %55
%102 = call i8* @inet_ntoa(i32 %101)
%103 = load i64, i64* %46
%104 = call i32 @ntohs(i64 %103)
%105 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %102, i32 %104)
%106 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.11, i64 0, i64 0))
br label %110
107: ; preds = %96, %91, %85, %79, %73, %50, %43, %37, %30, %24
%108 = phi i32 [ %13, %24 ], [ %13, %30 ], [ %13, %37 ], [ %13, %43 ], [ %13, %50 ], [ %65, %73 ], [ %65, %79 ], [ %65, %85 ], [ %65, %91 ], [ %65, %96 ]
%109 = call i32 @close(i32 %108)
br label %110
110: ; preds = %107, %99, %67, %15
%111 = phi i32 [ 1, %15 ], [ 1, %107 ], [ 1, %67 ], [ 0, %99 ]
ret i32 %111
}