#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 /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sqlite3;
typedef int /*<<< orphan*/ server_addr;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ SQLITE_OK;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_client(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
scalar_t__ sqlite3_open(char *, int /*<<< orphan*/ **);
int main(int argc, char *argv[]) {
  int listenfd, connectfd;
  struct sockaddr_in server_addr;
  pid_t pid;
  sqlite3 *db;
  if (argc < 3) {
    printf("Usage : %s <ip> <port>\n", argv[0]);
    exit(-1);
  }
  if (sqlite3_open("my.db", &db) != SQLITE_OK) {
    printf("error : %s\n", sqlite3_errmsg(db));
    exit(-1);
  }
  if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("fail to socket");
    exit(-1);
  }
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = PF_INET;
  server_addr.sin_addr.s_addr = inet_addr(argv[1]);
  server_addr.sin_port = htons(atoi(argv[2]));
  if (bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
      0) {
    perror("fail to bind");
    exit(-1);
  }
  if (listen(listenfd, 5) < 0) {
    perror("fail to listen");
    exit(-1);
  }
  signal(SIGCHLD, SIG_IGN);
  while (1) {
    if ((connectfd = accept(listenfd, NULL, NULL)) < 0) {
      perror("fail to accept");
      exit(-1);
    }
    if ((pid = fork()) < 0) {
      perror("fail to fork");
      exit(-1);
    }
    if (pid == 0) {
      do_client(connectfd, db);
    }
    close(connectfd);
  }
  return 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)
cmpl   $0x3,-0x8(%rbp)
jge    124d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdc2(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
lea    0xdc4(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
je     128f <main+0x7f>
mov    -0x38(%rbp),%rdi
call   1080 <sqlite3_errmsg@plt>
mov    %rax,%rsi
lea    0xd9e(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12ca <main+0xba>
lea    0xd6f(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    133c <main+0x12c>
lea    0xd0c(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
jge    1369 <main+0x159>
lea    0xcec(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
mov    0x2c5f(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10f0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    13b1 <main+0x1a1>
lea    0xcb3(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    13dc <main+0x1cc>
lea    0xc97(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    13f3 <main+0x1e3>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1030 <do_client@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    1380 <main+0x170>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jg     122e <main+0x1e>
mov    (%r14),%rsi
lea    0xdd4(%rip),%rdi
jmp    1286 <main+0x76>
lea    0xde3(%rip),%rdi
mov    %rsp,%rsi
call   1040 <sqlite3_open@plt>
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <main+0x63>
mov    0x2d70(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
test   %eax,%eax
jns    1295 <main+0x85>
lea    0xdbc(%rip),%rdi
jmp    137a <main+0x16a>
mov    (%rsp),%rdi
call   1080 <sqlite3_errmsg@plt>
lea    0xd9b(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%r14),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    0x10(%r14),%rdi
call   1100 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    12f1 <main+0xe1>
lea    0xd4d(%rip),%rdi
jmp    137a <main+0x16a>
mov    %ebx,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
test   %rax,%rax
jns    130b <main+0xfb>
lea    0xd3d(%rip),%rdi
jmp    137a <main+0x16a>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10f0 <accept@plt>
test   %eax,%eax
jns    133a <main+0x12a>
lea    0xd1d(%rip),%rdi
jmp    137a <main+0x16a>
mov    %eax,%ebp
jmp    1358 <main+0x148>
xchg   %ax,%ax
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10f0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1331 <main+0x121>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     1373 <main+0x163>
jne    1340 <main+0x130>
mov    (%rsp),%rsi
mov    %ebp,%edi
call   1030 <do_client@plt>
jmp    1340 <main+0x130>
lea    0xcea(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage : %s <ip> <port>\0A\00"
@.str.1 = [6 x i8] c"my.db\00"
@SQLITE_OK = global i64 0
@.str.2 = [12 x i8] c"error : %s\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [15 x i8] c"fail to socket\00"
@.str.4 = [13 x i8] c"fail to bind\00"
@.str.5 = [15 x i8] c"fail to listen\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.6 = [15 x i8] c"fail to accept\00"
@.str.7 = [13 x i8] c"fail to 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 %struct.sockaddr_in
%9 = alloca i64
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = call i64 @sqlite3_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32** %10)
%21 = load i64, i64* @SQLITE_OK
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32*, i32** %10
%25 = call i8* @sqlite3_errmsg(i32* %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %19
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %28
%37 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%38 = load i32, i32* @PF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @inet_addr(i8* %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%45 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %44, i32 0, i32 0
store i32 %43, i32* %45
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = call i32 @atoi(i8* %48)
%50 = call i32 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %6
%53 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%54 = call i64 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %36
%57 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 -1)
unreachable
59: ; preds = %36
%60 = load i32, i32* %6
%61 = call i64 @listen(i32 %60, i32 5)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %59
%67 = load i32, i32* @SIGCHLD
%68 = load i32, i32* @SIG_IGN
%69 = call i32 @signal(i32 %67, i32 %68)
br label %70
70: ; preds = %66, %90
%71 = load i32, i32* %6
%72 = call i32 @accept(i32 %71, i32* null, i32* null)
store i32 %72, i32* %7
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %70
%75 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 @exit(i32 -1)
unreachable
77: ; preds = %70
%78 = call i64 (...) @fork()
store i64 %78, i64* %9
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%82 = call i32 @exit(i32 -1)
unreachable
83: ; preds = %77
%84 = load i64, i64* %9
%85 = icmp eq i64 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = load i32, i32* %7
%88 = load i32*, i32** %10
%89 = call i32 @do_client(i32 %87, i32* %88)
br label %90
90: ; preds = %86, %83
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
br label %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage : %s <ip> <port>\0A\00"
@.str.1 = [6 x i8] c"my.db\00"
@SQLITE_OK = global i64 0
@.str.2 = [12 x i8] c"error : %s\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [15 x i8] c"fail to socket\00"
@.str.4 = [13 x i8] c"fail to bind\00"
@.str.5 = [15 x i8] c"fail to listen\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.6 = [15 x i8] c"fail to accept\00"
@.str.7 = [13 x i8] c"fail to fork\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32** %4 to i8*
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = call i64 @sqlite3_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32** %4)
%14 = load i64, i64* @SQLITE_OK
%15 = icmp eq i64 %13, %14
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32*, i32** %4
%18 = call i8* @sqlite3_errmsg(i32* %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %12
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %21
%30 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%31 = load i32, i32* @PF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %31, i32* %32
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @inet_addr(i8* %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %35, i32* %36
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %40, i32* %41
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @bind(i32 %24, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %29
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %29
%49 = call i64 @listen(i32 %24, i32 5)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %48
%55 = load i32, i32* @SIGCHLD
%56 = load i32, i32* @SIG_IGN
%57 = call i32 @signal(i32 %55, i32 %56)
%58 = call i32 @accept(i32 %24, i32* null, i32* null)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %75, %54
%61 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%62 = call i32 @exit(i32 -1)
unreachable
63: ; preds = %54, %75
%64 = phi i32 [ %77, %75 ], [ %58, %54 ]
%65 = call i64 (...) @fork()
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %63
%71 = icmp eq i64 %65, 0
br i1 %71, label %72, label %75
72: ; preds = %70
%73 = load i32*, i32** %4
%74 = call i32 @do_client(i32 %64, i32* %73)
br label %75
75: ; preds = %72, %70
%76 = call i32 @close(i32 %64)
%77 = call i32 @accept(i32 %24, i32* null, i32* null)
%78 = icmp slt i32 %77, 0
br i1 %78, label %60, label %63
}