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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ MSG_PEEK;
int /*<<< orphan*/ PF_INET;
int PF_UNSPEC;
int SOCK_DGRAM;
int SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int dsm_server_accept(int s, int type, int port, int broadcast,
                      struct sockaddr_in *gaddr, int *msglen, char **buf,
                      int maxlen) {
  int s2;
  struct hostent *hp;
  int prot, len, on = 1;
  struct linger l;
  struct sockaddr_in laddr, *saddr;
  if (gaddr == NULL) {
    memset(&laddr, 0, sizeof(struct sockaddr_in));
    laddr.sin_family = PF_INET;
    laddr.sin_addr.s_addr = htonl(INADDR_ANY);
    laddr.sin_port = htons(port);
    saddr = &laddr;
  } else
    saddr = gaddr;
  prot = (type == SOCK_DGRAM ? 0 : PF_UNSPEC);
  *msglen = 0;
  if (type == SOCK_DGRAM) {
    len = sizeof(struct sockaddr_in);
    while ((*msglen = recvfrom(s, *buf, maxlen, MSG_PEEK,
                               (struct sockaddr *)saddr, &len)) == -1)
      if (errno != EINTR) {
        *msglen = 0;
        perror("recvfrom error");
        return (0);
      }
  } else if (type == SOCK_STREAM) {
    *buf = NULL;
    len = sizeof(struct sockaddr_in);
    while ((s2 = accept(s, (struct sockaddr *)saddr, &len)) < 0)
      if (errno != EINTR) {
        return (0);
      }
    return (s2);
  }
  return (s);
}


x86 assembly (clang, -O0):
<dsm_server_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
movl   $0x1,-0x44(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11da <dsm_server_accept+0x7a>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x58(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    11e2 <dsm_server_accept+0x82>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%eax
jne    11fe <dsm_server_accept+0x9e>
xor    %eax,%eax
mov    %eax,-0x64(%rbp)
jmp    120a <dsm_server_accept+0xaa>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0xc(%rbp),%eax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%eax
jne    12ab <dsm_server_accept+0x14b>
movl   $0xc,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x18(%rbp),%edx
mov    0x2d91(%rip),%rax
mov    (%rax),%ecx
mov    -0x60(%rbp),%r8
lea    -0x40(%rbp),%r9
call   1060 <recvfrom@plt>
mov    -0x28(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    12a6 <dsm_server_accept+0x146>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
je     12a1 <dsm_server_accept+0x141>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
lea    0xd70(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    132c <dsm_server_accept+0x1cc>
jmp    1233 <dsm_server_accept+0xd3>
jmp    1326 <dsm_server_accept+0x1c6>
mov    -0xc(%rbp),%eax
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%eax
jne    1321 <dsm_server_accept+0x1c1>
mov    0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xc,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x60(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1316 <dsm_server_accept+0x1b6>
mov    0x2cc6(%rip),%rax
mov    (%rax),%rax
mov    0x2cac(%rip),%rcx
cmp    (%rcx),%rax
je     1311 <dsm_server_accept+0x1b1>
movl   $0x0,-0x4(%rbp)
jmp    132c <dsm_server_accept+0x1cc>
jmp    12cf <dsm_server_accept+0x16f>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132c <dsm_server_accept+0x1cc>
jmp    1326 <dsm_server_accept+0x1c6>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsm_server_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%rbp
mov    %r8,%rbx
mov    %esi,%r15d
mov    %edi,%r14d
test   %r8,%r8
jne    11c1 <dsm_server_accept+0x61>
mov    %edx,%r12d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
movl   $0x0,0x0(%rbp)
mov    0x2e09(%rip),%rax
cmp    %r15d,(%rax)
jne    1239 <dsm_server_accept+0xd9>
movl   $0xc,0x4(%rsp)
lea    0x4(%rsp),%r12
mov    0x2dd8(%rip),%r15
mov    0x2dc1(%rip),%r13
nop
mov    0x50(%rsp),%rax
mov    (%rax),%rsi
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    %r14d,%edi
mov    0x58(%rsp),%edx
mov    %rbx,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
mov    %eax,0x0(%rbp)
cmp    $0xffffffff,%eax
jne    1293 <dsm_server_accept+0x133>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
je     11f0 <dsm_server_accept+0x90>
movl   $0x0,0x0(%rbp)
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
jmp    128b <dsm_server_accept+0x12b>
mov    0x2d88(%rip),%rax
cmp    %r15d,(%rax)
jne    1293 <dsm_server_accept+0x133>
mov    0x50(%rsp),%rax
movq   $0x0,(%rax)
movl   $0xc,0x4(%rsp)
lea    0x4(%rsp),%r12
mov    0x2d5b(%rip),%r15
mov    0x2d44(%rip),%rbp
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <accept@plt>
test   %eax,%eax
jns    1290 <dsm_server_accept+0x130>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
je     1270 <dsm_server_accept+0x110>
xor    %r14d,%r14d
jmp    1293 <dsm_server_accept+0x133>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.linger = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@PF_UNSPEC = global i32 0
@MSG_PEEK = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"recvfrom error\00"
@SOCK_STREAM = global i32 0
define i32 @dsm_server_accept(i32 %0, i32 %1, i32 %2, i32 %3, %struct.sockaddr_in* %4, i32* %5, i8** %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in*
%15 = alloca i32*
%16 = alloca i8**
%17 = alloca i32
%18 = alloca i32
%19 = alloca %struct.hostent*
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca %struct.linger
%24 = alloca %struct.sockaddr_in
%25 = alloca %struct.sockaddr_in*
store i32 %0, i32* %10
store i32 %1, i32* %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store %struct.sockaddr_in* %4, %struct.sockaddr_in** %14
store i32* %5, i32** %15
store i8** %6, i8*** %16
store i32 %7, i32* %17
store i32 1, i32* %22
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %14
%27 = icmp eq %struct.sockaddr_in* %26, null
br i1 %27, label %28, label %39
28: ; preds = %8
%29 = call i32 @memset(%struct.sockaddr_in* %24, i32 0, i32 12)
%30 = load i32, i32* @PF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* %12
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 0
store i32 %37, i32* %38
store %struct.sockaddr_in* %24, %struct.sockaddr_in** %25
br label %41
39: ; preds = %8
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %14
store %struct.sockaddr_in* %40, %struct.sockaddr_in** %25
br label %41
41: ; preds = %39, %28
%42 = load i32, i32* %11
%43 = load i32, i32* @SOCK_DGRAM
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
br label %48
46: ; preds = %41
%47 = load i32, i32* @PF_UNSPEC
br label %48
48: ; preds = %46, %45
%49 = phi i32 [ 0, %45 ], [ %47, %46 ]
store i32 %49, i32* %20
%50 = load i32*, i32** %15
store i32 0, i32* %50
%51 = load i32, i32* %11
%52 = load i32, i32* @SOCK_DGRAM
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %75
54: ; preds = %48
store i32 12, i32* %21
br label %55
55: ; preds = %73, %54
%56 = load i32, i32* %10
%57 = load i8**, i8*** %16
%58 = load i8*, i8** %57
%59 = load i32, i32* %17
%60 = load i32, i32* @MSG_PEEK
%61 = load %struct.sockaddr_in*, %struct.sockaddr_in** %25
%62 = bitcast %struct.sockaddr_in* %61 to %struct.sockaddr*
%63 = call i32 @recvfrom(i32 %56, i8* %58, i32 %59, i32 %60, %struct.sockaddr* %62, i32* %21)
%64 = load i32*, i32** %15
store i32 %63, i32* %64
%65 = icmp eq i32 %63, -1
br i1 %65, label %66, label %74
66: ; preds = %55
%67 = load i64, i64* @errno
%68 = load i64, i64* @EINTR
%69 = icmp ne i64 %67, %68
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = load i32*, i32** %15
store i32 0, i32* %71
%72 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %9
br label %98
73: ; preds = %66
br label %55
74: ; preds = %55
br label %96
75: ; preds = %48
%76 = load i32, i32* %11
%77 = load i32, i32* @SOCK_STREAM
%78 = icmp eq i32 %76, %77
br i1 %78, label %79, label %95
79: ; preds = %75
%80 = load i8**, i8*** %16
store i8* null, i8** %80
store i32 12, i32* %21
br label %81
81: ; preds = %92, %79
%82 = load i32, i32* %10
%83 = load %struct.sockaddr_in*, %struct.sockaddr_in** %25
%84 = bitcast %struct.sockaddr_in* %83 to %struct.sockaddr*
%85 = call i32 @accept(i32 %82, %struct.sockaddr* %84, i32* %21)
store i32 %85, i32* %18
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %93
87: ; preds = %81
%88 = load i64, i64* @errno
%89 = load i64, i64* @EINTR
%90 = icmp ne i64 %88, %89
br i1 %90, label %91, label %92
91: ; preds = %87
store i32 0, i32* %9
br label %98
92: ; preds = %87
br label %81
93: ; preds = %81
%94 = load i32, i32* %18
store i32 %94, i32* %9
br label %98
95: ; preds = %75
br label %96
96: ; preds = %95, %74
%97 = load i32, i32* %10
store i32 %97, i32* %9
br label %98
98: ; preds = %96, %93, %91, %70
%99 = load i32, i32* %9
ret i32 %99
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@PF_UNSPEC = global i32 0
@MSG_PEEK = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"recvfrom error\00"
@SOCK_STREAM = global i32 0
define i32 @dsm_server_accept(i32 %0, i32 %1, i32 %2, i32 %3, %struct.sockaddr_in* %4, i32* nocapture writeonly %5, i8** nocapture %6, i32 %7) {
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = bitcast i32* %9 to i8*
%12 = bitcast %struct.sockaddr_in* %10 to i8*
%13 = icmp eq %struct.sockaddr_in* %4, null
br i1 %13, label %14, label %23
14: ; preds = %8
%15 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%16 = load i32, i32* @PF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %2)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 0
store i32 %21, i32* %22
br label %23
23: ; preds = %8, %14
%24 = phi %struct.sockaddr_in* [ %10, %14 ], [ %4, %8 ]
store i32 0, i32* %5
%25 = load i32, i32* @SOCK_DGRAM
%26 = icmp eq i32 %25, %1
br i1 %26, label %27, label %40
27: ; preds = %23
store i32 12, i32* %9
%28 = bitcast %struct.sockaddr_in* %24 to %struct.sockaddr*
br label %29
29: ; preds = %34, %27
%30 = load i8*, i8** %6
%31 = load i32, i32* @MSG_PEEK
%32 = call i32 @recvfrom(i32 %0, i8* %30, i32 %7, i32 %31, %struct.sockaddr* %28, i32* %9)
store i32 %32, i32* %5
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %52
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %29, label %38
38: ; preds = %34
store i32 0, i32* %5
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %52
40: ; preds = %23
%41 = load i32, i32* @SOCK_STREAM
%42 = icmp eq i32 %41, %1
br i1 %42, label %43, label %52
43: ; preds = %40
store i8* null, i8** %6
store i32 12, i32* %9
%44 = bitcast %struct.sockaddr_in* %24 to %struct.sockaddr*
br label %45
45: ; preds = %48, %43
%46 = call i32 @accept(i32 %0, %struct.sockaddr* %44, i32* %9)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i64, i64* @errno
%50 = load i64, i64* @EINTR
%51 = icmp eq i64 %49, %50
br i1 %51, label %45, label %52
52: ; preds = %45, %48, %29, %40, %38
%53 = phi i32 [ 0, %38 ], [ %0, %40 ], [ %0, %29 ], [ %46, %45 ], [ 0, %48 ]
ret i32 %53
}