#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bindedSocket(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                 int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ service(void *);
int main(int argc, char **argv) {
  int listen_fd, comm_fd;
  if (argc == 1) {
    listen_fd = bindedSocket(NULL, "2023", SOCK_STREAM, NULL);
  } else
    listen_fd = bindedSocket(NULL, argv[1], SOCK_STREAM, NULL);
  if (listen(listen_fd, 10) < 0) {
    perror("listen");
    exit(1);
  }
  while (1) {
    comm_fd = accept(listen_fd, (struct sockaddr *)NULL, NULL);
    if (comm_fd < 0) {
      perror("accept");
      exit(2);
    }
    if (fork() == 0) {
      printf("[INFO] un service fils pid=%d père=%d\n", getpid(), getppid());
      service((void *)(intptr_t)comm_fd);
      exit(0);
    }
  }
  close(listen_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e4 <main+0x44>
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xe2c(%rip),%rsi
mov    %rcx,%rdi
call   10b0 <bindedSocket@plt>
mov    %eax,-0x14(%rbp)
jmp    1204 <main+0x64>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdi
call   10b0 <bindedSocket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    1231 <main+0x91>
lea    0xde3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1236 <main+0x96>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1080 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1268 <main+0xc8>
lea    0xdb3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
jne    12ab <main+0x10b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xd7d(%rip),%rdi
call   1040 <printf@plt>
movslq -0x18(%rbp),%rdi
call   1070 <service@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1236 <main+0x96>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    11b1 <main+0x11>
lea    0xe51(%rip),%rsi
jmp    11b5 <main+0x15>
mov    0x8(%rsi),%rsi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
xor    %ecx,%ecx
call   10b0 <bindedSocket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
test   %rax,%rax
jns    11f0 <main+0x50>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <accept@plt>
test   %eax,%eax
js     123b <main+0x9b>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jne    11f0 <main+0x50>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xdef(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1070 <service@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdca(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"2023\00"
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [40 x i8] c"[INFO] un service fils pid=%d p\C3\A8re=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @bindedSocket(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11, i32* null)
store i32 %12, i32* %6
br label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @bindedSocket(i32* null, i8* %16, i32 %17, i32* null)
store i32 %18, i32* %6
br label %19
19: ; preds = %13, %10
%20 = load i32, i32* %6
%21 = call i64 @listen(i32 %20, i32 10)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
br label %27
27: ; preds = %26, %47
%28 = load i32, i32* %6
%29 = call i32 @accept(i32 %28, %struct.sockaddr* null, i32* null)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %27
%36 = call i64 (...) @fork()
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %47
38: ; preds = %35
%39 = call i32 (...) @getpid()
%40 = call i32 (...) @getppid()
%41 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %39, i32 %40)
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
%45 = call i32 @service(i8* %44)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %35
br label %27
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"2023\00"
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [40 x i8] c"[INFO] un service fils pid=%d p\C3\A8re=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %2, %4
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @bindedSocket(i32* null, i8* %8, i32 %9, i32* null)
%11 = tail call i64 @listen(i32 %10, i32 10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7, %22
%17 = tail call i32 @accept(i32 %10, %struct.sockaddr* null, i32* null)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %16
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %16
25: ; preds = %22
%26 = tail call i32 (...) @getpid()
%27 = tail call i32 (...) @getppid()
%28 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
%29 = zext i32 %17 to i64
%30 = inttoptr i64 %29 to i8*
%31 = tail call i32 @service(i8* %30)
%32 = tail call i32 @exit(i32 0)
unreachable
}