#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 accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ do_work(void *);
int /*<<< orphan*/ htons(int);
int listen_new(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
void main_loop() {
  int listen_fd = listen_new(0, htons(1234));
  int connfd = -1;
  if (listen_fd == -1) {
    printf("listen error\n");
    return;
  }
  printf("listen port = %d\n", 1234);
  for (;;) {
    connfd = accept(listen_fd, NULL, NULL);
    printf("connfd = %d\n", connfd);
    do_work((void *)(intptr_t)connfd);
  }
}


x86 assembly (clang, -O0):
<main_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x4d2,%edi
call   1030 <htons@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1050 <listen_new@plt>
mov    %eax,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1193 <main_loop+0x43>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret
lea    0xe74(%rip),%rdi
mov    $0x4d2,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x8(%rbp),%rdi
call   1060 <do_work@plt>
jmp    11a6 <main_loop+0x56>

x86 assembly (clang, -O2):
<main_loop>:
push   %rbp
push   %r14
push   %rbx
mov    $0x4d2,%edi
call   1040 <htons@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1060 <listen_new@plt>
cmp    $0xffffffff,%eax
je     11c3 <main_loop+0x63>
mov    %eax,%ebx
lea    0xe7b(%rip),%rdi
mov    $0x4d2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7a(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <accept@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebp,%rdi
call   1070 <do_work@plt>
jmp    11a0 <main_loop+0x40>
lea    0xe55(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"listen error\0A\00"
@.str.1 = [18 x i8] c"listen port = %d\0A\00"
@.str.2 = [13 x i8] c"connfd = %d\0A\00"
define void @main_loop() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @htons(i32 1234)
%4 = call i32 @listen_new(i32 0, i32 %3)
store i32 %4, i32* %1
store i32 -1, i32* %2
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
ret void
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 1234)
br label %11
11: ; preds = %11, %9
%12 = load i32, i32* %1
%13 = call i32 @accept(i32 %12, i32* null, i32* null)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i8*
%19 = call i32 @do_work(i8* %18)
br label %11
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"listen port = %d\0A\00"
@.str.2 = [13 x i8] c"connfd = %d\0A\00"
@str = [13 x i8] c"listen error\00"
define void @main_loop() {
%1 = tail call i32 @htons(i32 1234)
%2 = tail call i32 @listen_new(i32 0, i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
ret void
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 1234)
br label %8
8: ; preds = %8, %6
%9 = tail call i32 @accept(i32 %2, i32* null, i32* null)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = sext i32 %9 to i64
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @do_work(i8* %12)
br label %8
}