#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_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
int /*<<< orphan*/ endpwent();
int /*<<< orphan*/ exit(int);
struct passwd *getpwnam(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
void todaemon(void) {
  struct passwd *pwp;
  pwp = getpwnam("daemon");
  endpwent();
  if (pwp == NULL || setgid(pwp->pw_gid) || setuid(pwp->pw_uid))
    exit(1);
}


x86 assembly (clang, -O0):
<todaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <endpwent@plt>
cmpq   $0x0,-0x8(%rbp)
je     11a5 <todaemon+0x55>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
cmp    $0x0,%rax
jne    11a5 <todaemon+0x55>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1070 <setuid@plt>
cmp    $0x0,%rax
je     11af <todaemon+0x5f>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<todaemon>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1030 <getpwnam@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <endpwent@plt>
test   %rbx,%rbx
je     1187 <todaemon+0x37>
mov    0x4(%rbx),%edi
call   1050 <setgid@plt>
test   %rax,%rax
jne    1187 <todaemon+0x37>
mov    (%rbx),%edi
call   1070 <setuid@plt>
test   %rax,%rax
jne    1187 <todaemon+0x37>
pop    %rbx
ret
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"daemon\00"
define void @todaemon() {
%1 = alloca %struct.passwd*
%2 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store %struct.passwd* %2, %struct.passwd** %1
%3 = call i32 (...) @endpwent()
%4 = load %struct.passwd*, %struct.passwd** %1
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %18, label %6
6: ; preds = %0
%7 = load %struct.passwd*, %struct.passwd** %1
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i32 0, i32 1
%9 = load i32, i32* %8
%10 = call i64 @setgid(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %6
%13 = load %struct.passwd*, %struct.passwd** %1
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @setuid(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12, %6, %0
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"daemon\00"
define void @todaemon() {
%1 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (...) @endpwent()
%3 = icmp eq %struct.passwd* %1, null
br i1 %3, label %14, label %4
4: ; preds = %0
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 1
%6 = load i32, i32* %5
%7 = tail call i64 @setgid(i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i64 @setuid(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9, %4, %0
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
ret void
}