#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 cifs_ntsd {
  int dummy;
};
struct cifs_ace {
  int dummy;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int copy_ace(struct cifs_ace *, struct cifs_ace *);
int /*<<< orphan*/ copy_sec_desc(struct cifs_ntsd *, struct cifs_ntsd *, int,
                                 int);
int get_sec_desc_size(struct cifs_ntsd *, struct cifs_ntsd **, int,
                      int /*<<< orphan*/ *, size_t *);
int ace_set(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd,
            ssize_t *bufsize, struct cifs_ace **cacesptr, int numcaces) {
  int i, rc, size = 0, acessize = 0;
  size_t acesoffset;
  char *acesptr;
  rc = get_sec_desc_size(pntsd, npntsd, numcaces, bufsize, &acesoffset);
  if (rc)
    return rc;
  acesptr = (char *)*npntsd + acesoffset;
  for (i = 0; i < numcaces; ++i) {
    size = copy_ace((struct cifs_ace *)acesptr, cacesptr[i]);
    acessize += size;
    acesptr += size;
  }
  copy_sec_desc(pntsd, *npntsd, numcaces, acessize);
  acesptr = (char *)*npntsd + acesoffset;
  return 0;
}


x86 assembly (clang, -O0):
<ace_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1050 <get_sec_desc_size@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     118a <ace_set+0x5a>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <ace_set+0xe8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11ec <ace_set+0xbc>
mov    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <copy_ace@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11a0 <ace_set+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x3c(%rbp),%ecx
call   1040 <copy_sec_desc@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ace_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r15d
mov    %rcx,%rbx
mov    %rdx,%rcx
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x20(%rsp),%r8
mov    %r15d,%edx
call   1050 <get_sec_desc_size@plt>
mov    %eax,%r13d
test   %eax,%eax
jne    11d5 <ace_set+0xa5>
mov    (%r14),%rbp
xor    %r13d,%r13d
test   %r15d,%r15d
jle    11c1 <ace_set+0x91>
mov    %r14,0x10(%rsp)
mov    %r12,0x18(%rsp)
add    0x20(%rsp),%rbp
mov    %r15d,0xc(%rsp)
mov    %r15d,%r15d
xor    %r12d,%r12d
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    (%rbx,%r12,8),%rsi
mov    %rbp,%rdi
call   1030 <copy_ace@plt>
add    %eax,%r14d
cltq
add    %rax,%rbp
add    $0x1,%r12
cmp    %r12,%r15
jne    1190 <ace_set+0x60>
mov    0x10(%rsp),%rax
mov    (%rax),%rbp
mov    0xc(%rsp),%r15d
mov    0x18(%rsp),%r12
jmp    11c4 <ace_set+0x94>
xor    %r14d,%r14d
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1040 <copy_sec_desc@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cifs_ntsd = type { i32 }
%struct.cifs_ace = type { i32 }
define i32 @ace_set(%struct.cifs_ntsd* %0, %struct.cifs_ntsd** %1, i32* %2, %struct.cifs_ace** %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.cifs_ntsd*
%8 = alloca %struct.cifs_ntsd**
%9 = alloca i32*
%10 = alloca %struct.cifs_ace**
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
%17 = alloca i8*
store %struct.cifs_ntsd* %0, %struct.cifs_ntsd** %7
store %struct.cifs_ntsd** %1, %struct.cifs_ntsd*** %8
store i32* %2, i32** %9
store %struct.cifs_ace** %3, %struct.cifs_ace*** %10
store i32 %4, i32* %11
store i32 0, i32* %14
store i32 0, i32* %15
%18 = load %struct.cifs_ntsd*, %struct.cifs_ntsd** %7
%19 = load %struct.cifs_ntsd**, %struct.cifs_ntsd*** %8
%20 = load i32, i32* %11
%21 = load i32*, i32** %9
%22 = call i32 @get_sec_desc_size(%struct.cifs_ntsd* %18, %struct.cifs_ntsd** %19, i32 %20, i32* %21, i64* %16)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %5
%26 = load i32, i32* %13
store i32 %26, i32* %6
br label %68
27: ; preds = %5
%28 = load %struct.cifs_ntsd**, %struct.cifs_ntsd*** %8
%29 = load %struct.cifs_ntsd*, %struct.cifs_ntsd** %28
%30 = bitcast %struct.cifs_ntsd* %29 to i8*
%31 = load i64, i64* %16
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %17
store i32 0, i32* %12
br label %33
33: ; preds = %53, %27
%34 = load i32, i32* %12
%35 = load i32, i32* %11
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %56
37: ; preds = %33
%38 = load i8*, i8** %17
%39 = bitcast i8* %38 to %struct.cifs_ace*
%40 = load %struct.cifs_ace**, %struct.cifs_ace*** %10
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds %struct.cifs_ace*, %struct.cifs_ace** %40, i64 %42
%44 = load %struct.cifs_ace*, %struct.cifs_ace** %43
%45 = call i32 @copy_ace(%struct.cifs_ace* %39, %struct.cifs_ace* %44)
store i32 %45, i32* %14
%46 = load i32, i32* %14
%47 = load i32, i32* %15
%48 = add nsw i32 %47, %46
store i32 %48, i32* %15
%49 = load i32, i32* %14
%50 = load i8*, i8** %17
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %17
br label %53
53: ; preds = %37
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
store i32 %55, i32* %12
br label %33
56: ; preds = %33
%57 = load %struct.cifs_ntsd*, %struct.cifs_ntsd** %7
%58 = load %struct.cifs_ntsd**, %struct.cifs_ntsd*** %8
%59 = load %struct.cifs_ntsd*, %struct.cifs_ntsd** %58
%60 = load i32, i32* %11
%61 = load i32, i32* %15
%62 = call i32 @copy_sec_desc(%struct.cifs_ntsd* %57, %struct.cifs_ntsd* %59, i32 %60, i32 %61)
%63 = load %struct.cifs_ntsd**, %struct.cifs_ntsd*** %8
%64 = load %struct.cifs_ntsd*, %struct.cifs_ntsd** %63
%65 = bitcast %struct.cifs_ntsd* %64 to i8*
%66 = load i64, i64* %16
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8* %67, i8** %17
store i32 0, i32* %6
br label %68
68: ; preds = %56, %25
%69 = load i32, i32* %6
ret i32 %69
}

LLVM IR (-O2):
%struct.cifs_ntsd = type { i32 }
%struct.cifs_ace = type { i32 }
define i32 @ace_set(%struct.cifs_ntsd* %0, %struct.cifs_ntsd** %1, i32* %2, %struct.cifs_ace** nocapture %3, i32 %4) {
%6 = alloca i64
%7 = bitcast i64* %6 to i8*
%8 = call i32 @get_sec_desc_size(%struct.cifs_ntsd* %0, %struct.cifs_ntsd** %1, i32 %4, i32* %2, i64* %6)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %38
10: ; preds = %5
%11 = bitcast %struct.cifs_ntsd** %1 to i8**
%12 = load i8*, i8** %11
%13 = icmp sgt i32 %4, 0
%14 = bitcast i8* %12 to %struct.cifs_ntsd*
br i1 %13, label %15, label %34
15: ; preds = %10
%16 = load i64, i64* %6
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = zext i32 %4 to i64
br label %19
19: ; preds = %15, %19
%20 = phi i64 [ 0, %15 ], [ %30, %19 ]
%21 = phi i8* [ %17, %15 ], [ %29, %19 ]
%22 = phi i32 [ 0, %15 ], [ %27, %19 ]
%23 = bitcast i8* %21 to %struct.cifs_ace*
%24 = getelementptr inbounds %struct.cifs_ace*, %struct.cifs_ace** %3, i64 %20
%25 = load %struct.cifs_ace*, %struct.cifs_ace** %24
%26 = call i32 @copy_ace(%struct.cifs_ace* %23, %struct.cifs_ace* %25)
%27 = add nsw i32 %26, %22
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %21, i64 %28
%30 = add nuw nsw i64 %20, 1
%31 = icmp eq i64 %30, %18
br i1 %31, label %32, label %19
32: ; preds = %19
%33 = load %struct.cifs_ntsd*, %struct.cifs_ntsd** %1
br label %34
34: ; preds = %32, %10
%35 = phi %struct.cifs_ntsd* [ %14, %10 ], [ %33, %32 ]
%36 = phi i32 [ 0, %10 ], [ %27, %32 ]
%37 = call i32 @copy_sec_desc(%struct.cifs_ntsd* %0, %struct.cifs_ntsd* %35, i32 %4, i32 %36)
br label %38
38: ; preds = %5, %34
%39 = phi i32 [ 0, %34 ], [ %8, %5 ]
ret i32 %39
}