#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 */
struct stat {
  scalar_t__ st_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ O_CREAT;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ diod_log_init(char *);
int /*<<< orphan*/ err_exit(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ getgid();
int /*<<< orphan*/ msg(char *, ...);
int /*<<< orphan*/ msg_exit(char *);
int open(char *, int /*<<< orphan*/, int);
scalar_t__ setegid(scalar_t__);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int fd;
  gid_t egid, gid;
  struct stat sb;
  diod_log_init(argv[0]);
  if (argc != 3) {
    msg("Usage: tfsgid gid path");
    exit(1);
  }
  egid = strtoul(argv[1], NULL, 10);
  gid = getgid();
  if (setegid(egid) < 0)
    err_exit("setegid");
  if (access(argv[2], F_OK) == 0)
    msg_exit("file exists");
  if ((fd = open(argv[2], O_CREAT, 0644)) < 0)
    err_exit("open");
  (void)close(fd);
  if (stat(argv[2], &sb) < 0)
    err_exit("stat");
  msg("file gid is %s egid and %s gid",
      sb.st_gid == egid ? "same as" : "different than",
      sb.st_gid == gid ? "same as" : "different than");
  (void)unlink(argv[2]);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <diod_log_init@plt>
cmpl   $0x3,-0x8(%rbp)
je     1214 <main+0x44>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   10f0 <msg@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getgid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   10e0 <setegid@plt>
cmp    $0x0,%rax
jge    1258 <main+0x88>
lea    0xdc4(%rip),%rdi
call   1080 <err_exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
jne    1284 <main+0xb4>
lea    0xda0(%rip),%rdi
call   1060 <msg_exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12b7 <main+0xe7>
lea    0xd79(%rip),%rdi
call   1080 <err_exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    12e6 <main+0x116>
lea    0xd4f(%rip),%rdi
call   1080 <err_exit@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rdx
lea    0xd67(%rip),%rsi
lea    0xd58(%rip),%rax
cmp    %rdx,%rcx
cmove  %rax,%rsi
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rdi
lea    0xd4a(%rip),%rdx
lea    0xd3b(%rip),%rax
cmp    %rdi,%rcx
cmove  %rax,%rdx
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%rdi
call   10d0 <diod_log_init@plt>
cmp    $0x3,%ebp
jne    12d4 <main+0x104>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10b0 <strtoul@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1070 <getgid@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   10e0 <setegid@plt>
test   %rax,%rax
jns    1223 <main+0x53>
lea    0xdf9(%rip),%rdi
call   1080 <err_exit@plt>
mov    0x10(%rbx),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
test   %rax,%rax
jne    1246 <main+0x76>
lea    0xdde(%rip),%rdi
call   1060 <msg_exit@plt>
mov    0x10(%rbx),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    126f <main+0x9f>
lea    0xdc1(%rip),%rdi
call   1080 <err_exit@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x10(%rbx),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jns    1293 <main+0xc3>
lea    0xda2(%rip),%rdi
call   1080 <err_exit@plt>
mov    (%rsp),%rax
cmp    %r14,%rax
lea    0xdb3(%rip),%rcx
lea    0xdb4(%rip),%rdx
mov    %rdx,%rsi
cmove  %rcx,%rsi
cmp    %r15,%rax
cmove  %rcx,%rdx
lea    0xd78(%rip),%rdi
xor    %eax,%eax
call   10f0 <msg@plt>
mov    0x10(%rbx),%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd25(%rip),%rdi
xor    %eax,%eax
call   10f0 <msg@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [23 x i8] c"Usage: tfsgid gid path\00"
@.str.1 = [8 x i8] c"setegid\00"
@F_OK = global i32 0
@.str.2 = [12 x i8] c"file exists\00"
@O_CREAT = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [5 x i8] c"stat\00"
@.str.5 = [31 x i8] c"file gid is %s egid and %s gid\00"
@.str.6 = [8 x i8] c"same as\00"
@.str.7 = [15 x i8] c"different than\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 @diod_log_init(i8* %12)
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @strtoul(i8* %22, i32* null, i32 10)
store i64 %23, i64* %7
%24 = call i64 (...) @getgid()
store i64 %24, i64* %8
%25 = load i64, i64* %7
%26 = call i64 @setegid(i64 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = call i32 @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %19
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i32, i32* @F_OK
%35 = call i64 @access(i8* %33, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @msg_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %30
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i32, i32* @O_CREAT
%44 = call i32 @open(i8* %42, i32 %43, i32 420)
store i32 %44, i32* %6
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %39
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i64 @stat(i8* %53, %struct.stat* %9)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %56, %48
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%60 = load i64, i64* %59
%61 = load i64, i64* %7
%62 = icmp eq i64 %60, %61
%63 = zext i1 %62 to i64
%64 = select i1 %62, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0)
%65 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%66 = load i64, i64* %65
%67 = load i64, i64* %8
%68 = icmp eq i64 %66, %67
%69 = zext i1 %68 to i64
%70 = select i1 %68, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0)
%71 = call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %64, i8* %70)
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = call i32 @unlink(i8* %74)
%76 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [23 x i8] c"Usage: tfsgid gid path\00"
@.str.1 = [8 x i8] c"setegid\00"
@F_OK = global i32 0
@.str.2 = [12 x i8] c"file exists\00"
@O_CREAT = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [5 x i8] c"stat\00"
@.str.5 = [31 x i8] c"file gid is %s egid and %s gid\00"
@.str.6 = [8 x i8] c"same as\00"
@.str.7 = [15 x i8] c"different than\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i8*, i8** %1
%6 = tail call i32 @diod_log_init(i8* %5)
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i64 @strtoul(i8* nocapture %13, i32* null, i32 10)
%15 = tail call i64 (...) @getgid()
%16 = tail call i64 @setegid(i64 %14)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %11
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @F_OK
%24 = tail call i64 @access(i8* %22, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = tail call i32 @msg_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load i8*, i8** %21
%30 = load i32, i32* @O_CREAT
%31 = tail call i32 @open(i8* %29, i32 %30, i32 420)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = tail call i32 @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = tail call i32 @close(i32 %31)
%37 = load i8*, i8** %21
%38 = call i64 @stat(i8* %37, %struct.stat* %3)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%44 = load i64, i64* %43
%45 = icmp eq i64 %44, %14
%46 = select i1 %45, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0)
%47 = icmp eq i64 %44, %15
%48 = select i1 %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0)
%49 = call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %46, i8* %48)
%50 = load i8*, i8** %21
%51 = call i32 @unlink(i8* %50)
%52 = call i32 @exit(i32 0)
unreachable
}