#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ myaddr_in;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EOF;
int /*<<< orphan*/ INADDR_ANY;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ _exit(int);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int fork();
int getopt(int, char **, char *);
int /*<<< orphan*/ memset(void *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ serve(char *, int, int);
int /*<<< orphan*/ setsid();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int s;
  int i1;
  int noFork = 0;
  int pid;
  int logfd = 1;
  struct sockaddr_in myaddr_in;
  extern char *optarg;
  extern int optind, optopt;
  while (EOF != (i1 = getopt(argc, argv, "fl:"))) {
    switch (i1) {
    case 'f':
      noFork = 1;
      break;
    case 'l':
      if ((logfd = open(optarg, O_WRONLY | O_APPEND | O_CREAT, 0644)) < 0) {
        (void)perror(optarg);
        (void)exit(1);
      }
      break;
    }
  }
  (void)memset((void *)&myaddr_in, (int)'\0', sizeof(myaddr_in));
  myaddr_in.sin_family = AF_INET;
  myaddr_in.sin_addr.s_addr = INADDR_ANY;
  myaddr_in.sin_port = 4444;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    (void)perror("socket");
    (void)exit(1);
  }
  if (bind(s, (struct sockaddr *)&myaddr_in, sizeof(myaddr_in)) == -1) {
    (void)perror("bind");
    (void)exit(1);
  }
  (void)setsid();
  if (noFork) {
    pid = 0;
  } else {
    pid = fork();
  }
  switch (pid) {
  case -1:
    (void)perror("fork");
    (void)_exit(1);
    break;
  case 0:
    (void)serve(argv[0], s, logfd);
    break;
  default:
    (void)exit(0);
  }
  (void)exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x24(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe12(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
mov    %ecx,-0x18(%rbp)
cmp    %ecx,%eax
je     1292 <main+0xe2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x66,%eax
je     1228 <main+0x78>
jmp    1217 <main+0x67>
mov    -0x38(%rbp),%eax
sub    $0x6c,%eax
je     1234 <main+0x84>
jmp    128d <main+0xdd>
movl   $0x1,-0x1c(%rbp)
jmp    128d <main+0xdd>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rdi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    0x2d52(%rip),%rax
or     (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1288 <main+0xd8>
mov    0x2d42(%rip),%rax
mov    (%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    128d <main+0xdd>
jmp    11d4 <main+0x24>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x115c,-0x30(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12fd <main+0x14d>
lea    0xd16(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    132d <main+0x17d>
lea    0xced(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmpl   $0x0,-0x1c(%rbp)
je     134a <main+0x19a>
movl   $0x0,-0x20(%rbp)
jmp    1354 <main+0x1a4>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0xffffffff,%eax
je     1378 <main+0x1c8>
jmp    1368 <main+0x1b8>
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     138e <main+0x1de>
jmp    13a5 <main+0x1f5>
lea    0xc91(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1060 <serve@plt>
jmp    13ac <main+0x1fc>
xor    %edi,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e06(%rip),%r13
mov    0x0(%r13),%ebp
lea    0xe2b(%rip),%rdx
call   10a0 <getopt@plt>
cmp    %eax,%ebp
jne    1242 <main+0x92>
mov    $0x1,%ebp
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x115c,0x8(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    12c7 <main+0x117>
lea    0xdc7(%rip),%rdi
jmp    1339 <main+0x189>
movl   $0x0,0x4(%rsp)
mov    $0x1,%ebp
lea    0xdaa(%rip),%r15
jmp    12b3 <main+0x103>
nopl   0x0(%rax,%rax,1)
mov    0x2d51(%rip),%rax
mov    (%rax),%rdi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    0x2d2e(%rip),%rax
or     (%rax),%esi
mov    0x2d75(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     132f <main+0x17f>
mov    0x0(%r13),%r12d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <getopt@plt>
cmp    %eax,%r12d
je     11eb <main+0x3b>
cmp    $0x6c,%eax
je     1260 <main+0xb0>
cmp    $0x66,%eax
jne    1299 <main+0xe9>
movl   $0x1,0x4(%rsp)
jmp    1299 <main+0xe9>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    12e8 <main+0x138>
lea    0xd25(%rip),%rdi
jmp    1339 <main+0x189>
xor    %eax,%eax
call   1050 <setsid@plt>
cmpl   $0x0,0x4(%rsp)
jne    131c <main+0x16c>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     131c <main+0x16c>
cmp    $0xffffffff,%eax
jne    1328 <main+0x178>
lea    0xd03(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    (%r14),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <serve@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@EOF = global i32 0
@.str = [4 x i8] c"fl:\00"
@optarg = external global i8*
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [5 x i8] c"fork\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
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 1, i32* %10
br label %12
12: ; preds = %35, %2
%13 = load i32, i32* @EOF
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @getopt(i32 %14, i8** %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %7
%17 = icmp ne i32 %13, %16
br i1 %17, label %18, label %36
18: ; preds = %12
%19 = load i32, i32* %7
switch i32 %19, label %35 [
i32 102, label %20
i32 108, label %21
]
20: ; preds = %18
store i32 1, i32* %8
br label %35
21: ; preds = %18
%22 = load i8*, i8** @optarg
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_APPEND
%25 = or i32 %23, %24
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %22, i32 %27, i32 420)
store i32 %28, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i8*, i8** @optarg
%32 = call i32 @perror(i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
br label %35
35: ; preds = %18, %34, %20
br label %12
36: ; preds = %12
%37 = bitcast %struct.sockaddr_in* %11 to i8*
%38 = call i32 @memset(i8* %37, i32 0, i32 12)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %39, i32* %40
%41 = load i32, i32* @INADDR_ANY
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %41, i32* %43
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 4444, i32* %44
%45 = load i32, i32* @AF_INET
%46 = load i32, i32* @SOCK_DGRAM
%47 = call i32 @socket(i32 %45, i32 %46, i32 0)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %36
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %36
%54 = load i32, i32* %6
%55 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%56 = call i32 @bind(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %53
%62 = call i32 (...) @setsid()
%63 = load i32, i32* %8
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61
store i32 0, i32* %9
br label %68
66: ; preds = %61
%67 = call i32 (...) @fork()
store i32 %67, i32* %9
br label %68
68: ; preds = %66, %65
%69 = load i32, i32* %9
switch i32 %69, label %80 [
i32 -1, label %70
i32 0, label %73
]
70: ; preds = %68
%71 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%72 = call i32 @_exit(i32 1)
unreachable
73: ; preds = %68
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 0
%76 = load i8*, i8** %75
%77 = load i32, i32* %6
%78 = load i32, i32* %10
%79 = call i32 @serve(i8* %76, i32 %77, i32 %78)
br label %82
80: ; preds = %68
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %73
%83 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@EOF = global i32 0
@.str = [4 x i8] c"fl:\00"
@optarg = external global i8*
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @EOF
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32 %5, %6
br i1 %7, label %32, label %8
8: ; preds = %2, %26
%9 = phi i32 [ %30, %26 ], [ %6, %2 ]
%10 = phi i32 [ %28, %26 ], [ 1, %2 ]
%11 = phi i32 [ %27, %26 ], [ 0, %2 ]
switch i32 %9, label %26 [
i32 102, label %12
i32 108, label %13
]
12: ; preds = %8
br label %26
13: ; preds = %8
%14 = load i8*, i8** @optarg
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_APPEND
%17 = or i32 %16, %15
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %14, i32 %19, i32 420)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = load i8*, i8** @optarg
%24 = tail call i32 @perror(i8* %23)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %13, %8, %12
%27 = phi i32 [ %11, %8 ], [ %11, %13 ], [ 1, %12 ]
%28 = phi i32 [ %10, %8 ], [ %20, %13 ], [ %10, %12 ]
%29 = load i32, i32* @EOF
%30 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %8
32: ; preds = %26, %2
%33 = phi i32 [ 0, %2 ], [ %27, %26 ]
%34 = phi i32 [ 1, %2 ], [ %28, %26 ]
%35 = call i32 @memset(i8* %4, i32 0, i32 12)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 4444, i32* %40
%41 = load i32, i32* @SOCK_DGRAM
%42 = call i32 @socket(i32 %36, i32 %41, i32 0)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %32
%48 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%49 = call i32 @bind(i32 %42, %struct.sockaddr* %48, i32 12)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %47
%55 = call i32 (...) @setsid()
%56 = icmp eq i32 %33, 0
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = call i32 (...) @fork()
switch i32 %58, label %66 [
i32 -1, label %59
i32 0, label %62
]
59: ; preds = %57
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @_exit(i32 1)
unreachable
62: ; preds = %54, %57
%63 = load i8*, i8** %1
%64 = call i32 @serve(i8* %63, i32 %42, i32 %34)
%65 = call i32 @exit(i32 0)
unreachable
66: ; preds = %57
%67 = call i32 @exit(i32 0)
unreachable
}