#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ chmod(char *, int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int cache_public_ip(struct sockaddr *addr) {
  if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
    return 0;
  FILE *f = fopen("/tmp/bind_public.ip", "w");
  if (!f) {
    int errsv = errno;
    fprintf(stderr, "bind_public: cache_public_ip: fopen(%s) errno = %d\n",
            "/tmp/bind_public.ip", errsv);
    return errsv;
  }
  switch (addr->sa_family) {
  case AF_INET:
    fwrite(&((struct sockaddr_in *)addr)->sin_addr, 1,
           sizeof(((struct sockaddr_in *)addr)->sin_addr), f);
    break;
  case AF_INET6:
    fwrite(&((struct sockaddr_in6 *)addr)->sin6_addr, 1,
           sizeof(((struct sockaddr_in6 *)addr)->sin6_addr), f);
    break;
  default:
    break;
  }
  fclose(f);
  chmod("/tmp/bind_public.ip", 0666);
  return 0;
}


x86 assembly (clang, -O0):
<cache_public_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x81,(%rax)
je     1188 <cache_public_ip+0x38>
mov    -0x10(%rbp),%rax
cmpl   $0x80,(%rax)
je     1188 <cache_public_ip+0x38>
movl   $0x0,-0x4(%rbp)
jmp    126a <cache_public_ip+0x11a>
lea    0xe71(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e0 <cache_public_ip+0x90>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%ecx
lea    0xe4d(%rip),%rsi
lea    0xe30(%rip),%rdx
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <cache_public_ip+0x11a>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     1228 <cache_public_ip+0xd8>
jmp    11f9 <cache_public_ip+0xa9>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
jne    1244 <cache_public_ip+0xf4>
jmp    120c <cache_public_ip+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
jmp    1249 <cache_public_ip+0xf9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
jmp    1249 <cache_public_ip+0xf9>
jmp    1249 <cache_public_ip+0xf9>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xda7(%rip),%rdi
mov    $0x1b6,%esi
call   1050 <chmod@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_public_ip>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%eax
and    $0xfffffffe,%eax
xor    %r14d,%r14d
cmp    $0x80,%eax
jne    11ea <cache_public_ip+0x9a>
mov    %rdi,%rbx
lea    0xe8f(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c1 <cache_public_ip+0x71>
mov    %rax,%rbp
mov    (%rbx),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    11a6 <cache_public_ip+0x56>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe4b(%rip),%rdi
mov    $0x1b6,%esi
call   1050 <chmod@plt>
jmp    11ea <cache_public_ip+0x9a>
mov    0x2e10(%rip),%rax
mov    (%rax),%r14d
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
lea    0xe1e(%rip),%rdx
mov    %r14d,%ecx
call   1040 <fprintf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [20 x i8] c"/tmp/bind_public.ip\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [52 x i8] c"bind_public: cache_public_ip: fopen(%s) errno = %d\0A\00"
define i32 @cache_public_ip(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr*
%4 = alloca i32*
%5 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %3
%6 = load %struct.sockaddr*, %struct.sockaddr** %3
%7 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = icmp ne i32 %8, 129
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load %struct.sockaddr*, %struct.sockaddr** %3
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, 128
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 0, i32* %2
br label %47
16: ; preds = %10, %1
%17 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = load i32, i32* @errno
store i32 %21, i32* %5
%22 = load i32, i32* @stderr
%23 = load i32, i32* %5
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %5
store i32 %25, i32* %2
br label %47
26: ; preds = %16
%27 = load %struct.sockaddr*, %struct.sockaddr** %3
%28 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %27, i32 0, i32 0
%29 = load i32, i32* %28
switch i32 %29, label %42 [
i32 129, label %30
i32 128, label %36
]
30: ; preds = %26
%31 = load %struct.sockaddr*, %struct.sockaddr** %3
%32 = bitcast %struct.sockaddr* %31 to %struct.sockaddr_in*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 0
%34 = load i32*, i32** %4
%35 = call i32 @fwrite(i32* %33, i32 1, i32 4, i32* %34)
br label %43
36: ; preds = %26
%37 = load %struct.sockaddr*, %struct.sockaddr** %3
%38 = bitcast %struct.sockaddr* %37 to %struct.sockaddr_in6*
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %38, i32 0, i32 0
%40 = load i32*, i32** %4
%41 = call i32 @fwrite(i32* %39, i32 1, i32 4, i32* %40)
br label %43
42: ; preds = %26
br label %43
43: ; preds = %42, %36, %30
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
%46 = call i32 @chmod(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 438)
store i32 0, i32* %2
br label %47
47: ; preds = %43, %20, %15
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"/tmp/bind_public.ip\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [52 x i8] c"bind_public: cache_public_ip: fopen(%s) errno = %d\0A\00"
define i32 @cache_public_ip(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = and i32 %3, -2
%5 = icmp eq i32 %4, 128
br i1 %5, label %6, label %22
6: ; preds = %1
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %22
13: ; preds = %6
%14 = load i32, i32* %2
%15 = and i32 %14, -2
%16 = icmp eq i32 %15, 128
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @fwrite(i32* %2, i32 1, i32 4, i32* %7)
br label %19
19: ; preds = %13, %17
%20 = tail call i32 @fclose(i32* %7)
%21 = tail call i32 @chmod(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 438)
br label %22
22: ; preds = %1, %9, %19
%23 = phi i32 [ 0, %1 ], [ 0, %19 ], [ %10, %9 ]
ret i32 %23
}