#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 passwd {
  int /*<<< orphan*/ pw_name;
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ atoi(char *);
scalar_t__ chmod(char const *, int);
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ exit(int);
char *getdef_str(char *);
struct group *getgrnam(char *);
int /*<<< orphan*/ is_my_tty(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, ...);
void chown_tty(const char *tty, const struct passwd *info) {
  char buf[200], full_tty[200];
  char *group;
  struct group *grent;
  gid_t gid;
  if (!(group = getdef_str("TTYGROUP")))
    gid = info->pw_gid;
  else if (group[0] >= '0' && group[0] <= '9')
    gid = atoi(group);
  else if ((grent = getgrnam(group)))
    gid = grent->gr_gid;
  else
    gid = info->pw_gid;
  if (*tty != '/') {
    snprintf(full_tty, sizeof full_tty, "/dev/%s", tty);
    tty = full_tty;
  }
  if (!is_my_tty(tty)) {
    syslog(LOG_WARNING, "unable to determine TTY name, got %s\n", tty);
    closelog();
    exit(1);
  }
  if (chown(tty, info->pw_uid, gid) || chmod(tty, 0600)) {
    snprintf(buf, sizeof buf, "Unable to change tty %s", tty);
    syslog(LOG_WARNING, "unable to change tty `%s' for user `%s'\n", tty,
           info->pw_name);
    closelog();
    perror(buf);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<chown_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe36(%rip),%rdi
call   1070 <getdef_str@plt>
mov    %rax,-0x1b8(%rbp)
cmp    $0x0,%rax
jne    11f2 <chown_tty+0x42>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x1c4(%rbp)
jmp    1277 <chown_tty+0xc7>
mov    -0x1b8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     122f <chown_tty+0x7f>
mov    -0x1b8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     122f <chown_tty+0x7f>
mov    -0x1b8(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c4(%rbp)
jmp    1272 <chown_tty+0xc2>
mov    -0x1b8(%rbp),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x1c0(%rbp)
cmp    $0x0,%rax
je     1260 <chown_tty+0xb0>
mov    -0x1c0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c4(%rbp)
jmp    126d <chown_tty+0xbd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x1c4(%rbp)
jmp    1272 <chown_tty+0xc2>
jmp    1277 <chown_tty+0xc7>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     12ae <chown_tty+0xfe>
lea    -0x1b0(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0xc8,%esi
lea    0xd6b(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1b0(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <is_my_tty@plt>
cmp    $0x0,%eax
jne    12ec <chown_tty+0x13c>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd3d(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x1c4(%rbp),%edx
call   1090 <chown@plt>
cmp    $0x0,%rax
jne    1324 <chown_tty+0x174>
mov    -0x8(%rbp),%rdi
mov    $0x180,%esi
call   10a0 <chmod@plt>
cmp    $0x0,%rax
je     137e <chown_tty+0x1ce>
lea    -0xe0(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0xc8,%esi
lea    0xcfc(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
lea    -0xe0(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chown_tty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x198,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe36(%rip),%rdi
call   1070 <getdef_str@plt>
test   %rax,%rax
je     11f8 <chown_tty+0x48>
mov    (%rax),%cl
add    $0xd0,%cl
mov    %rax,%rdi
cmp    $0x9,%cl
ja     11ea <chown_tty+0x3a>
call   10c0 <atoi@plt>
mov    %eax,%ebp
jmp    11fc <chown_tty+0x4c>
call   1060 <getgrnam@plt>
test   %rax,%rax
je     11f8 <chown_tty+0x48>
mov    (%rax),%ebp
jmp    11fc <chown_tty+0x4c>
mov    0x8(%r14),%ebp
cmpb   $0x2f,(%rbx)
je     121e <chown_tty+0x6e>
mov    %rsp,%r15
lea    0xdfe(%rip),%rdx
mov    %r15,%rdi
mov    $0xc8,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rbx
mov    %rbx,%rdi
call   1050 <is_my_tty@plt>
test   %eax,%eax
je     12b6 <chown_tty+0x106>
mov    0x4(%r14),%esi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <chown@plt>
test   %rax,%rax
jne    1261 <chown_tty+0xb1>
mov    %rbx,%rdi
mov    $0x180,%esi
call   10a0 <chmod@plt>
test   %rax,%rax
jne    1261 <chown_tty+0xb1>
add    $0x198,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcf(%rip),%rdx
lea    0xd0(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc8,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%ecx
lea    0xdbc(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
mov    %rbp,%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
%struct.group = type { i32 }
@.str = [9 x i8] c"TTYGROUP\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
@LOG_WARNING = global i32 0
@.str.2 = [38 x i8] c"unable to determine TTY name, got %s\0A\00"
@.str.3 = [24 x i8] c"Unable to change tty %s\00"
@.str.4 = [41 x i8] c"unable to change tty `%s' for user `%s'\0A\00"
define void @chown_tty(i8* %0, %struct.passwd* %1) {
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca [200 x i8]
%6 = alloca [200 x i8]
%7 = alloca i8*
%8 = alloca %struct.group*
%9 = alloca i32
store i8* %0, i8** %3
store %struct.passwd* %1, %struct.passwd** %4
%10 = call i8* @getdef_str(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load %struct.passwd*, %struct.passwd** %4
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 2
%15 = load i32, i32* %14
store i32 %15, i32* %9
br label %45
16: ; preds = %2
%17 = load i8*, i8** %7
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp sge i32 %20, 48
br i1 %21, label %22, label %31
22: ; preds = %16
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp sle i32 %26, 57
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %7
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %9
br label %44
31: ; preds = %22, %16
%32 = load i8*, i8** %7
%33 = call %struct.group* @getgrnam(i8* %32)
store %struct.group* %33, %struct.group** %8
%34 = icmp ne %struct.group* %33, null
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load %struct.group*, %struct.group** %8
%37 = getelementptr inbounds %struct.group, %struct.group* %36, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %9
br label %43
39: ; preds = %31
%40 = load %struct.passwd*, %struct.passwd** %4
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 2
%42 = load i32, i32* %41
store i32 %42, i32* %9
br label %43
43: ; preds = %39, %35
br label %44
44: ; preds = %43, %28
br label %45
45: ; preds = %44, %12
%46 = load i8*, i8** %3
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 47
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%52 = load i8*, i8** %3
%53 = call i32 @snprintf(i8* %51, i32 200, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %52)
%54 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
store i8* %54, i8** %3
br label %55
55: ; preds = %50, %45
%56 = load i8*, i8** %3
%57 = call i32 @is_my_tty(i8* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %65, label %59
59: ; preds = %55
%60 = load i32, i32* @LOG_WARNING
%61 = load i8*, i8** %3
%62 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %60, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = call i32 (...) @closelog()
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %55
%66 = load i8*, i8** %3
%67 = load %struct.passwd*, %struct.passwd** %4
%68 = getelementptr inbounds %struct.passwd, %struct.passwd* %67, i32 0, i32 1
%69 = load i32, i32* %68
%70 = load i32, i32* %9
%71 = call i64 @chown(i8* %66, i32 %69, i32 %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %77, label %73
73: ; preds = %65
%74 = load i8*, i8** %3
%75 = call i64 @chmod(i8* %74, i32 384)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %91
77: ; preds = %73, %65
%78 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%79 = load i8*, i8** %3
%80 = call i32 @snprintf(i8* %78, i32 200, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %79)
%81 = load i32, i32* @LOG_WARNING
%82 = load i8*, i8** %3
%83 = load %struct.passwd*, %struct.passwd** %4
%84 = getelementptr inbounds %struct.passwd, %struct.passwd* %83, i32 0, i32 0
%85 = load i32, i32* %84
%86 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %81, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %82, i32 %85)
%87 = call i32 (...) @closelog()
%88 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%89 = call i32 @perror(i8* %88)
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %73
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
%struct.group = type { i32 }
@.str = [9 x i8] c"TTYGROUP\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
@LOG_WARNING = global i32 0
@.str.2 = [38 x i8] c"unable to determine TTY name, got %s\0A\00"
@.str.3 = [24 x i8] c"Unable to change tty %s\00"
@.str.4 = [41 x i8] c"unable to change tty `%s' for user `%s'\0A\00"
define void @chown_tty(i8* %0, %struct.passwd* nocapture %1) {
%3 = alloca [200 x i8]
%4 = alloca [200 x i8]
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%7 = tail call i8* @getdef_str(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 2
%11 = load i32, i32* %10
br label %27
12: ; preds = %2
%13 = load i8, i8* %7
%14 = add i8 %13, -48
%15 = icmp ult i8 %14, 10
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @atoi(i8* %7)
br label %27
18: ; preds = %12
%19 = tail call %struct.group* @getgrnam(i8* %7)
%20 = icmp eq %struct.group* %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.group, %struct.group* %19, i64 0, i32 0
%23 = load i32, i32* %22
br label %27
24: ; preds = %18
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 2
%26 = load i32, i32* %25
br label %27
27: ; preds = %16, %24, %21, %9
%28 = phi i32 [ %17, %16 ], [ %23, %21 ], [ %26, %24 ], [ %11, %9 ]
%29 = load i8, i8* %0
%30 = icmp eq i8 %29, 47
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @snprintf(i8* %6, i32 200, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %31, %27
%34 = phi i8* [ %6, %31 ], [ %0, %27 ]
%35 = call i32 @is_my_tty(i8* %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @LOG_WARNING
%39 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%40 = call i32 (...) @closelog()
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
%43 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 1
%44 = load i32, i32* %43
%45 = call i64 @chown(i8* %34, i32 %44, i32 %28)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = call i64 @chmod(i8* %34, i32 384)
%49 = icmp eq i64 %48, 0
br i1 %49, label %59, label %50
50: ; preds = %47, %42
%51 = call i32 @snprintf(i8* %5, i32 200, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%52 = load i32, i32* @LOG_WARNING
%53 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %52, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %34, i32 %54)
%56 = call i32 (...) @closelog()
%57 = call i32 @perror(i8* %5)
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %47
ret void
}