#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 S_IFCHR;
int /*<<< orphan*/ atoi(char *);
scalar_t__ chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ makedev(int, int /*<<< orphan*/);
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "%s <path>\n", argv[0]);
    return 1;
  }
  if (mknod(argv[1], S_IFCHR | 0666, makedev(2038, 0)) != 0) {
    perror("mknod");
    return 1;
  }
  char *caller_uid = getenv("SUDO_UID");
  char *caller_gid = getenv("SUDO_GID");
  if (caller_uid && caller_gid) {
    if (chown(argv[1], atoi(caller_uid), atoi(caller_gid)) != 0) {
      perror("chown");
      return 1;
    }
  }
  return 0;
}


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)
jge    11b8 <main+0x48>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <main+0x134>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
or     $0x1b6,%eax
mov    %eax,-0x24(%rbp)
mov    $0x7f6,%edi
xor    %esi,%esi
call   1080 <makedev@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
call   1050 <mknod@plt>
cmp    $0x0,%rax
je     1211 <main+0xa1>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <main+0x134>
lea    0xdf9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    0xdf2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     129d <main+0x12d>
cmpq   $0x0,-0x20(%rbp)
je     129d <main+0x12d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <atoi@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1060 <chown@plt>
cmp    $0x0,%rax
je     1298 <main+0x128>
lea    0xd9c(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <main+0x134>
jmp    129d <main+0x12d>
movl   $0x0,-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
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11a2 <main+0x32>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe6e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%r14d
jmp    1223 <main+0xb3>
mov    0x8(%rbx),%r15
mov    0x2e2b(%rip),%rax
mov    $0x1b6,%ebp
or     (%rax),%ebp
xor    %r14d,%r14d
mov    $0x7f6,%edi
xor    %esi,%esi
call   1080 <makedev@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <mknod@plt>
test   %rax,%rax
jne    1231 <main+0xc1>
lea    0xe36(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
lea    0xe30(%rip),%rdi
call   1030 <getenv@plt>
test   %rbp,%rbp
je     1223 <main+0xb3>
mov    %rax,%r15
test   %rax,%rax
je     1223 <main+0xb3>
mov    0x8(%rbx),%rbx
mov    %rbp,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1090 <atoi@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <chown@plt>
test   %rax,%rax
jne    123a <main+0xca>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd3(%rip),%rdi
jmp    1241 <main+0xd1>
lea    0xde2(%rip),%rdi
call   1070 <perror@plt>
jmp    1197 <main+0x27>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"%s <path>\0A\00"
@S_IFCHR = global i32 0
@.str.1 = [6 x i8] c"mknod\00"
@.str.2 = [9 x i8] c"SUDO_UID\00"
@.str.3 = [9 x i8] c"SUDO_GID\00"
@.str.4 = [6 x i8] c"chown\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca 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 slt i32 %8, 2
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %3
br label %49
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @S_IFCHR
%21 = or i32 %20, 438
%22 = call i32 @makedev(i32 2038, i32 0)
%23 = call i64 @mknod(i8* %19, i32 %21, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
27: ; preds = %16
%28 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* %28, i8** %6
%29 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i8* %29, i8** %7
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %48
32: ; preds = %27
%33 = load i8*, i8** %7
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %48
35: ; preds = %32
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = load i8*, i8** %6
%40 = call i32 @atoi(i8* %39)
%41 = load i8*, i8** %7
%42 = call i32 @atoi(i8* %41)
%43 = call i64 @chown(i8* %38, i32 %40, i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %49
47: ; preds = %35
br label %48
48: ; preds = %47, %32, %27
store i32 0, i32* %3
br label %49
49: ; preds = %48, %45, %25, %10
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"%s <path>\0A\00"
@S_IFCHR = global i32 0
@.str.1 = [6 x i8] c"mknod\00"
@.str.2 = [9 x i8] c"SUDO_UID\00"
@.str.3 = [9 x i8] c"SUDO_GID\00"
@.str.4 = [6 x i8] c"chown\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %32
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @S_IFCHR
%12 = or i32 %11, 438
%13 = tail call i32 @makedev(i32 2038, i32 0)
%14 = tail call i64 @mknod(i8* %10, i32 %12, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %32
18: ; preds = %8
%19 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp ne i8* %19, null
%22 = icmp ne i8* %20, null
%23 = and i1 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load i8*, i8** %9
%26 = tail call i32 @atoi(i8* %19)
%27 = tail call i32 @atoi(i8* %20)
%28 = tail call i64 @chown(i8* %25, i32 %26, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %24, %18, %16, %4
%33 = phi i32 [ 1, %4 ], [ 1, %16 ], [ 1, %30 ], [ 0, %24 ], [ 0, %18 ]
ret i32 %33
}