#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*/ 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 /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  char *buf;
  char *cp;
  ssize_t size;
  ssize_t ret;
  if (argc < 2 || argv[1] == NULL) {
    printf("one arg needed\n");
    exit(-1);
  }
  size = 256;
  buf = malloc(size);
  if (!buf)
    return -1;
  memset(buf, 0, size);
  ret = getxattr(argv[1], "security.selinux", buf, size - 1);
  if (ret == -1) {
    perror("getxattr: ");
  } else
    printf("xattr1 %s\n", buf);
  ret = setxattr(argv[1], "security.selinux", buf, strlen(buf), XATTR_REPLACE);
  if (ret != 0) {
    perror("setxattr :");
    exit(-1);
  }
  ret = getxattr(argv[1], "security.selinux", buf, size - 1);
  if (ret == -1) {
    perror("getxattr: ");
  } else
    printf("xattr2 %s\n", buf);
  return ret;
}


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   $0x2,-0x8(%rbp)
jl     11af <main+0x2f>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11c7 <main+0x47>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movl   $0x100,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f1 <main+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <main+0x181>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
lea    0xdf8(%rip),%rsi
call   1080 <getxattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    123b <main+0xbb>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
jmp    124d <main+0xcd>
mov    -0x18(%rbp),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <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   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
mov    0x2d65(%rip),%rax
mov    (%rax),%r8d
lea    0xd8b(%rip),%rsi
call   10a0 <setxattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12ad <main+0x12d>
lea    0xd99(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
lea    0xd4a(%rip),%rsi
call   1080 <getxattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12e9 <main+0x169>
lea    0xd42(%rip),%rdi
call   1070 <perror@plt>
jmp    12fb <main+0x17b>
mov    -0x18(%rbp),%rsi
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     1296 <main+0x106>
mov    %rsi,%rbp
cmpq   $0x0,0x8(%rsi)
je     1296 <main+0x106>
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     126c <main+0xdc>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    0x8(%rbp),%rdi
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdx
mov    $0xff,%ecx
call   1090 <getxattr@plt>
cmp    $0xffffffff,%eax
je     127a <main+0xea>
lea    0xe1b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbp),%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%r8d
lea    0xdd8(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   10b0 <setxattr@plt>
test   %eax,%eax
jne    12ac <main+0x11c>
mov    0x8(%rbp),%rdi
lea    0xdbc(%rip),%rsi
mov    %rbx,%rdx
mov    $0xff,%ecx
call   1090 <getxattr@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xf8>
mov    %eax,%ebp
lea    0xdd3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
jmp    120b <main+0x7b>
lea    0xd82(%rip),%rdi
call   1080 <perror@plt>
jmp    126c <main+0xdc>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xd74(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"one arg needed\0A\00"
@.str.1 = [17 x i8] c"security.selinux\00"
@.str.2 = [11 x i8] c"getxattr: \00"
@.str.3 = [11 x i8] c"xattr1 %s\0A\00"
@XATTR_REPLACE = global i32 0
@.str.4 = [11 x i8] c"setxattr :\00"
@.str.5 = [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, 2
br i1 %11, label %17, 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 %17, label %20
17: ; preds = %12, %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %12
store i32 256, i32* %8
%21 = load i32, i32* %8
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
store i32 -1, i32* %3
br label %75
26: ; preds = %20
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = call i32 @memset(i8* %27, i32 0, i32 %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i8*, i8** %6
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 1
%36 = call i32 @getxattr(i8* %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %26
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %44
41: ; preds = %26
%42 = load i8*, i8** %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %39
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = load i8*, i8** %6
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
%51 = load i32, i32* @XATTR_REPLACE
%52 = call i32 @setxattr(i8* %47, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %48, i32 %50, i32 %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %44
%56 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %44
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = load i8*, i8** %6
%63 = load i32, i32* %8
%64 = sub nsw i32 %63, 1
%65 = call i32 @getxattr(i8* %61, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %62, i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %58
%69 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %73
70: ; preds = %58
%71 = load i8*, i8** %6
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %70, %68
%74 = load i32, i32* %9
store i32 %74, i32* %3
br label %75
75: ; preds = %73, %25
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"security.selinux\00"
@.str.2 = [11 x i8] c"getxattr: \00"
@.str.3 = [11 x i8] c"xattr1 %s\0A\00"
@XATTR_REPLACE = global i32 0
@.str.4 = [11 x i8] c"setxattr :\00"
@.str.5 = [11 x i8] c"xattr2 %s\0A\00"
@str = [15 x i8] c"one arg needed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %8, 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 %8, label %11
8: ; preds = %4, %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %4
%12 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%13 = icmp eq i8* %12, null
br i1 %13, label %40, label %14
14: ; preds = %11
%15 = tail call i32 @memset(i8* %12, i32 0, i32 256)
%16 = load i8*, i8** %5
%17 = tail call i32 @getxattr(i8* %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 255)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %23
21: ; preds = %14
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %23
23: ; preds = %21, %19
%24 = load i8*, i8** %5
%25 = tail call i32 @strlen(i8* %12)
%26 = load i32, i32* @XATTR_REPLACE
%27 = tail call i32 @setxattr(i8* %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 %25, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %23
%33 = load i8*, i8** %5
%34 = tail call i32 @getxattr(i8* %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 255)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %40
38: ; preds = %32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %12)
br label %40
40: ; preds = %36, %38, %11
%41 = phi i32 [ -1, %11 ], [ %34, %38 ], [ -1, %36 ]
ret i32 %41
}