#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*/ 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;
  int status, pid;
  if (argc == 1) {
    listen_fd = bindedSocket(NULL, "2023", SOCK_STREAM);
  } else
    listen_fd = bindedSocket(NULL, argv[1], SOCK_STREAM);
  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("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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e1 <main+0x41>
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    0xe2c(%rip),%rsi
call   10b0 <bindedSocket@plt>
mov    %eax,-0x14(%rbp)
jmp    11fe <main+0x5e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <bindedSocket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    122b <main+0x8b>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1230 <main+0x90>
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    1262 <main+0xc2>
lea    0xdb9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
jne    12a5 <main+0x105>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xd83(%rip),%rdi
call   1040 <printf@plt>
movslq -0x18(%rbp),%rdi
call   1070 <service@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1230 <main+0x90>

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
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    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xchg   %ax,%ax
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 = [33 x i8] c"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
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @bindedSocket(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
br label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @bindedSocket(i32* null, i8* %18, i32 %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %15, %12
%22 = load i32, i32* %6
%23 = call i64 @listen(i32 %22, i32 10)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %21
br label %29
29: ; preds = %28, %49
%30 = load i32, i32* %6
%31 = call i32 @accept(i32 %30, %struct.sockaddr* null, i32* null)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 2)
unreachable
37: ; preds = %29
%38 = call i64 (...) @fork()
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %37
%41 = call i32 (...) @getpid()
%42 = call i32 (...) @getppid()
%43 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = inttoptr i64 %45 to i8*
%47 = call i32 @service(i8* %46)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %37
br label %29
}

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 = [33 x i8] c"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)
%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 ([33 x i8], [33 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
}