#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_in {
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ self;
typedef int /*<<< orphan*/ peer;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ getpeername(int, struct sockaddr *, int *);
scalar_t__ getsockname(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ losing(char *, char *);
int ntohs(scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
void dump_addr(int fd, char *name, int do_peer) {
  struct sockaddr_in self;
  socklen_t self_len = sizeof(self);
  struct sockaddr_in peer;
  socklen_t peer_len = sizeof(peer);
  char self_name[256];
  char peer_name[256];
  if (fd == 1) {
    fprintf(stderr, "socket %s: stdout\n", name);
    return;
  }
  if (getsockname(fd, (struct sockaddr *)&self, &self_len) < 0)
    losing(name, "dump_addr self");
  strcpy(self_name, inet_ntoa(self.sin_addr));
  if (do_peer) {
    if (getpeername(fd, (struct sockaddr *)&peer, &peer_len) < 0)
      losing(name, "dump_addr peer");
    strcpy(peer_name, inet_ntoa(peer.sin_addr));
  } else {
    strcpy(peer_name, "none");
    peer.sin_port = 0;
  }
  fprintf(stderr, "socket %s: self %s:%d peer %s:%d\n", name, self_name,
          ntohs(self.sin_port), peer_name, ntohs(peer.sin_port));
}


x86 assembly (clang, -O0):
<dump_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x10,-0x2c(%rbp)
movl   $0x10,-0x44(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    11bd <dump_addr+0x4d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1306 <dump_addr+0x196>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1090 <getsockname@plt>
cmp    $0x0,%rax
jge    11e7 <dump_addr+0x77>
mov    -0x10(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1040 <losing@plt>
lea    -0x150(%rbp),%rax
mov    %rax,-0x258(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0x258(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
cmpl   $0x0,-0x14(%rbp)
je     126a <dump_addr+0xfa>
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   1060 <getpeername@plt>
cmp    $0x0,%rax
jge    1240 <dump_addr+0xd0>
mov    -0x10(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1040 <losing@plt>
lea    -0x250(%rbp),%rax
mov    %rax,-0x260(%rbp)
mov    -0x38(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0x260(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
jmp    1285 <dump_addr+0x115>
lea    -0x250(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   1030 <strcpy@plt>
movq   $0x0,-0x40(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x284(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x280(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x278(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <ntohs@plt>
mov    %eax,-0x26c(%rbp)
lea    -0x250(%rbp),%rax
mov    %rax,-0x268(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <ntohs@plt>
mov    -0x284(%rbp),%edi
mov    -0x280(%rbp),%rdx
mov    -0x278(%rbp),%rcx
mov    -0x26c(%rbp),%r8d
mov    -0x268(%rbp),%r9
lea    0xd3a(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1070 <fprintf@plt>
add    $0x290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x230,%rsp
mov    %rsi,%r14
movl   $0x10,0xc(%rsp)
movl   $0x10,0x8(%rsp)
cmp    $0x1,%edi
jne    11b8 <dump_addr+0x48>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <fprintf@plt>
mov    %edx,%ebx
mov    %edi,%ebp
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1090 <getsockname@plt>
test   %rax,%rax
jns    11df <dump_addr+0x6f>
lea    0xe3c(%rip),%rsi
mov    %r14,%rdi
call   1040 <losing@plt>
mov    0x28(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0x130(%rsp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
test   %ebx,%ebx
je     1239 <dump_addr+0xc9>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <getpeername@plt>
test   %rax,%rax
jns    1221 <dump_addr+0xb1>
lea    0xe09(%rip),%rsi
mov    %r14,%rdi
call   1040 <losing@plt>
mov    0x18(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0x30(%rsp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
jmp    1253 <dump_addr+0xe3>
lea    0xdf1(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1030 <strcpy@plt>
movq   $0x0,0x10(%rsp)
mov    0x2d86(%rip),%rax
mov    (%rax),%ebx
mov    0x20(%rsp),%rdi
call   1080 <ntohs@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%rdi
call   1080 <ntohs@plt>
mov    %eax,(%rsp)
lea    0xdba(%rip),%rsi
lea    0x130(%rsp),%rcx
lea    0x30(%rsp),%r9
mov    %ebx,%edi
mov    %r14,%rdx
mov    %ebp,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x230,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"socket %s: stdout\0A\00"
@.str.1 = [15 x i8] c"dump_addr self\00"
@.str.2 = [15 x i8] c"dump_addr peer\00"
@.str.3 = [5 x i8] c"none\00"
@.str.4 = [34 x i8] c"socket %s: self %s:%d peer %s:%d\0A\00"
define void @dump_addr(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 16, i32* %8
store i32 16, i32* %10
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %5
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %65
19: ; preds = %3
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%22 = call i64 @getsockname(i32 %20, %struct.sockaddr* %21, i32* %8)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i8*, i8** %5
%26 = call i32 @losing(i8* %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %19
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i8* @inet_ntoa(i32 %30)
%32 = call i32 @strcpy(i8* %28, i8* %31)
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %27
%36 = load i32, i32* %4
%37 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%38 = call i64 @getpeername(i32 %36, %struct.sockaddr* %37, i32* %10)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i8*, i8** %5
%42 = call i32 @losing(i8* %41, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %40, %35
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%46 = load i32, i32* %45
%47 = call i8* @inet_ntoa(i32 %46)
%48 = call i32 @strcpy(i8* %44, i8* %47)
br label %53
49: ; preds = %27
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%51 = call i32 @strcpy(i8* %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i64 0, i64* %52
br label %53
53: ; preds = %49, %43
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %5
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%58 = load i64, i64* %57
%59 = call i32 @ntohs(i64 %58)
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%62 = load i64, i64* %61
%63 = call i32 @ntohs(i64 %62)
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %55, i8* %56, i32 %59, i8* %60, i32 %63)
br label %65
65: ; preds = %53, %15
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"socket %s: stdout\0A\00"
@.str.1 = [15 x i8] c"dump_addr self\00"
@.str.2 = [15 x i8] c"dump_addr peer\00"
@.str.3 = [5 x i8] c"none\00"
@.str.4 = [34 x i8] c"socket %s: self %s:%d peer %s:%d\0A\00"
define void @dump_addr(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
%10 = bitcast %struct.sockaddr_in* %4 to i8*
%11 = bitcast i32* %5 to i8*
store i32 16, i32* %5
%12 = bitcast %struct.sockaddr_in* %6 to i8*
%13 = bitcast i32* %7 to i8*
store i32 16, i32* %7
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%16 = icmp eq i32 %0, 1
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %55
20: ; preds = %3
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i64 @getsockname(i32 %0, %struct.sockaddr* %21, i32* %5)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @losing(i8* %1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%28 = load i32, i32* %27
%29 = call i8* @inet_ntoa(i32 %28)
%30 = call i32 @strcpy(i8* %14, i8* %29)
%31 = icmp eq i32 %2, 0
br i1 %31, label %43, label %32
32: ; preds = %26
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i64 @getpeername(i32 %0, %struct.sockaddr* %33, i32* %7)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @losing(i8* %1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%40 = load i32, i32* %39
%41 = call i8* @inet_ntoa(i32 %40)
%42 = call i32 @strcpy(i8* %15, i8* %41)
br label %46
43: ; preds = %26
%44 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i64 0, i64* %45
br label %46
46: ; preds = %43, %38
%47 = load i32, i32* @stderr
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%49 = load i64, i64* %48
%50 = call i32 @ntohs(i64 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%52 = load i64, i64* %51
%53 = call i32 @ntohs(i64 %52)
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %14, i32 %50, i8* %15, i32 %53)
br label %55
55: ; preds = %46, %17
ret void
}