#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 /*<<< orphan*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ isalnum(char);
int /*<<< orphan*/ perror(char *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv) {
  char cmd[1024];
  char *p;
  if (argc != 2) {
    fprintf(stderr, "Syntax: fdiskwrap device\n");
    exit(1);
  }
  if (setuid(0) != 0) {
    perror("Unable to setuid(0)\n");
    exit(2);
  }
  for (p = argv[1]; *p; p++) {
    if (!isalnum(*p)) {
      fprintf(stderr, "Illegal argument.\n");
      exit(10);
    }
  }
  snprintf(cmd, sizeof(cmd), "/sbin/fdisk -l /dev/%s", argv[1]);
  system(cmd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b2 <main+0x42>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     11d9 <main+0x69>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
cmpb   $0x0,(%rax)
je     124b <main+0xdb>
mov    -0x418(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%eax
jne    122f <main+0xbf>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xa,%edi
call   1080 <exit@plt>
jmp    1234 <main+0xc4>
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
jmp    11e8 <main+0x78>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x400,%esi
lea    0xddc(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x410(%rbp),%rdi
call   1030 <system@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
cmp    $0x2,%edi
jne    1219 <main+0xa9>
mov    %rsi,%r14
xor    %edi,%edi
call   1090 <setuid@plt>
test   %rax,%rax
jne    1238 <main+0xc8>
mov    0x8(%r14),%rbx
mov    (%rbx),%al
test   %al,%al
je     11cb <main+0x5b>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl %al,%edi
call   1040 <isalnum@plt>
test   %eax,%eax
je     11fa <main+0x8a>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11b0 <main+0x40>
mov    0x8(%r14),%rbx
lea    0xe70(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   1030 <system@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xa,%edi
call   1080 <exit@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xddb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Syntax: fdiskwrap device\0A\00"
@.str.1 = [21 x i8] c"Unable to setuid(0)\0A\00"
@.str.2 = [19 x i8] c"Illegal argument.\0A\00"
@.str.3 = [23 x i8] c"/sbin/fdisk -l /dev/%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i64 @setuid(i32 0)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %7
br label %24
24: ; preds = %38, %20
%25 = load i8*, i8** %7
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = load i8, i8* %29
%31 = call i32 @isalnum(i8 signext %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 10)
unreachable
37: ; preds = %28
br label %38
38: ; preds = %37
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %7
br label %24
41: ; preds = %24
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 @snprintf(i8* %42, i32 1024, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = call i32 @system(i8* %47)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Syntax: fdiskwrap device\0A\00"
@.str.1 = [21 x i8] c"Unable to setuid(0)\0A\00"
@.str.2 = [19 x i8] c"Illegal argument.\0A\00"
@.str.3 = [23 x i8] c"/sbin/fdisk -l /dev/%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i64 @setuid(i32 0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 2)
unreachable
16: ; preds = %10
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %36, label %24
21: ; preds = %24
%22 = load i8, i8* %29
%23 = icmp eq i8 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %16, %21
%25 = phi i8 [ %22, %21 ], [ %19, %16 ]
%26 = phi i8* [ %29, %21 ], [ %18, %16 ]
%27 = tail call i32 @isalnum(i8 signext %25)
%28 = icmp eq i32 %27, 0
%29 = getelementptr inbounds i8, i8* %26, i64 1
br i1 %28, label %30, label %21
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 10)
unreachable
34: ; preds = %21
%35 = load i8*, i8** %17
br label %36
36: ; preds = %34, %16
%37 = phi i8* [ %35, %34 ], [ %18, %16 ]
%38 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = call i32 @system(i8* %4)
ret i32 0
}