#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
char *decode_type(unsigned int);
char *get_family_name(unsigned int);
char *get_proto_name(unsigned int, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned int *, int);
void open_sockets(char *cachefilename) {
  int cachefile;
  unsigned int family, type, protocol;
  unsigned int buffer[3];
  int bytesread = -1;
  unsigned int nr_sockets = 0;
  cachefile = open(cachefilename, O_RDONLY);
  if (cachefile < 0)
    return;
  while (bytesread != 0) {
    bytesread = read(cachefile, buffer, sizeof(int) * 3);
    if (bytesread == 0)
      break;
    family = buffer[0];
    type = buffer[1];
    protocol = buffer[2];
    printf("family:%s type:%s protocol:%s (%d)\n", get_family_name(family),
           decode_type(type), get_proto_name(family, protocol), protocol);
    nr_sockets++;
  }
  printf("%d entries in socket cachefile.\n", nr_sockets);
  close(cachefile);
}


x86 assembly (clang, -O0):
<open_sockets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ae <open_sockets+0x3e>
jmp    1259 <open_sockets+0xe9>
jmp    11b3 <open_sockets+0x43>
cmpl   $0x0,-0x28(%rbp)
je     1240 <open_sockets+0xd0>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11e0 <open_sockets+0x70>
jmp    1240 <open_sockets+0xd0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1080 <get_family_name@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <decode_type@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <get_proto_name@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
mov    -0x18(%rbp),%r8d
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b3 <open_sockets+0x43>
mov    -0x2c(%rbp),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_sockets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1236 <open_sockets+0xc6>
mov    %eax,%r12d
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <read@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1210 <open_sockets+0xa0>
xchg   %ax,%ax
mov    0xc(%rsp),%ebx
mov    0x10(%rsp),%r13d
mov    0x14(%rsp),%r14d
mov    %ebx,%edi
call   1080 <get_family_name@plt>
mov    %rax,%r15
mov    %r13d,%edi
call   1030 <decode_type@plt>
mov    %rax,%r13
mov    %ebx,%edi
mov    %r14d,%esi
call   1090 <get_proto_name@plt>
lea    0xe1c(%rip),%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %rax,%rcx
mov    %r14d,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
mov    %r12d,%edi
lea    0xc(%rsp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11b0 <open_sockets+0x40>
lea    0xe0d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"family:%s type:%s protocol:%s (%d)\0A\00"
@.str.1 = [33 x i8] c"%d entries in socket cachefile.\0A\00"
define void @open_sockets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [3 x i32]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %8
store i32 0, i32* %9
%10 = load i8*, i8** %2
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
br label %50
16: ; preds = %1
br label %17
17: ; preds = %27, %16
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %45
20: ; preds = %17
%21 = load i32, i32* %3
%22 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 0
%23 = call i32 @read(i32 %21, i32* %22, i32 12)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
br label %45
27: ; preds = %20
%28 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 0
%29 = load i32, i32* %28
store i32 %29, i32* %4
%30 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 1
%31 = load i32, i32* %30
store i32 %31, i32* %5
%32 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 2
%33 = load i32, i32* %32
store i32 %33, i32* %6
%34 = load i32, i32* %4
%35 = call i8* @get_family_name(i32 %34)
%36 = load i32, i32* %5
%37 = call i8* @decode_type(i32 %36)
%38 = load i32, i32* %4
%39 = load i32, i32* %6
%40 = call i8* @get_proto_name(i32 %38, i32 %39)
%41 = load i32, i32* %6
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %37, i8* %40, i32 %41)
%43 = load i32, i32* %9
%44 = add i32 %43, 1
store i32 %44, i32* %9
br label %17
45: ; preds = %26, %17
%46 = load i32, i32* %9
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %45, %15
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"family:%s type:%s protocol:%s (%d)\0A\00"
@.str.1 = [33 x i8] c"%d entries in socket cachefile.\0A\00"
define void @open_sockets(i8* nocapture %0) {
%2 = alloca [3 x i32]
%3 = bitcast [3 x i32]* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %30, label %7
7: ; preds = %1
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%9 = call i32 @read(i32 %5, i32* %8, i32 12)
%10 = icmp eq i32 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %7
%12 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 1
%13 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 2
br label %14
14: ; preds = %11, %14
%15 = phi i32 [ 0, %11 ], [ %23, %14 ]
%16 = load i32, i32* %8
%17 = load i32, i32* %12
%18 = load i32, i32* %13
%19 = tail call i8* @get_family_name(i32 %16)
%20 = tail call i8* @decode_type(i32 %17)
%21 = tail call i8* @get_proto_name(i32 %16, i32 %18)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20, i8* %21, i32 %18)
%23 = add i32 %15, 1
%24 = call i32 @read(i32 %5, i32* %8, i32 12)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %14
26: ; preds = %14, %7
%27 = phi i32 [ 0, %7 ], [ %23, %14 ]
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = tail call i32 @close(i32 %5)
br label %30
30: ; preds = %1, %26
ret void
}