#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ XATTR_REPLACE;
int /*<<< orphan*/ dis_issbuf(char *);
int /*<<< orphan*/ exit(int);
int getxattr(char *, char *, char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int setxattr(char *, char *, char *, int, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  char *buf;
  char *cp;
  ssize_t size;
  ssize_t ret;
  if (argc < 3 || argv[1] == NULL || argv[2] == NULL) {
    printf("two arg needed\n");
    exit(-1);
  }
  size = 256;
  buf = malloc(size);
  if (!buf)
    return -1;
  memset(buf, 0, size);
  ret = getxattr(argv[1], "security.kse", buf, size - 1);
  if (ret == -1) {
    perror("getxattr: ");
  } else {
    printf("xattr1 %s\n", buf);
    dis_issbuf(buf);
  }
  memset(buf, 0, size);
  strncpy(buf, argv[2], strlen(argv[2]));
  buf[strlen(buf)] = '\0';
  printf("buf %s size %d\n", buf, strlen(buf));
  ret = setxattr(argv[1], "security.kse", buf, strlen(buf), XATTR_REPLACE);
  if (ret != 0) {
    perror("setxattr :");
    exit(-1);
  }
  memset(buf, 0, size);
  ret = getxattr(argv[1], "security.kse", buf, size - 1);
  if (ret == -1) {
    perror("getxattr: ");
  } else {
    printf("xattr2 %s\n", buf);
    dis_issbuf(buf);
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11de <main+0x3e>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11de <main+0x3e>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    11f6 <main+0x56>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x100,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1220 <main+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d1 <main+0x231>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
lea    0xdc9(%rip),%rsi
call   10a0 <getxattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    126a <main+0xca>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
jmp    1285 <main+0xe5>
mov    -0x18(%rbp),%rsi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dis_issbuf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
mov    0x2cac(%rip),%rax
mov    (%rax),%r8d
lea    0xcd2(%rip),%rsi
call   10c0 <setxattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1366 <main+0x1c6>
lea    0xcec(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
lea    0xc83(%rip),%rsi
call   10a0 <getxattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    13b0 <main+0x210>
lea    0xc77(%rip),%rdi
call   1090 <perror@plt>
jmp    13cb <main+0x22b>
mov    -0x18(%rbp),%rsi
lea    0xc93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dis_issbuf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jl     1338 <main+0x188>
mov    %rsi,%r15
cmpq   $0x0,0x8(%rsi)
je     1338 <main+0x188>
cmpq   $0x0,0x10(%r15)
je     1338 <main+0x188>
mov    $0x100,%edi
call   1090 <malloc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     130b <main+0x15b>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    0x8(%r15),%rdi
lea    0xdf1(%rip),%rsi
mov    %rbx,%rdx
mov    $0xff,%ecx
call   10b0 <getxattr@plt>
cmp    $0xffffffff,%eax
je     1319 <main+0x169>
lea    0xdec(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <dis_issbuf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    0x10(%r15),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xda0(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%r15),%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0x2d3e(%rip),%rcx
mov    (%rcx),%r8d
lea    0xd54(%rip),%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   10d0 <setxattr@plt>
test   %eax,%eax
jne    134e <main+0x19e>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    0x8(%r15),%rdi
lea    0xd25(%rip),%rsi
mov    %rbx,%rdx
mov    $0xff,%ecx
call   10b0 <getxattr@plt>
cmp    $0xffffffff,%eax
je     132a <main+0x17a>
mov    %eax,%ebp
lea    0xd48(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <dis_issbuf@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xced(%rip),%rdi
call   10a0 <perror@plt>
jmp    123e <main+0x8e>
lea    0xcdc(%rip),%rdi
call   10a0 <perror@plt>
jmp    130b <main+0x15b>
lea    0xd0a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xcde(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"two arg needed\0A\00"
@.str.1 = [13 x i8] c"security.kse\00"
@.str.2 = [11 x i8] c"getxattr: \00"
@.str.3 = [11 x i8] c"xattr1 %s\0A\00"
@.str.4 = [16 x i8] c"buf %s size %d\0A\00"
@XATTR_REPLACE = global i32 0
@.str.5 = [11 x i8] c"setxattr :\00"
@.str.6 = [11 x i8] c"xattr2 %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%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 slt i32 %10, 3
br i1 %11, label %22, label %12
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17, %12, %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %17
store i32 256, i32* %8
%26 = load i32, i32* %8
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
store i32 -1, i32* %3
br label %108
31: ; preds = %25
%32 = load i8*, i8** %6
%33 = load i32, i32* %8
%34 = call i32 @memset(i8* %32, i32 0, i32 %33)
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i8*, i8** %6
%39 = load i32, i32* %8
%40 = sub nsw i32 %39, 1
%41 = call i32 @getxattr(i8* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %38, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %31
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %51
46: ; preds = %31
%47 = load i8*, i8** %6
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = load i8*, i8** %6
%50 = call i32 @dis_issbuf(i8* %49)
br label %51
51: ; preds = %46, %44
%52 = load i8*, i8** %6
%53 = load i32, i32* %8
%54 = call i32 @memset(i8* %52, i32 0, i32 %53)
%55 = load i8*, i8** %6
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load i8*, i8** %57
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 2
%61 = load i8*, i8** %60
%62 = call i32 @strlen(i8* %61)
%63 = call i32 @strncpy(i8* %55, i8* %58, i32 %62)
%64 = load i8*, i8** %6
%65 = load i8*, i8** %6
%66 = call i32 @strlen(i8* %65)
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %64, i64 %67
store i8 0, i8* %68
%69 = load i8*, i8** %6
%70 = load i8*, i8** %6
%71 = call i32 @strlen(i8* %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %69, i32 %71)
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = load i8*, i8** %6
%77 = load i8*, i8** %6
%78 = call i32 @strlen(i8* %77)
%79 = load i32, i32* @XATTR_REPLACE
%80 = call i32 @setxattr(i8* %75, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %76, i32 %78, i32 %79)
store i32 %80, i32* %9
%81 = load i32, i32* %9
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %51
%84 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%85 = call i32 @exit(i32 -1)
unreachable
86: ; preds = %51
%87 = load i8*, i8** %6
%88 = load i32, i32* %8
%89 = call i32 @memset(i8* %87, i32 0, i32 %88)
%90 = load i8**, i8*** %5
%91 = getelementptr inbounds i8*, i8** %90, i64 1
%92 = load i8*, i8** %91
%93 = load i8*, i8** %6
%94 = load i32, i32* %8
%95 = sub nsw i32 %94, 1
%96 = call i32 @getxattr(i8* %92, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %93, i32 %95)
store i32 %96, i32* %9
%97 = load i32, i32* %9
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %101
99: ; preds = %86
%100 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %106
101: ; preds = %86
%102 = load i8*, i8** %6
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %102)
%104 = load i8*, i8** %6
%105 = call i32 @dis_issbuf(i8* %104)
br label %106
106: ; preds = %101, %99
%107 = load i32, i32* %9
store i32 %107, i32* %3
br label %108
108: ; preds = %106, %30
%109 = load i32, i32* %3
ret i32 %109
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"security.kse\00"
@.str.2 = [11 x i8] c"getxattr: \00"
@.str.3 = [11 x i8] c"xattr1 %s\0A\00"
@.str.4 = [16 x i8] c"buf %s size %d\0A\00"
@XATTR_REPLACE = global i32 0
@.str.5 = [11 x i8] c"setxattr :\00"
@.str.6 = [11 x i8] c"xattr2 %s\0A\00"
@str = [15 x i8] c"two arg needed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %8, %4, %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %8
%16 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%17 = icmp eq i8* %16, null
br i1 %17, label %56, label %18
18: ; preds = %15
%19 = tail call i32 @memset(i8* %16, i32 0, i32 256)
%20 = load i8*, i8** %5
%21 = tail call i32 @getxattr(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %16, i32 255)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %28
25: ; preds = %18
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%27 = tail call i32 @dis_issbuf(i8* %16)
br label %28
28: ; preds = %25, %23
%29 = tail call i32 @memset(i8* %16, i32 0, i32 256)
%30 = load i8*, i8** %9
%31 = tail call i32 @strlen(i8* %30)
%32 = tail call i32 @strncpy(i8* %16, i8* %30, i32 %31)
%33 = tail call i32 @strlen(i8* %16)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %16, i64 %34
store i8 0, i8* %35
%36 = tail call i32 @strlen(i8* %16)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %16, i32 %36)
%38 = load i8*, i8** %5
%39 = tail call i32 @strlen(i8* %16)
%40 = load i32, i32* @XATTR_REPLACE
%41 = tail call i32 @setxattr(i8* %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %16, i32 %39, i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %28
%44 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%45 = tail call i32 @exit(i32 -1)
unreachable
46: ; preds = %28
%47 = tail call i32 @memset(i8* %16, i32 0, i32 256)
%48 = load i8*, i8** %5
%49 = tail call i32 @getxattr(i8* %48, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %16, i32 255)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %56
53: ; preds = %46
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %16)
%55 = tail call i32 @dis_issbuf(i8* %16)
br label %56
56: ; preds = %51, %53, %15
%57 = phi i32 [ -1, %15 ], [ %49, %53 ], [ -1, %51 ]
ret i32 %57
}