#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 scalar_t__ uid_t;
struct stat {
  int st_mode;
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ S_ISLNK(int);
int S_IWGRP;
int S_IWOTH;
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int storagegroup_main(int argc, char **argv) {
  struct stat stat_buf;
  char cmd[1000];
  sprintf(cmd, "%s display dev=all", argv[1]);
  fprintf(stdout, "command %s\n", cmd);
  if (lstat(argv[1], &stat_buf) < 0) {
    fprintf(stderr, "Cannot lstat %s\n", argv[1]);
    exit(1);
  }
  if (S_ISLNK(stat_buf.st_mode)) {
    fprintf(stderr, "Cannot use %s: it is a symbolic link", argv[1]);
    exit(1);
  }
  if (stat(argv[1], &stat_buf) < 0) {
    fprintf(stderr, "Cannot stat %s\n", argv[1]);
    exit(1);
  }
  if (stat_buf.st_uid != (uid_t)0) {
    fprintf(stderr, "Cannot use %s: invalid ownership\n", argv[1]);
    exit(1);
  }
  if (((stat_buf.st_mode & S_IWGRP) != 0) ||
      ((stat_buf.st_mode & S_IWOTH) != 0)) {
    fprintf(stderr, "Cannot use %s: invalid permissions\n", argv[1]);
    exit(1);
  }
  setuid(0);
  execl(argv[1], argv[1], "display", "dev=all", NULL);
  return (1);
}


x86 assembly (clang, -O0):
<storagegroup_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe58(%rip),%rsi
call   1070 <sprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
jge    120b <storagegroup_main+0x8b>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1244 <storagegroup_main+0xc4>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdfb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1286 <storagegroup_main+0x106>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdde(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     12b8 <storagegroup_main+0x138>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdbc(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%eax
mov    0x2d26(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12e2 <storagegroup_main+0x162>
mov    -0x20(%rbp),%eax
mov    0x2ce9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1309 <storagegroup_main+0x189>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd8d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1090 <setuid@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd84(%rip),%rdx
lea    0xcdd(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   10a0 <execl@plt>
mov    $0x1,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<storagegroup_main>:
push   %r14
push   %rbx
sub    $0x3f8,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdx
lea    0xe68(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1070 <sprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %rax,%rax
js     1267 <storagegroup_main+0xe7>
mov    (%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
jne    127d <storagegroup_main+0xfd>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1293 <storagegroup_main+0x113>
cmpq   $0x0,0x8(%rsp)
jne    12a9 <storagegroup_main+0x129>
mov    (%rsp),%eax
mov    0x2dda(%rip),%rcx
test   %eax,(%rcx)
jne    1251 <storagegroup_main+0xd1>
mov    0x2da7(%rip),%rcx
test   %eax,(%rcx)
jne    1251 <storagegroup_main+0xd1>
xor    %edi,%edi
call   1090 <setuid@plt>
mov    0x8(%rbx),%rdi
lea    0xe7c(%rip),%rdx
lea    0xdd5(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   10a0 <execl@plt>
mov    $0x1,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe22(%rip),%rsi
jmp    12bd <storagegroup_main+0x13d>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda4(%rip),%rsi
jmp    12bd <storagegroup_main+0x13d>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd9f(%rip),%rsi
jmp    12bd <storagegroup_main+0x13d>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xdae(%rip),%rsi
jmp    12bd <storagegroup_main+0x13d>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda8(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@.str = [19 x i8] c"%s display dev=all\00"
@stdout = global i32 0
@.str.1 = [12 x i8] c"command %s\0A\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"Cannot lstat %s\0A\00"
@.str.3 = [37 x i8] c"Cannot use %s: it is a symbolic link\00"
@.str.4 = [16 x i8] c"Cannot stat %s\0A\00"
@.str.5 = [34 x i8] c"Cannot use %s: invalid ownership\0A\00"
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.6 = [36 x i8] c"Cannot use %s: invalid permissions\0A\00"
@.str.7 = [8 x i8] c"display\00"
@.str.8 = [8 x i8] c"dev=all\00"
define i32 @storagegroup_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.stat
%6 = alloca [1000 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%8 = load i8**, i8*** %4
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @stdout
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @lstat(i8* %17, %struct.stat* %5)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISLNK(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i8**, i8*** %4
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %27
%40 = load i8**, i8*** %4
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i64 @stat(i8* %42, %struct.stat* %5)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = load i8**, i8*** %4
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %39
%53 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%54 = load i64, i64* %53
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = load i8**, i8*** %4
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %52
%64 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load i32, i32* @S_IWGRP
%67 = and i32 %65, %66
%68 = icmp ne i32 %67, 0
br i1 %68, label %75, label %69
69: ; preds = %63
%70 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%71 = load i32, i32* %70
%72 = load i32, i32* @S_IWOTH
%73 = and i32 %71, %72
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %69, %63
%76 = load i32, i32* @stderr
%77 = load i8**, i8*** %4
%78 = getelementptr inbounds i8*, i8** %77, i64 1
%79 = load i8*, i8** %78
%80 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %79)
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %69
%83 = call i32 @setuid(i32 0)
%84 = load i8**, i8*** %4
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = load i8**, i8*** %4
%88 = getelementptr inbounds i8*, i8** %87, i64 1
%89 = load i8*, i8** %88
%90 = call i32 @execl(i8* %86, i8* %89, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i32* null)
ret i32 1
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@.str = [19 x i8] c"%s display dev=all\00"
@stdout = global i32 0
@.str.1 = [12 x i8] c"command %s\0A\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"Cannot lstat %s\0A\00"
@.str.3 = [37 x i8] c"Cannot use %s: it is a symbolic link\00"
@.str.4 = [16 x i8] c"Cannot stat %s\0A\00"
@.str.5 = [34 x i8] c"Cannot use %s: invalid ownership\0A\00"
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.6 = [36 x i8] c"Cannot use %s: invalid permissions\0A\00"
@.str.7 = [8 x i8] c"display\00"
@.str.8 = [8 x i8] c"dev=all\00"
define i32 @storagegroup_main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [1000 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @stdout
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%12 = load i8*, i8** %7
%13 = call i64 @lstat(i8* %12, %struct.stat* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %7
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @S_ISLNK(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %7
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = load i8*, i8** %7
%32 = call i64 @stat(i8* %31, %struct.stat* %3)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %7
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%41 = load i64, i64* %40
%42 = icmp eq i64 %41, 0
br i1 %42, label %48, label %43
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %7
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %39
%49 = load i32, i32* %21
%50 = load i32, i32* @S_IWGRP
%51 = and i32 %50, %49
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = load i32, i32* @S_IWOTH
%55 = and i32 %54, %49
%56 = icmp eq i32 %55, 0
br i1 %56, label %62, label %57
57: ; preds = %53, %48
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %7
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %53
%63 = call i32 @setuid(i32 0)
%64 = load i8*, i8** %7
%65 = call i32 @execl(i8* %64, i8* %64, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i32* null)
ret i32 1
}