#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strlen(char *);
scalar_t__ write(int, char *, int);
int main(int argc, char *argv[]) {
  int i;
  int fd;
  int smacklen;
  int newargc;
  char *newargv[256];
  char *selfish1 = "/proc/self/attr/smack/current";
  char *selfish2 = "/proc/self/attr/current";
  char *smack;
  if (argc <= 1) {
    fprintf(stderr, "%s: No Smack value specified\n", argv[0]);
    exit(1);
  }
  if (argc > 256) {
    fprintf(stderr, "%s: Too many arguments specified\n", argv[0]);
    exit(1);
  }
  smack = argv[1];
  smacklen = strlen(smack);
  if (smacklen >= 256 || smacklen < 1) {
    fprintf(stderr, "%s: Bad Smack value \"%s\" specified\n", argv[0], smack);
    exit(1);
  }
  if (argc == 1) {
    fprintf(stderr, "%s: Starting a shell at \"%s\"\n", argv[0], smack);
    newargv[0] = "sh";
    newargv[1] = NULL;
  } else {
    for (i = 2; i < argc; i++)
      newargv[i - 2] = argv[i];
    newargv[i - 2] = NULL;
  }
  if ((fd = open(selfish1, O_RDWR)) < 0 && (fd = open(selfish2, O_RDWR)) < 0) {
    fprintf(stderr, "%s: cannot open to set smack\n", argv[0]);
    exit(1);
  }
  if (write(fd, smack, smacklen) < 0) {
    fprintf(stderr, "%s: cannot write to set smack\n", argv[0]);
    exit(1);
  }
  close(fd);
  execvp(newargv[0], newargv);
  fprintf(stderr, "%s: exec failure.\n", argv[0]);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rax
mov    %rax,-0x828(%rbp)
lea    0xe80(%rip),%rax
mov    %rax,-0x830(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11d7 <main+0x67>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x100,-0x8(%rbp)
jle    120c <main+0x9c>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jge    1241 <main+0xd1>
cmpl   $0x1,-0x1c(%rbp)
jge    1270 <main+0x100>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x838(%rbp),%rcx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jne    12bd <main+0x14d>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x838(%rbp),%rcx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe12(%rip),%rax
mov    %rax,-0x820(%rbp)
movq   $0x0,-0x818(%rbp)
jmp    130e <main+0x19e>
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12fa <main+0x18a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
cltq
mov    %rcx,-0x820(%rbp,%rax,8)
ff
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12c4 <main+0x154>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
cltq
movq   $0x0,-0x820(%rbp,%rax,8)
mov    -0x828(%rbp),%rdi
mov    0x2ccc(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1378 <main+0x208>
mov    -0x830(%rbp),%rdi
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1378 <main+0x208>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    13bc <main+0x24c>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd2e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x820(%rbp),%rdi
lea    -0x820(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2bfa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd0a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rsi,%r12
cmp    $0x1,%edi
jg     11ae <main+0x2e>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xe8a(%rip),%rsi
jmp    11cc <main+0x4c>
mov    %edi,%ebx
cmp    $0x101,%edi
jb     11dd <main+0x5d>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x8(%r12),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    -0x100(%r15),%eax
cmp    $0xffffff01,%eax
jae    1223 <main+0xa3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xe67(%rip),%rsi
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmp    $0x3,%ebx
jb     1242 <main+0xc2>
lea    0x10(%r12),%rsi
add    $0xfffffffe,%ebx
lea    0x0(,%rbx,8),%rdx
mov    %rsp,%rdi
call   1070 <memcpy@plt>
jmp    1244 <main+0xc4>
xor    %ebx,%ebx
movq   $0x0,(%rsp,%rbx,8)
mov    0x2d95(%rip),%r13
mov    0x0(%r13),%esi
lea    0xda2(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1298 <main+0x118>
mov    0x0(%r13),%esi
lea    0xdaa(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1298 <main+0x118>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xe07(%rip),%rsi
jmp    11cc <main+0x4c>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    12c3 <main+0x143>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xdfa(%rip),%rsi
jmp    11cc <main+0x4c>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%rsp),%rdi
mov    %rsp,%rsi
call   1090 <execvp@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xded(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"/proc/self/attr/smack/current\00"
@.str.1 = [24 x i8] c"/proc/self/attr/current\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"%s: No Smack value specified\0A\00"
@.str.3 = [34 x i8] c"%s: Too many arguments specified\0A\00"
@.str.4 = [36 x i8] c"%s: Bad Smack value \22%s\22 specified\0A\00"
@.str.5 = [30 x i8] c"%s: Starting a shell at \22%s\22\0A\00"
@.str.6 = [3 x i8] c"sh\00"
@O_RDWR = global i32 0
@.str.7 = [30 x i8] c"%s: cannot open to set smack\0A\00"
@.str.8 = [31 x i8] c"%s: cannot write to set smack\0A\00"
@.str.9 = [19 x i8] c"%s: exec failure.\0A\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 i32
%9 = alloca i32
%10 = alloca [256 x i8*]
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8** %11
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8** %12
%14 = load i32, i32* %4
%15 = icmp sle i32 %14, 1
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 256
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
store i8* %36, i8** %13
%37 = load i8*, i8** %13
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp sge i32 %39, 256
br i1 %40, label %44, label %41
41: ; preds = %33
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 1
br i1 %43, label %44, label %52
44: ; preds = %41, %33
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = load i8*, i8** %13
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %48, i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %41
%53 = load i32, i32* %4
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 0
%59 = load i8*, i8** %58
%60 = load i8*, i8** %13
%61 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %59, i8* %60)
%62 = getelementptr inbounds [256 x i8*], [256 x i8*]* %10, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %62
%63 = getelementptr inbounds [256 x i8*], [256 x i8*]* %10, i64 0, i64 1
store i8* null, i8** %63
br label %87
64: ; preds = %52
store i32 2, i32* %6
br label %65
65: ; preds = %79, %64
%66 = load i32, i32* %6
%67 = load i32, i32* %4
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %82
69: ; preds = %65
%70 = load i8**, i8*** %5
%71 = load i32, i32* %6
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
%74 = load i8*, i8** %73
%75 = load i32, i32* %6
%76 = sub nsw i32 %75, 2
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [256 x i8*], [256 x i8*]* %10, i64 0, i64 %77
store i8* %74, i8** %78
br label %79
79: ; preds = %69
%80 = load i32, i32* %6
%81 = add nsw i32 %80, 1
store i32 %81, i32* %6
br label %65
82: ; preds = %65
%83 = load i32, i32* %6
%84 = sub nsw i32 %83, 2
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [256 x i8*], [256 x i8*]* %10, i64 0, i64 %85
store i8* null, i8** %86
br label %87
87: ; preds = %82, %55
%88 = load i8*, i8** %11
%89 = load i32, i32* @O_RDWR
%90 = call i32 @open(i8* %88, i32 %89)
store i32 %90, i32* %7
%91 = icmp slt i32 %90, 0
br i1 %91, label %92, label %104
92: ; preds = %87
%93 = load i8*, i8** %12
%94 = load i32, i32* @O_RDWR
%95 = call i32 @open(i8* %93, i32 %94)
store i32 %95, i32* %7
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %104
97: ; preds = %92
%98 = load i32, i32* @stderr
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 0
%101 = load i8*, i8** %100
%102 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %98, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %101)
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %92, %87
%105 = load i32, i32* %7
%106 = load i8*, i8** %13
%107 = load i32, i32* %8
%108 = call i64 @write(i32 %105, i8* %106, i32 %107)
%109 = icmp slt i64 %108, 0
br i1 %109, label %110, label %117
110: ; preds = %104
%111 = load i32, i32* @stderr
%112 = load i8**, i8*** %5
%113 = getelementptr inbounds i8*, i8** %112, i64 0
%114 = load i8*, i8** %113
%115 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %111, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %114)
%116 = call i32 @exit(i32 1)
unreachable
117: ; preds = %104
%118 = load i32, i32* %7
%119 = call i32 @close(i32 %118)
%120 = getelementptr inbounds [256 x i8*], [256 x i8*]* %10, i64 0, i64 0
%121 = load i8*, i8** %120
%122 = getelementptr inbounds [256 x i8*], [256 x i8*]* %10, i64 0, i64 0
%123 = call i32 @execvp(i8* %121, i8** %122)
%124 = load i32, i32* @stderr
%125 = load i8**, i8*** %5
%126 = getelementptr inbounds i8*, i8** %125, i64 0
%127 = load i8*, i8** %126
%128 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %124, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %127)
%129 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [30 x i8] c"/proc/self/attr/smack/current\00"
@.str.1 = [24 x i8] c"/proc/self/attr/current\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"%s: No Smack value specified\0A\00"
@.str.3 = [34 x i8] c"%s: Too many arguments specified\0A\00"
@.str.4 = [36 x i8] c"%s: Bad Smack value \22%s\22 specified\0A\00"
@O_RDWR = global i32 0
@.str.7 = [30 x i8] c"%s: cannot open to set smack\0A\00"
@.str.8 = [31 x i8] c"%s: cannot write to set smack\0A\00"
@.str.9 = [19 x i8] c"%s: exec failure.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8*]
%4 = bitcast [256 x i8*]* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp ugt i32 %0, 256
br i1 %12, label %13, label %18
13: ; preds = %11
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %1
%16 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @strlen(i8* %20)
%22 = add i32 %21, -256
%23 = icmp ult i32 %22, -255
br i1 %23, label %34, label %24
24: ; preds = %18
%25 = icmp ugt i32 %0, 2
br i1 %25, label %26, label %39
26: ; preds = %24
%27 = getelementptr i8*, i8** %1, i64 2
%28 = bitcast i8** %27 to i8*
%29 = add nsw i32 %0, -2
%30 = zext i32 %29 to i64
%31 = shl nuw nsw i64 %30, 3
%32 = add nsw i32 %0, -2
%33 = zext i32 %32 to i64
br label %39
34: ; preds = %18
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %1
%37 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %20)
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %26, %24
%40 = phi i64 [ 0, %24 ], [ %33, %26 ]
%41 = getelementptr inbounds [256 x i8*], [256 x i8*]* %3, i64 0, i64 %40
store i8* null, i8** %41
%42 = load i32, i32* @O_RDWR
%43 = tail call i32 @open(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %39
%46 = load i32, i32* @O_RDWR
%47 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %1
%52 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %51)
%53 = tail call i32 @exit(i32 1)
unreachable
54: ; preds = %45, %39
%55 = phi i32 [ %47, %45 ], [ %43, %39 ]
%56 = tail call i64 @write(i32 %55, i8* %20, i32 %21)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %54
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %1
%61 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %60)
%62 = tail call i32 @exit(i32 1)
unreachable
63: ; preds = %54
%64 = tail call i32 @close(i32 %55)
%65 = getelementptr inbounds [256 x i8*], [256 x i8*]* %3, i64 0, i64 0
%66 = load i8*, i8** %65
%67 = call i32 @execvp(i8* %66, i8** %65)
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %1
%70 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %69)
%71 = call i32 @exit(i32 0)
unreachable
}