#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*/ disconnectfunction_exec(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ log_close();
int /*<<< orphan*/ log_print(char *, char const *);
int mainsocket_get();
int /*<<< orphan*/ network_close(int);
int socketlist_get_next();
int /*<<< orphan*/ socketlist_remove(int);
void main_shutdown(const char *errmsg) {
  int i;
  log_print("Shutting down. Reason: %s", errmsg);
  while ((i = socketlist_get_next()) > 0) {
    network_close(i);
    socketlist_remove(i);
    if (i != mainsocket_get())
      disconnectfunction_exec(i);
  }
  log_close();
  exit(1);
}


x86 assembly (clang, -O0):
<main_shutdown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe69(%rip),%rdi
call   1060 <log_print@plt>
mov    $0x0,%al
call   1050 <socketlist_get_next@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11e6 <main_shutdown+0x66>
mov    -0xc(%rbp),%edi
call   1030 <network_close@plt>
mov    -0xc(%rbp),%edi
call   1090 <socketlist_remove@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <mainsocket_get@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
cmp    %ecx,%eax
je     11e1 <main_shutdown+0x61>
mov    -0xc(%rbp),%edi
call   1070 <disconnectfunction_exec@plt>
jmp    119c <main_shutdown+0x1c>
mov    $0x0,%al
call   1080 <log_close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main_shutdown>:
push   %rbx
mov    %rdi,%rsi
lea    0xe75(%rip),%rdi
call   1060 <log_print@plt>
xor    %eax,%eax
call   1050 <socketlist_get_next@plt>
test   %eax,%eax
jle    11cf <main_shutdown+0x4f>
mov    %eax,%ebx
jmp    11ad <main_shutdown+0x2d>
nop
xor    %eax,%eax
call   1050 <socketlist_get_next@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11cf <main_shutdown+0x4f>
mov    %ebx,%edi
call   1030 <network_close@plt>
mov    %ebx,%edi
call   1090 <socketlist_remove@plt>
xor    %eax,%eax
call   1040 <mainsocket_get@plt>
cmp    %eax,%ebx
je     11a0 <main_shutdown+0x20>
mov    %ebx,%edi
call   1070 <disconnectfunction_exec@plt>
jmp    11a0 <main_shutdown+0x20>
xor    %eax,%eax
call   1080 <log_close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Shutting down. Reason: %s\00"
define void @main_shutdown(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @log_print(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %6
6: ; preds = %20, %1
%7 = call i32 (...) @socketlist_get_next()
store i32 %7, i32* %3
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %6
%10 = load i32, i32* %3
%11 = call i32 @network_close(i32 %10)
%12 = load i32, i32* %3
%13 = call i32 @socketlist_remove(i32 %12)
%14 = load i32, i32* %3
%15 = call i32 (...) @mainsocket_get()
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i32, i32* %3
%19 = call i32 @disconnectfunction_exec(i32 %18)
br label %20
20: ; preds = %17, %9
br label %6
21: ; preds = %6
%22 = call i32 (...) @log_close()
%23 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [26 x i8] c"Shutting down. Reason: %s\00"
define void @main_shutdown(i8* %0) {
%2 = tail call i32 @log_print(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32 (...) @socketlist_get_next()
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %1, %13
%6 = phi i32 [ %14, %13 ], [ %3, %1 ]
%7 = tail call i32 @network_close(i32 %6)
%8 = tail call i32 @socketlist_remove(i32 %6)
%9 = tail call i32 (...) @mainsocket_get()
%10 = icmp eq i32 %6, %9
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = tail call i32 @disconnectfunction_exec(i32 %6)
br label %13
13: ; preds = %11, %5
%14 = tail call i32 (...) @socketlist_get_next()
%15 = icmp sgt i32 %14, 0
br i1 %15, label %5, label %16
16: ; preds = %13, %1
%17 = tail call i32 (...) @log_close()
%18 = tail call i32 @exit(i32 1)
unreachable
}